Skip to content

Commit 6e92f94

Browse files
authored
Merge pull request #3 from iamjono/master
Simpler non-typed object version
2 parents be5ad27 + ae60c40 commit 6e92f94

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

Sources/Curly/HttpClient.swift

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,85 @@ public struct HttpClient {
222222
}
223223
}
224224

225+
// Simpler non-typed object version. Will use the same error methodology, but instead of using a codable conversion, will just respond with a [String: Any]. Useful in cases where one does not know the structure of what is going to be returned.
226+
public static func request<E: ClientAPIDefaultErrorResponseProtocol>(
227+
_ method: HTTPMethod,
228+
_ url: String,
229+
error errorType: E.Type,
230+
body: String = "",
231+
json: [String: Any] = [String: Any](),
232+
params: [String: Any] = [String: Any](),
233+
headers: [String: Any] = [String: Any](), // additional headers. use to override things like weird auth formats
234+
encoding: String = "json",
235+
bearerToken: String = "") throws -> [String: Any] {
236+
237+
var curlObject = CURLRequest(url, options: [CURLRequest.Option.httpMethod(method)])
238+
var byteArray = [UInt8]()
239+
240+
if !body.isEmpty {
241+
print(body.utf8)
242+
byteArray = [UInt8](body.utf8)
243+
} else if !json.isEmpty {
244+
do {
245+
print(try json.jsonEncodedString().utf8)
246+
byteArray = [UInt8](try json.jsonEncodedString().utf8)
247+
} catch {
248+
throw error
249+
}
250+
} else if !params.isEmpty {
251+
byteArray = [UInt8]((self.toParams(params).joined(separator: "&")).utf8)
252+
}
253+
254+
255+
if method == .post || method == .put || method == .patch {
256+
curlObject = CURLRequest(url, CURLRequest.Option.httpMethod(method), .postData(byteArray))
257+
} else {
258+
curlObject = CURLRequest(url, CURLRequest.Option.httpMethod(method))
259+
}
260+
261+
262+
curlObject.addHeader(.accept, value: "application/json")
263+
curlObject.addHeader(.cacheControl, value: "no-cache")
264+
curlObject.addHeader(.userAgent, value: "PerfectCodableRequest1.0")
265+
266+
for (i,v) in headers {
267+
curlObject.addHeader(.custom(name: i), value: "\(v)")
268+
}
269+
270+
271+
if !bearerToken.isEmpty {
272+
curlObject.addHeader(.authorization, value: "Bearer \(bearerToken)")
273+
}
274+
275+
if encoding == "json" {
276+
curlObject.addHeader(.contentType, value: "application/json")
277+
} else {
278+
curlObject.addHeader(.contentType, value: "application/x-www-form-urlencoded")
279+
}
280+
281+
do {
282+
let response = try curlObject.perform()
283+
if response.responseCode >= 400 {
284+
do {
285+
let e = try response.bodyJSON(errorType)
286+
throw e
287+
288+
} catch {
289+
let e = ClientAPIDefaultErrorResponse(error: ClientAPIDefaultErrorMsg(message: response.bodyString, type: "", param: "", code: "\(response.responseCode)"))
290+
throw e
291+
}
292+
}
293+
let model = try UTF8Encoding.encode(bytes: response.bodyBytes).jsonDecode() as? [String:Any] ?? [:]
294+
return model
295+
296+
} catch let error as CURLResponse.Error {
297+
let e = try error.response.bodyJSON(errorType)
298+
throw e
299+
300+
} catch {
301+
throw error
302+
}
303+
}
225304
private static func toParams(_ params:[String: Any]) -> [String] {
226305
var str = [String]()
227306
for (key, value) in params {

0 commit comments

Comments
 (0)