Swiftpack.co - propellerhr/ios-http-client as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by propellerhr.
propellerhr/ios-http-client 1.0.0
Http Client
⭐️ 0
🕓 2 years ago
iOS
.package(url: "https://github.com/propellerhr/ios-http-client.git", from: "1.0.0")

HttpClient Swift Package

Instalation

SPM

.package(url: "https://github.com/propellerhr/ios-http-client", .upToNextMajor(from: "1.0.0"))

Usage

1. Create a HttpClientConfiguration

extension HttpClientConfiguration {
    static var myAppDev: HttpClientConfiguration {
        HttpClientConfiguration(baseURL: URL(string: "https://jsonplaceholder.typicode.com")!)
    }
}

2. Initialize the HttpClient

Initialize the HttpClient before using it by executing:

HttpClient.initialize(configuration: .myAppDev)

3. Create a Request

class ToDoRequest: HttpRequestInterface {
    typealias Body = EmptyHttpRequestBody
    var path: String = "/todos"
    var method: HttpMethod = .get
}

4. Create a Response object

struct ToDo: Codable {
    var userId: Int
    var title: String
    var completed: Bool
}

5. Executing requests

import RxSwift

let http = HttpClient.shared
let disposeBag = DisposeBag()

func fetchTodos() -> Single<[ToDo]> {
    HttpClient.shared.perform(request: ToDoRequest())
}

func yourMethod() {
  fetchTodo()
    .subscribe(on: ConcurrentDispatchQueueScheduler(qos: .background))
    .observe(on: MainScheduler.instance)
    .subscribe( { observer in
        switch observer {
        case .success(let todos):
            print("Fetched todos: \(model)")

        case .failure(let error):
            print("Whoops, something went wrong: \(error)")
        }
    })
    .disposed(by: disposeBag)
}

GitHub

link
Stars: 0
Last commit: 2 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Dependencies

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