ApiKit helps you integrate with external REST APIs.
ApiKit has an ApiClient protocol that can fetch any URLRequest and decode the response to any Decodable type. It’s implemented by URLSession so you can either use URLSession.shared or create your own service.
ApiKit has ApiEnvironment and ApiRoute models that can be used to model any REST API, such as the base URL of a certain API environment, the URL of a certain route, which parameters and headers to send etc.
An ApiClient can then be used to fetch any ApiRoute from any ApiEnvironment and return a typed result.
In ApiKit, you can either fetch raw URLRequests and handle the raw data, or create custom ApiEnvironment and ApiRoute types to model various APIs and return typed results.
For instance, with this TheMovieDb-specific ApiEnvironment:
enum TheMovieDbEnvironment: ApiEnvironment {
case production(apiKey: String)
public var url: String {
switch self {
case .production: return "https://api.themoviedb.org/3"
}
}
public var headers: [String: String]? { nil }
public var queryParams: [String: String]? {
switch self {
case .production(let key): return ["api_key": key]
}
}
}
and this TheMovieDb-specific ApiRoute:
enum Route: ApiRoute {
case movie(id: Int)
public var path: String {
switch self {
case .movie(let id): return "movie/\(id)"
}
}
public var queryParams: [String: String]? {
switch self {
case .movie: return nil
}
}
public var httpMethod: HttpMethod { .get }
public var headers: [String: String]? { nil }
public var formParams: [String: String]? { nil }
public var postData: Data? { nil }
}
we can now fetch movies like this:
let session = URLSession.shared
let environment = TheMovieDb.Environment.production("API_KEY")
let route = TheMovieDb.Route.movie(id: 123)
let movie: TheMovieDb.Movie = try await session.fetchItem(at: route, in: environment)
About ApiKit
ApiKit helps you integrate with external REST APIs.
ApiKit has an
ApiClient
protocol that can fetch anyURLRequest
and decode the response to anyDecodable
type. It’s implemented byURLSession
so you can either useURLSession.shared
or create your own service.ApiKit has
ApiEnvironment
andApiRoute
models that can be used to model any REST API, such as the base URL of a certain API environment, the URL of a certain route, which parameters and headers to send etc.An
ApiClient
can then be used to fetch anyApiRoute
from anyApiEnvironment
and return a typed result.ApiKit supports
iOS 13
,macOS 11
,tvOS 13
andwatchOS 6
.Installation
ApiKit can be installed with the Swift Package Manager:
If you prefer to not have external dependencies, you can also just copy the source code into your app.
Getting started
The online documentation has a getting started guide to help you get started with ApiKit.
In ApiKit, you can either fetch raw
URLRequest
s and handle the raw data, or create customApiEnvironment
andApiRoute
types to model various APIs and return typed results.For instance, with this TheMovieDb-specific
ApiEnvironment
:and this TheMovieDb-specific
ApiRoute
:we can now fetch movies like this:
For more information, please see the online documentation and getting started guide guide.
Documentation
The online documentation has more information, code examples, etc., and lets you overview the various parts of the library.
Demo Application
The demo app lets you explore the library on iOS and macOS. To try it out, just open and run the
Demo
project.Support this library
I manage my various open-source projects in my free time and am really thankful for any help I can get from the community.
You can sponsor this project on GitHub Sponsors or get in touch for paid support.
Contact
Feel free to reach out if you have questions or if you want to contribute in any way:
License
ApiKit is available under the MIT license. See the LICENSE file for more info.