Giter VIP home page Giter VIP logo

apikit's Introduction

ApiKit Logo

Version Swift 5.7 MIT License Twitter: @danielsaidi Mastodon: @danielsaidi@mastodon.social

About ApiKit

ApiKit is a Swift library that makes it easy to integrate with any external REST APIs.

ApiKit has an ApiClient protocol that can fetch any URLRequest and decode the data to any Decodable type. It's implemented by URLSession so you can either use URLSession.shared or create your own custom service.

ApiKit has an ApiEnvironment and ApiRoute model that can be used to model the available environments and routes for 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.

Any ApiClient can then fetch any ApiRoute from any ApiEnvironment and automatically have the result decoded to any Decodable type.

Installation

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.

Supported Platforms

ApiKit supports iOS 13, macOS 11, tvOS 13 and watchOS 6.

Getting started

Implementing API integrations with ApiKit is very easy. You can either fetch raw URLRequests and handle the raw data, or create custom ApiEnvironment and ApiRoute types to model various APIs.

For instance, with a TheMovieDb-specific environment:

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 a TheMovieDb-specific route:

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 could easily fetch movies like this:

let client = URLSession.shared
let environment = TheMovieDb.Environment.production("API_KEY") 
let route = TheMovieDb.Route.movie(id: 123) 
let movie: TheMovieDb.Movie = try await client.fetchItem(at: route, in: environment)

For more information, please see the online documentation and getting started guide guide.

Documentation

The online documentation contains more information, code examples, etc., and makes it easy to 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

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.

apikit's People

Contributors

danielsaidi avatar maximbazarov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.