A clean, generic interface from your application to an API, written in pure Swift with no other dependencies.
This library is made available as a Swift Package. To use it as a dependency in your project see this documentation from Apple.
General usage is as follows:
- Create your API client
let client = APIClient("api.github.com")
- Define your requests to the API you are interfacing with (see
Source/Examples/Requests
), and yourCodable
JSON objects to serialize the API's data into (seeSource/Examples/Data Structures
) - Instantiate your request, send, and handle the response!
// Assuming a client named `client` has already been created...
// Create our request (see Source/Examples/Requests/GetPosts.swift)
let request = GetPosts(userId: 1)
// Send request
client.send(request: request, completion: { result in
switch result {
case .success(let (response, data)):
// Handle your data here
// In this case, data is serialized into a `Post` object (see Source/Examples/Data Structures/Post.swift)
break
case .failure(let error):
// Handle your error here
break
}
})