-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSignUpService.swift
More file actions
107 lines (86 loc) · 4.06 KB
/
Copy pathSignUpService.swift
File metadata and controls
107 lines (86 loc) · 4.06 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
//
// SignUpService.swift
// NativeAppTemplate
//
import Foundation
struct SignUpsService {
var networkClient = NativeAppTemplateAPI()
var session: URLSession = .pinned
}
extension SignUpsService {
func makeShopkeeper(signUp: SignUp) async throws -> Shopkeeper {
try await makeRequest(request: MakeShopkeeperRequest(signUp: signUp))
}
func updateShopkeeper(id: String, signUp: SignUp) async throws -> UpdateShopkeeperRequest.Response {
let request = UpdateShopkeeperRequest(id: id, signUp: signUp)
return try await makeRequest(request: request)
}
func destroyShopkeeper() async throws -> DestroyShopkeeperRequest.Response {
try await makeRequest(request: DestroyShopkeeperRequest())
}
func sendResetPasswordInstruction(sendResetPassword: SendResetPassword) async throws
-> SendResetPasswordInstructionRequest.Response {
try await makeRequest(request: SendResetPasswordInstructionRequest(sendResetPassword: sendResetPassword))
}
func sendConfirmationInstruction(sendConfirmation: SendConfirmation) async throws
-> SendConfirmationInstructionRequest.Response {
try await makeRequest(request: SendConfirmationInstructionRequest(sendConfirmation: sendConfirmation))
}
@MainActor func makeRequest<Request: NativeAppTemplate.Request>(
request: Request,
parameters: [Parameter]? = nil
) async throws -> Request.Response {
func prepare(
request: some NativeAppTemplate.Request,
parameters: [Parameter]? = nil
) throws -> URLRequest {
let pathURL = networkClient.environment.baseURL.appendingPathComponent(request.path)
guard let components = URLComponents(
url: pathURL,
resolvingAgainstBaseURL: false
) else {
throw URLError(.badURL)
}
guard let url = components.url
else { throw URLError(.badURL) }
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = request.method.rawValue
// body *needs* to be the last property that we set, because of this bug:
// https://bugs.swift.org/browse/SR-6687
urlRequest.httpBody = request.body
let headerAccessToken: HTTPHeader = ("access-token", networkClient.authToken)
let headerTokenType: HTTPHeader = ("token-type", "Bearer")
let headerClient: HTTPHeader = ("client", networkClient.client)
let headerExpiry: HTTPHeader = ("expiry", networkClient.expiry)
let headerUid: HTTPHeader = ("uid", networkClient.uid)
let headerSource: HTTPHeader = ("source", "ios")
let headers =
[headerAccessToken, headerTokenType, headerClient, headerExpiry, headerUid, headerSource]
+ [networkClient.additionalHeaders, request.additionalHeaders].joined()
if !headerAccessToken.value.isEmpty {
headers.forEach { urlRequest.addValue($0.value, forHTTPHeaderField: $0.key) }
}
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
urlRequest.setValue("ios", forHTTPHeaderField: "source")
return urlRequest
}
let (data, response) = try await session.data(
for: prepare(request: request)
)
let statusCode = (response as? HTTPURLResponse)?.statusCode
guard statusCode.map((200 ..< 300).contains) == true
else {
var errorMessage: String?
do {
if let json = try JSONSerialization.jsonObject(with: data) as? [String: Any],
let theErrorMessage = json["error_message"] as? String {
errorMessage = theErrorMessage
}
} catch {
throw NativeAppTemplateAPIError.requestFailed(nil, statusCode ?? 0, "")
}
throw NativeAppTemplateAPIError.requestFailed(nil, statusCode ?? 0, errorMessage)
}
return try request.handle(response: data)
}
}