This commit is contained in:
Aakiyaru 2025-05-27 15:19:30 +07:00
parent b12622219a
commit 336990f12c
3 changed files with 10 additions and 6 deletions

View File

@ -4,12 +4,14 @@
import Alamofire import Alamofire
import Foundation import Foundation
//Класс для запросов
public final class BaseNetworkService: BaseNetworkServiceProtocol { public final class BaseNetworkService: BaseNetworkServiceProtocol {
public var baseURL: String? public var baseURL: String? // standart url
public init() {} public init() {}
//func request
public func execute(path: String, method: HTTPMethod, headers: HTTPHeaders, parameters: Parameters? = nil) async throws -> Data { public func execute(path: String, method: HTTPMethod, headers: HTTPHeaders, parameters: Parameters? = nil) async throws -> Data {
guard let baseURL = baseURL else { guard let baseURL = baseURL else {
fatalError("Error baseURL") fatalError("Error baseURL")

View File

@ -4,6 +4,7 @@
import Alamofire import Alamofire
import Foundation import Foundation
//protocol for BaseNetworkService
public protocol BaseNetworkServiceProtocol { public protocol BaseNetworkServiceProtocol {
func execute(path: String, method: HTTPMethod, headers: HTTPHeaders, parameters: Parameters?) async throws -> Data func execute(path: String, method: HTTPMethod, headers: HTTPHeaders, parameters: Parameters?) async throws -> Data
func configure(baseURL: String) func configure(baseURL: String)

View File

@ -7,15 +7,16 @@
import Foundation import Foundation
//Main class for request for user
public final class NetworkUser: NetworkUserProtocol { public final class NetworkUser: NetworkUserProtocol {
let baseUrl: BaseNetworkServiceProtocol let baseUrl: BaseNetworkServiceProtocol //Create request
public init(baseUrl: BaseNetworkServiceProtocol) { public init(baseUrl: BaseNetworkServiceProtocol) {
self.baseUrl = baseUrl self.baseUrl = baseUrl
} }
//method login
public func login(identity: String, password: String) async throws -> ServerResponseAuth { public func login(identity: String, password: String) async throws -> ServerResponseAuth {
let data = try await baseUrl.execute(path: URLS.login, method: .post, headers: Headers.headers(), parameters: [ let data = try await baseUrl.execute(path: URLS.login, method: .post, headers: Headers.headers(), parameters: [
"identity": identity, "identity": identity,
@ -23,7 +24,7 @@ public final class NetworkUser: NetworkUserProtocol {
]) ])
return try JSONDecoder().decode(ServerResponseAuth.self, from: data) return try JSONDecoder().decode(ServerResponseAuth.self, from: data)
} }
//method register
public func register(email: String, password: String, passwordConfirm: String, firstname: String, lastname: String, secondname: String, datebirthday: String, gender: String) async throws -> ServerResponseRegister { public func register(email: String, password: String, passwordConfirm: String, firstname: String, lastname: String, secondname: String, datebirthday: String, gender: String) async throws -> ServerResponseRegister {
let data = try await baseUrl.execute(path: URLS.register, method: .post, headers: Headers.headers(), parameters: [ let data = try await baseUrl.execute(path: URLS.register, method: .post, headers: Headers.headers(), parameters: [
"email": email, "email": email,
@ -32,11 +33,11 @@ public final class NetworkUser: NetworkUserProtocol {
]) ])
let result = try JSONDecoder().decode(ServerResponseRegister.self, from: data) let result = try JSONDecoder().decode(ServerResponseRegister.self, from: data)
let login = try await login(identity: email, password: password) let login = try await login(identity: email, password: password)
print("TOKEN: \(login.token)") UserDefaults.standard.set(login.token, forKey: "token")
let addUserResult = try await addUser(idUser: result.id, email: email, firstname: firstname, lastname: lastname, secondname: secondname, datebirthday: datebirthday, gender: gender, token: login.token) let addUserResult = try await addUser(idUser: result.id, email: email, firstname: firstname, lastname: lastname, secondname: secondname, datebirthday: datebirthday, gender: gender, token: login.token)
return addUserResult return addUserResult
} }
//method addUser
public func addUser(idUser: String, email: String, firstname: String, lastname: String, secondname: String, datebirthday: String, gender: String, token: String) async throws -> ServerResponseRegister { public func addUser(idUser: String, email: String, firstname: String, lastname: String, secondname: String, datebirthday: String, gender: String, token: String) async throws -> ServerResponseRegister {
let path = URLS.updateUserInfo+idUser let path = URLS.updateUserInfo+idUser
print(path) print(path)