Swiftpack.co - cpageler93/api-client as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by cpageler93.
cpageler93/api-client 0.4.0
Easy HTTP Client for Swift
⭐️ 3
🕓 2 weeks ago
iOS macOS tvOS
.package(url: "https://github.com/cpageler93/api-client.git", from: "0.4.0")

APIClient

Swift Xcode License Twitter: @cpageler93

APIClient is an easy to use HTTP Client for Swift based on swift-server/async-http-client..

Usage Example (GitHub)

Call your APIs like this

let githubClient = TestGitHubClient()
let repos = try githubClient.user.repositories(owner: "cpageler93").wait()

GitHub API Client (simplified)

import Foundation
import NIO
import NIOHTTP1
import APIClient


// Define your clients routes

class TestGitHubClient: APIClient {

    public var user: UserRoutes!

    init() {
        super.init(baseURL: URL(string: "https://api.github.com")!)
        user = UserRoutes(apiHandler: self.handler)
    }

}

// Define single routes

struct UserRoutes {

    let apiHandler: APIRouteHandler

    func repositories(owner: String) -> EventLoopFuture<[Repository]> {
        return apiHandler.get("/users/\(owner)/repos", headers: apiHandler.githubHeader())
    }

}


// Codable DTOs

struct Repository: Codable {

    var id: Int
    var name: String?
    var fullName: String?

}


// Header Helper

private extension APIRouteHandler {

    func githubHeader() -> HTTPHeaders {
        return headers(["User-Agent": "Swift GitHub Client"])
    }

}

Need Help?

Please submit an issue on GitHub or contact me via Mail or Twitter.

License

This project is licensed under the terms of the MIT license. See the LICENSE file.

GitHub

link
Stars: 3
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics