Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 9cb5cf14b95b566960199e173b82560ca9dcf6e6

COCOAPODS: 1.11.3
COCOAPODS: 1.11.2
4 changes: 2 additions & 2 deletions RevoHttp.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Pod::Spec.new do |spec|

spec.name = "RevoHttp"
spec.version = "0.3.2"
spec.version = "0.3.3"
spec.summary = "Foundation http."

spec.description = "A wrapper arround native Http requests for simpler and cleaner code"
Expand Down Expand Up @@ -39,7 +39,7 @@ Pod::Spec.new do |spec|

# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

spec.source = { :git => "https://github.com/revosystems/revohttp.git", :tag => "0.3.2" }
spec.source = { :git => "https://github.com/revosystems/revohttp.git", :tag => "0.3.3" }


# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
Expand Down
31 changes: 26 additions & 5 deletions RevoHttp/src/Http.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ public class Http : NSObject {

//MARK: Async call
public func call<T:Codable>(_ method:HttpRequest.Method, _ url:String, params:[String:Codable] = [:], headers:[String:String] = [:]) async throws -> T {
return try await withCheckedThrowingContinuation { continuation in
try await withCheckedThrowingContinuation { continuation in
let request = HttpRequest(method: method, url: url, params:params, headers: headers)

call(request) { response in
print(response.toString)
guard response.error == nil else {
return continuation.resume(throwing: HttpError.responseError)
return continuation.resume(throwing: HttpError.responseError())
}
guard response.statusCode >= 200 && response.statusCode < 300 else {
return continuation.resume(throwing: HttpError.reponseStatusError(response: response))
return continuation.resume(throwing: HttpError.responseStatusError(response: response))
}
guard let result:T = response.decoded() else {
return continuation.resume(throwing: HttpError.undecodableResponse)
Expand All @@ -66,15 +66,36 @@ public class Http : NSObject {
}
}
}


public func call<T:Codable>(_ method:HttpRequest.Method, _ url:String, body:String, headers:[String:String] = [:]) async throws -> T {
try await withCheckedThrowingContinuation { continuation in
let request = HttpRequest(method: method, url: url, headers: headers)
request.body = body

call(request) { response in
print(response.toString)
guard response.error == nil else {
return continuation.resume(throwing: HttpError.responseError())
}
guard response.statusCode >= 200 && response.statusCode < 300 else {
return continuation.resume(throwing: HttpError.responseStatusError(response: response))
}
guard let result:T = response.decoded() else {
return continuation.resume(throwing: HttpError.undecodableResponse)
}
return continuation.resume(returning:result)
}
}
}

public func call<T:Codable>(_ method:HttpRequest.Method, url:String, params:[String:Codable] = [:], headers:[String:String] = [:], then:@escaping(_ response:T?, _ error:Error?) -> Void) {
let request = HttpRequest(method: method, url: url, params: params, headers: headers)
call(request) { response in
let result:T? = response.decoded()
then(result, response.error)
}
}

public func get(_ url:String, params:[String:Codable] = [:], headers:[String:String] = [:], then:@escaping(_ response:HttpResponse) -> Void) {
let request = HttpRequest(method: .get, url: url, params: params, headers: headers)
call(request, then:then)
Expand Down
8 changes: 4 additions & 4 deletions RevoHttp/src/HttpError.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import Foundation

enum HttpError : Error {
public enum HttpError : Error {

case invalidUrl
case invalidParams
case responseError
case reponseStatusError(response:HttpResponse)
case responseError(errorMessage:String? = nil)
case responseStatusError(response:HttpResponse)
case undecodableResponse

var localizedDescription: String {
switch self {
case .invalidUrl: return "Malformed Url"
case .invalidParams: return "Invalid input params"
case .responseError: return "Response returned and error"
case .reponseStatusError: return "Response returned a non 200 status"
case .responseStatusError: return "Response returned a non 200 status"
case .undecodableResponse: return "Undecodable response"

}
Expand Down