Harbor is a library for making API requests in Swift in a simple way using async/await.
- Swift 5
- iOS 13.0
You can add Harbor to your project using CocoaPods or Swift Package Manager.
Add the following line to your Podfile:
pod 'Harbor'
Add the following to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/javiermanzo/Harbor.git")
]
To make a request using Harbor, you need to create a class that implements the HServiceProtocolWithResult
protocol if you want to parse the response, or HServiceProtocol
if the response doesn't need to be parsed.
class MyRequest: HServiceProtocol {
var url: String = "YOUR_URL/{PATH_PARAM}"
var httpMethod: HHttpMethod = .post
var headers: [String : String]?
var queryParameters: [String : String]? = nil
var pathParameters: [String : String]? = nil
var body: [String : Any]? = nil
var needAuth: Bool = true
var timeout: TimeInterval = 5
init() {
self.pathParameters = [
"PATH_PARAM" : "value"
]
self.body = [
"bodyParameter" : "value"
]
self.headers = [
"header" : "value"
]
}
}
class MyRequestWithResult: HServiceProtocolWithResult {
typealias T = MyModel
var url: String = "YOUR_URL/{PATH_PARAM}"
var httpMethod: HHttpMethod = .get
var headers: [String : String]?
var queryParameters: [String : String]? = nil
var pathParameters: [String : String]? = nil
var body: [String : Any]? = nil
var needAuth: Bool = true
var timeout: TimeInterval = 5
init() {
self.pathParameters = [
"PATH_PARAM" : "value"
]
self.queryParameters = [
"queryParameter" : "value"
]
self.headers = [
"header" : "value"
]
}
}
Once the request class is created, you can execute the request using the request
method.
Task {
let response = await MyRequestWithResult().request()
}
If you use HServiceProtocol
, the result of calling request()
will be an HResponse
enum.
switch response {
case .success:
break
case .cancelled:
break
case .error(let error):
break
}
If you use HServiceProtocolWithResult
, the result of calling request()
will be an HResponseWithResult
enum.
switch response {
case .success(let result):
break
case .cancelled:
break
case .error(let error):
break
}
You can also implement the HAuthProviderProtocol
protocol if you need to handle authentication.
You setAuthProvider
method of the Harbor
class to set the authentication provider.
You have to create a class that implements HAuthProviderProtocol
class MyAuthProvider: HAuthProviderProtocol {
func getCredentialsHeader() -> [String : String] {
// Return the authentication headers
}
}
After that, you have to set your Auth provider:
Harbor.setAuthProvider(MyAuthProvider())
If the request class has the needAuth
property set to true
, Harbor will call the getCredentialsHeader
method of the authentication provider to get the authentication headers before executing the request.
You can cancel the task of the request if it is running. request()
will return cancelled
.
let task = Task {
let response = await MyRequestWithResult().request()
}
task.cancel()
You can print debug information about your request using the HDebugServiceProtocol
protocol.
Implement the protocol in the request class.
class MyRequest: HServiceProtocolWithResult, HDebugServiceProtocol {
var debugType: HDebugServiceType = .requestAndResponse
....
}
debugType
defines what you want to print in console. The options are none, request, response or requestAndResponse
.
When your request is called, you will see in the Xcode console the information about your request.
If you run into any problems, please submit an issue. Pull requests are also welcome!
Harbor was created by Javier Manzo.
Harbor is available under the MIT license. See the LICENSE file for more info.