This repository has been archived by the owner on Apr 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplicationHandler.swift
241 lines (204 loc) · 10 KB
/
ApplicationHandler.swift
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//
// ApplicationHandler.swift
// mobile
//
// Created by cb on 06.09.23.
//
import Foundation
class ApplicationHandler {
static func fetchApplication(accessToken: String, jobId: Int, completion: @escaping (Result<ApplicationResponse, APIError>) -> Void) {
print("Started fetching own applications with: \naccess_token: \(accessToken)")
guard let urlComponents = URLComponents(string: Routes.ROOT_URL + Routes.JOBS_PATH + "/\(jobId)" + Routes.APPLICATION_PATH ) else {
completion(.failure(APIError.invalidURL))
return
}
guard let url = urlComponents.url else {
completion(.failure(APIError.invalidURL))
return
}
print("URL: \(url)")
RequestHandler.performRequest(url: url, httpMethod: HTTPMethod.GET, accessToken: accessToken, responseType: ApplicationResponse.self, completion: completion)
}
static func fetchOwnApplications(accessToken: String, completion: @escaping (Result<ApplicationsResponse, APIError>) -> Void) {
print("Started fetching own applications with: \naccess_token: \(accessToken)")
guard let urlComponents = URLComponents(string: Routes.ROOT_URL + Routes.USER_APPLICATIONS_PATH) else {
completion(.failure(APIError.invalidURL))
return
}
guard let url = urlComponents.url else {
completion(.failure(APIError.invalidURL))
return
}
print("URL: \(url)")
RequestHandler.performRequest(url: url, httpMethod: HTTPMethod.GET, accessToken: accessToken, responseType: ApplicationsResponse.self, completion: completion)
}
static func createNormalApplication(accessToken: String, application: Application, completion: @escaping (Result<APIResponse, APIError>) -> Void) {
print("Started creating application with: \naccess_token: \(accessToken)")
guard let urlComponents = URLComponents(string: Routes.ROOT_URL + Routes.JOBS_PATH + "/\(application.jobId)" + Routes.APPLICATIONS_PATH) else {
completion(.failure(APIError.invalidURL))
return
}
guard let url = urlComponents.url else {
completion(.failure(APIError.invalidURL))
return
}
print("URL: \(url)")
let requestBody = ["application": ["application_text": application.applicationText]]
RequestHandler.performRequest(
url: url,
httpMethod: HTTPMethod.POST,
accessToken: accessToken,
responseType: APIResponse.self,
requestBody: requestBody,
completion: completion
)
}
static func acceptApplication(accessToken: String, message: String?, application: Application, completion: @escaping (Result<APIResponse, APIError>) -> Void) {
print("Started accepting application with: \naccess_token: \(accessToken)\njobId: \(application.jobId)\nuserId: \(application.userId)")
guard let urlComponents = URLComponents(string: Routes.ROOT_URL + Routes.JOBS_PATH + "/\(application.jobId)" + Routes.APPLICATIONS_PATH + "/\(application.userId)" + Routes.ACCEPT_PATH) else {
completion(.failure(APIError.invalidURL))
return
}
guard let url = urlComponents.url else {
completion(.failure(APIError.invalidURL))
return
}
print("URL: \(url)")
if let message {
RequestHandler.performRequest(
url: url,
httpMethod: HTTPMethod.PATCH,
accessToken: accessToken,
responseType: APIResponse.self,
queryParameters: ["message": message],
completion: completion
)
} else {
RequestHandler.performRequest(
url: url,
httpMethod: HTTPMethod.PATCH,
accessToken: accessToken,
responseType: APIResponse.self,
completion: completion
)
}
}
static func createAttachmentApplication(accessToken: String, application: Application, attachment: Data, format: String, completion: @escaping (Result<APIResponse, APIError>) -> Void) {
print("Started creating application with: \naccess_token: \(accessToken)\njobId: \(application.jobId)\nuserId: \(application.userId)")
guard let urlComponents = URLComponents(string: Routes.ROOT_URL + Routes.JOBS_PATH + "/\(application.jobId)" + Routes.APPLICATIONS_PATH) else {
completion(.failure(APIError.invalidURL))
return
}
guard let url = urlComponents.url else {
completion(.failure(APIError.invalidURL))
return
}
var request = URLRequest(url: url)
request.httpMethod = "POST"
let boundary = UUID().uuidString
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.setValue(accessToken, forHTTPHeaderField: "access_token")
var body = Data()
body.append("--\(boundary)\r\n".data(using: .utf8)!)
body.append("Content-Disposition: form-data; name=\"application_text\"\r\n\r\n".data(using: .utf8)!)
body.append("\(application.applicationText)\r\n".data(using: .utf8)!)
var contentType = ""
switch format {
case ".pdf":
contentType = "application/pdf"
case ".docx":
contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
case ".xml":
contentType = "text/xml"
case ".txt":
contentType = "text/plain"
default:
contentType = ""
}
let filename = "\(application.jobId)_\(application.userId)_cv\(format)"
print("ContentType: \(contentType)")
print("fileName: \(filename)")
body.append("--\(boundary)\r\n".data(using: .utf8)!)
body.append("Content-Disposition: form-data; name=\"application_attachment\"; filename=\"\(filename)\"\r\n".data(using: .utf8)!)
body.append("Content-Type: \(contentType)\r\n\r\n".data(using: .utf8)!)
body.append(attachment)
body.append("\r\n".data(using: .utf8)!)
body.append("--\(boundary)--\r\n".data(using: .utf8)!)
request.httpBody = body
URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
print("Error fetching data: \(error)")
completion(.failure(APIError.networkError(error)))
return
}
if let httpResponse = response as? HTTPURLResponse {
let statusCode = httpResponse.statusCode
print("HTTP Response Code: \(statusCode)")
print("HTTP Response: \(response.debugDescription)")
RequestHandler.handleApiErrorsNew(data: data, statusCode: statusCode, completion: completion)
switch statusCode {
case 204:
completion(.failure(APIError.noContent(String(describing: ImageResponse.self))))
case 200:
if let data = data {
do {
if let responseString = String(data: data, encoding: .utf8) {
print("Data as String: \(responseString)")
} else {
print("Failed to convert data to string")
}
let responseData = try JSONDecoder().decode(APIResponse.self, from: data)
completion(.success(responseData))
} catch {
print("JSON Error: \(error)")
completion(.failure(APIError.jsonParsingError(error)))
}
} else {
completion(.failure(APIError.unknownError))
}
default:
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print("Error JSON = \(json)")
} catch {
completion(.failure(APIError.jsonParsingError(error)))
}
} else {
completion(.failure(APIError.unknownError))
}
}
}
}.resume()
}
static func rejectApplication(accessToken: String, message: String?, application: Application, completion: @escaping (Result<APIResponse, APIError>) -> Void) {
print("Started rejecting application with: \naccess_token: \(accessToken)\njobId: \(application.jobId)\nuserId: \(application.userId)")
guard let urlComponents = URLComponents(string: Routes.ROOT_URL + Routes.JOBS_PATH + "/\(application.jobId)" + Routes.APPLICATIONS_PATH + "/\(application.userId)" + Routes.REJECT_PATH) else {
completion(.failure(APIError.invalidURL))
return
}
guard let url = urlComponents.url else {
completion(.failure(APIError.invalidURL))
return
}
print("URL: \(url)")
if let message {
RequestHandler.performRequest(
url: url,
httpMethod: HTTPMethod.PATCH,
accessToken: accessToken,
responseType: APIResponse.self,
queryParameters: ["message": message],
completion: completion
)
} else {
RequestHandler.performRequest(
url: url,
httpMethod: HTTPMethod.PATCH,
accessToken: accessToken,
responseType: APIResponse.self,
completion: completion
)
}
}
}