@@ -48,8 +48,6 @@ class ApiManager {
4848 var request = URLRequest ( url: url, timeoutInterval: Double . infinity)
4949 request. httpMethod = endpoint. method
5050
51-
52-
5351 if let formData = endpoint. formData {
5452 let boundary = " Boundary- \( UUID ( ) . uuidString) "
5553 request. setValue ( " multipart/form-data; boundary= \( boundary) " , forHTTPHeaderField: " Content-Type " )
@@ -75,7 +73,7 @@ class ApiManager {
7573 do {
7674 let filename = param [ " filename " ] as? String ?? " image.jpeg "
7775 let contentType = param [ " contentType " ] as? String ?? " application/octet-stream " // Default content type
78-
76+
7977 // Append headers for file data
8078 body. append ( " Content-Disposition: form-data; name= \" \( paramName) \" ; filename= \" \( filename) \" \r \n " . data ( using: . utf8) !)
8179 body. append ( " Content-Type: \( contentType) \r \n \r \n " . data ( using: . utf8) !)
@@ -95,14 +93,14 @@ class ApiManager {
9593 // Set the HTTP body
9694 request. httpBody = body
9795 } else if let httpBody = endpoint. body {
98- request. httpBody = httpBody
99- }
96+ request. httpBody = httpBody
97+ }
10098
10199 if let headers = endpoint. headers {
102- for (key, value) in headers {
103- request. setValue ( value, forHTTPHeaderField: key)
104- }
105- }
100+ for (key, value) in headers {
101+ request. setValue ( value, forHTTPHeaderField: key)
102+ }
103+ }
106104
107105 let task = URLSession . shared. dataTask ( with: request) { [ weak self] data, response, error in
108106 guard let self = self else {
@@ -116,8 +114,9 @@ class ApiManager {
116114 }
117115
118116 if let httpResponse = response as? HTTPURLResponse ,
119- httpResponse. statusCode == 401 ,
120- request. url!. lastPathComponent. contains ( " refresh-token " ) == false {
117+ httpResponse. statusCode == 401 ,
118+ request. url!. lastPathComponent. contains ( " refresh-token " ) == false ,
119+ request. url!. lastPathComponent. contains ( " authenticate " ) == false {
121120 print ( " Failed requests: \( String ( describing: request. url) ) " )
122121 TokenRefresher . shared. refreshToken { [ weak self] status in
123122 if status {
@@ -172,35 +171,35 @@ class ApiManager {
172171 } else {
173172 completion ( . failure( APIError . custom ( " Empty JSON " ) ) )
174173 }
175-
174+
176175 return
177176 }
178177 }
179178 else {
180- if let response = response as? HTTPURLResponse {
181- switch response. statusCode {
182- case 409 :
183- let conflictError = NSError ( domain: " goinfogame " , code: 409 , userInfo: [ NSLocalizedDescriptionKey: " version mismatch " ] )
184- completion ( . failure( APIError . conflict) )
185-
186- case 200 :
187- do {
188- if let decodedString = String ( data: data, encoding: . utf8) {
189- completion ( . success( decodedString as! T ) )
190- } else {
191- completion ( . failure( APIError . custom ( " Failed to decode string " ) ) )
192- }
193- } catch {
194- print ( " Failed to decode the non JSON: \( error. localizedDescription) " )
195- completion ( . failure( APIError . custom ( " Failed to decode non JSON " ) ) )
179+ if let response = response as? HTTPURLResponse {
180+ switch response. statusCode {
181+ case 409 :
182+ let conflictError = NSError ( domain: " goinfogame " , code: 409 , userInfo: [ NSLocalizedDescriptionKey: " version mismatch " ] )
183+ completion ( . failure( APIError . conflict) )
184+
185+ case 200 :
186+ do {
187+ if let decodedString = String ( data: data, encoding: . utf8) {
188+ completion ( . success( decodedString as! T ) )
189+ } else {
190+ completion ( . failure( APIError . custom ( " Failed to decode string " ) ) )
196191 }
197-
198- default :
199- completion ( . failure( APIError ( statusCode : response . statusCode ) ) )
192+ } catch {
193+ print ( " Failed to decode the non JSON: \( error . localizedDescription ) " )
194+ completion ( . failure( APIError . custom ( " Failed to decode non JSON " ) ) )
200195 }
201- } else {
202- completion ( . failure( APIError . custom ( " No valid HTTP response received " ) ) )
196+
197+ default :
198+ completion ( . failure( APIError ( statusCode: response. statusCode) ) )
203199 }
200+ } else {
201+ completion ( . failure( APIError . custom ( " No valid HTTP response received " ) ) )
202+ }
204203 }
205204 }
206205 task. resume ( )
0 commit comments