HTTPClient

0.6.0

Communicate via HTTP easily in Swift.
uhooi/swift-http-client

What's New

0.6.0 - Support Swift Concurrency

2021-12-19T11:12:31Z

Breaking

  • Drop support for the following OS:
    @uhooi
    #23
    • iOS under 13.0
    • macOS under 10.15
    • tvOS under 13.0
    • watchOS under 6.0
  • Requires Xcode 13.2+ (Swift 5.5.2+)
    @uhooi
    #23

Enhancements

swift-http-client

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

Communicate via HTTP easily in Swift.

Table of Contents

Requirement

  • Xcode 13.2+ (Swift 5.5.2+)

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.6.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.6.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.6.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
    }
  2. 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) }
    }
  3. 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] }
    }
  4. 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) async throws -> UserID
    }
    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) async throws -> UserID {
            let requestBody = RegisterUserRequestBody(name: name, description: description)
            let responseBody = try await httpClient.request(RegisterUserRequest(), requestBody: requestBody)
            return responseBody.convertToUserID()
        }
    }
    do {
        let userID = try await VersatileAPIClient.shared.registerUser(name: "Uhooi", description: "Green monster.")
        // Do something.
    } catch {
        // Do error handling.
        print(error)
    }

Contribution

I would be happy if you contribute :)

Stats

Stats

Description

  • Swift Tools 5.5.0
View More Packages from this Author

Dependencies

  • None
Last updated: Mon Oct 21 2024 20:24:26 GMT-0900 (Hawaii-Aleutian Daylight Time)