Light weight and simplistic API Client written in Swift using protocol oriented programming. You can send requests using async await or Combine publishers without having to change anything. SwiftAPIClient can help you implement your HTTP and server API calls with just a couple lines of code.
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.
Once you have your Swift package set up, adding SwiftAPIClient as a dependency is as easy as adding it to the dependencies value of your Package.swift.
import Foundation
import SwiftAPIClient
enum Endpoints: Endpoint {
case getData, addData(_ name: String)
var baseUrlString: String { "https://example.com/" }
var httpBody: RequestBody? {
switch self {
case .addData(let name):
return .jsonDictionary(["name" : name])
default: return nil
}
}
var httpMethod: HTTPMethod {
switch self {
case .getData: return .get
case .addData: return .post
}
}
var path: String {
// URL path should always start with forward slash
switch self {
case .getData:
return "/getData"
case .addData(let data):
return "/addData"
}
}
}
Endpoint example using struct
struct GetDataEndpoint: Endpoint {
var baseUrlString: String { "https://example.com/" }
// URL path should always start with forward slash
var path: String { "/getData" }
}
Decode JSON responses
If you’re expecting a JSON response you can use JsonResponse protocol which is a wrapper of Decodable with some extra build-in functionality. You can also create your own response type by conforming to Response protocol.
import Foundation
import SwiftAPIClient
struct ExampleDataResponse: JsonResponse {
let name: String
}
Send request using Combine
Create client wrapper with Combine using the endpoints
As mentioned the endpoint automatically handles both Combine and async, so you can use either approach.
For example if you want to use async all you need to do is replace send() with asyncSend() using the same Endponts definition above
SwiftAPIClient
Light weight and simplistic API Client written in Swift using protocol oriented programming. You can send requests using
async await
orCombine
publishers without having to change anything. SwiftAPIClient can help you implement your HTTP and server API calls with just a couple lines of code.Swift Package Manager
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.
Once you have your Swift package set up, adding SwiftAPIClient as a dependency is as easy as adding it to the dependencies value of your Package.swift.
Define your endpoints using enum
Endpoint example using
struct
Decode JSON responses
If you’re expecting a JSON response you can use
JsonResponse
protocol which is a wrapper ofDecodable
with some extra build-in functionality. You can also create your own response type by conforming toResponse
protocol.Send request using Combine
Create client wrapper with
Combine
using the endpointsSend request using async
As mentioned the endpoint automatically handles both
Combine
andasync
, so you can use either approach. For example if you want to useasync
all you need to do is replacesend()
withasyncSend()
using the sameEndponts
definition aboveValidating responses
The default validator code is below.
You can also create your own validator by conforming to
and then pass it as a validator to your endpoints to replace the default response validation
Contribution
Contributors are welcome.