Swiftpack.co - uhooi/swift-http-client as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
uhooi/swift-http-client
Communicate via HTTP easily in Swift.
.package(url: "https://github.com/uhooi/swift-http-client.git", from: "0.4.0")

swift-http-client

CI Release CocoaPods Version Carthage compatible Swift Compatibility Platform Compatibility License Twitter

Communicate via HTTP easily in Swift.

Table of Contents

Installation

Swift Package Manager (Recommended)

Package

You can add this package to Package.swift, include it in your target dependencies.

let package = Package(
    dependencies: [
        .package(url: "https://github.com/uhooi/swift-http-client", .upToNextMajor(from: "0.4.0")),
    ],
    targets: [
        .target(
            name: "<your-target-name>",
            dependencies: ["HTTPClient"]),
    ]
)

Xcode

You can add this package on Xcode. See documentation.

CocoaPods

This library is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'UHIHTTPClient', '~> 0.4.0'

Carthage

This library is available through Carthage. To install it, simply add the following line to your Cartfile:

github "uhooi/swift-http-client" ~> 0.4.0

How to use

You can just import HTTPClient to use it.

  1. Implement a request body structure that conforms to the Encodable protocol. (Optional)
struct RegisterUserRequestBody: Encodable {
    let name: String
    let description: String
}
  1. Implement a response body structure that conforms to the Decodable protocol.
struct RegisterUserResponseBody: Decodable {
    let id: String
}

extension RegisterUserResponseBody {
    func convertToUserID() -> UserID { .init(id: self.id) }
}
  1. Implement a request structure that conforms to the Request protocol.
import HTTPClient

struct RegisterUserRequest: Request {
    typealias ResponseBody = RegisterUserResponseBody
    var path: String { "user/create_user" }
    var httpMethod: HTTPMethod { .post }
    var httpHeaders: [HTTPHeaderField: String]? { [.contentType: ContentType.applicationJson.rawValue] }
}
  1. Create an instance of the HTTPClient class and call the request method.
struct UserID: Identifiable, Equatable {
    let id: String
}
protocol VersatileRepository {
    func registerUser(name: String, description: String, completion: @escaping (Result<UserID, Error>) -> Void)
}
import HTTPClient

final class VersatileAPIClient {
    static let shared = VersatileAPIClient()
    
    private let httpClient = HTTPClient(baseURLString: "https://example.com/api/")
}

extension VersatileAPIClient: VersatileRepository {
    func registerUser(name: String, description: String, completion: @escaping (Result<UserID, Error>) -> Void) {
        let requestBody = RegisterUserRequestBody(name: name, description: description)
        httpClient.request(RegisterUserRequest(), requestBody: requestBody) { result in
            switch result {
            case let .success(responseBody):
                completion(.success(responseBody.convertToUserID()))
            case let .failure(error):
                completion(.failure(error))
            }
        }
    }
}
VersatileAPIClient.shared.registerUser(name: "Uhooi", description: "Green monster.") { result in
    switch result {
    case let .success(userID):
        // Do something.
    case let .failure(error):
        // Do error handling.
    }
}

Contribution

I would be happy if you contribute :)

GitHub

link
Stars: 14
Last commit: 3 days ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

0.4.0 - Remove ambiguous methods
11 weeks ago

Breaking

  • Remove ambiguous methods
    @uhooi
    #6

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