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.
ApiKit supports iOS 13
, macOS 11
, tvOS 13
and watchOS 6
.
ApiKit can be installed with the Swift Package Manager:
https://github.com/danielsaidi/ApiKit.git
If you prefer to not have external dependencies, you can also just copy the source code into your app.
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 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)
For more information, please see the online documentation and getting started guide guide.
The online documentation has more information, code examples, etc., and lets you overview the various parts of the library.
The demo app lets you explore the library on iOS and macOS. To try it out, just open and run the Demo
project.
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.
Feel free to reach out if you have questions or if you want to contribute in any way:
ApiKit is available under the MIT license. See the LICENSE file for more info.
link |
Stars: 29 |
Last commit: 5 weeks ago |
This version adds Yelp
api integrations.
Yelp
is a new namespace with Yelp API integrations.Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics