AuthNetworkManager.swift
4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// NetworkManager.swift
// InterQR-Internship
//
// Created by Дмитрий Тимофеев on 25.06.2022.
//
import Foundation
import Alamofire
class AuthNetworkManager {
static let share = AuthNetworkManager()
var networkToken: String? {
get {
// обязательно ключи на подобии NETWORK_TOKEN выносить в Constants.networkTokenKey
return UserDefaults.standard.string(forKey: "NETWORK_TOKEN")
}
set (newValue) {
UserDefaults.standard.set(newValue, forKey: "NETWORK_TOKEN")
}
}
typealias InitWebServiceResponse = (InitResponseModel) -> Void
typealias StartWebServiceResponse = (StartResponseModel) -> Void
typealias VerifyWebServiceResponse = (VerifyResponseModel) -> Void
typealias LoginWebServiceResponse = (LoginResponseModel) -> Void
typealias LogoutWebServiceResponse = (LogoutResponseModel) -> Void
typealias FailedHandler = (Error) -> Void
func initRequest(_ model: InitRequestModel, completion: @escaping (InitResponseModel) -> Void, fail: @escaping FailedHandler) {
let params = ["device_uuid": model.deviceUUID,
"manufacturer": model.manufacturer,
"model": model.model,
"platform": model.platform,
"os_version": model.osVersion,
"app_version": model.appVersion] as [String: Any]
AF.request(Constants.baseURL + APIRoutes.initURL.urlString, method: .post, parameters: params, encoding: JSONEncoding.default).responseDecodable(of: InitResponseModel.self ) { response in
if let error = response.error {
fail(error)
} else {
if let res = response.value {
completion(res)
}
}
}
}
func startRequest(_ url: URL, model: StartRequestModel, completion: @escaping StartWebServiceResponse, fail: @escaping FailedHandler) {
let params = ["email_or_number": model.emailOrNumber] as [String: Any]
AF.request(url, method: .post, parameters: params, encoding: JSONEncoding.default).responseDecodable(of: StartResponseModel.self ) { response in
if let error = response.error {
fail(error)
} else {
if let res = response.value {
completion(res)
}
}
}
}
func verifyRequest(_ url: URL, model: VerifyRequestModel, completion: @escaping VerifyWebServiceResponse, fail: @escaping FailedHandler) {
let params = ["email_or_number": model.emailOrNumber,
"code": model.code,
"second_auth_token": model.secondAuthToken] as [String: Any]
AF.request(url, method: .post, parameters: params, encoding: JSONEncoding.default).responseDecodable(of: VerifyResponseModel.self ) { response in
if let error = response.error {
fail(error)
} else {
if let res = response.value {
self.networkToken = res.data.token
completion(res)
}
}
}
}
func loginRequest(_ url: URL, model: LoginRequestModel, completion: @escaping LoginWebServiceResponse, fail: @escaping FailedHandler) {
let headers = ["Authorization": "Bearer \(self.networkToken ?? "")"]
let params = ["device_uuid": model.deviceUUID] as [String: Any]
AF.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: HTTPHeaders(headers), interceptor: nil).responseDecodable(of: LoginResponseModel.self ) { response in
if let error = response.error {
fail(error)
} else {
if let res = response.value {
completion(res)
}
}
}
}
func logoutRequest(_ url: URL, model: LogoutRequestModel, completion: @escaping LogoutWebServiceResponse, fail: @escaping FailedHandler) {
let headers = ["Authorization": "Bearer \(self.networkToken ?? "")"]
let params = ["device_uuid": model.deviceUUID] as [String: Any]
AF.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: HTTPHeaders(headers), interceptor: nil).responseDecodable(of: LogoutResponseModel.self ) { response in
if let error = response.error {
fail(error)
} else {
if let res = response.value {
completion(res)
}
}
}
}
}