This Swift Package provides the implementation of a Network Manager for dispatching URL Requests based on URLSession.
The following are the main concepts about the NetworkManager:
This Package relies on ReactiveSwift and OSLogger.
To integrate the package in your application you need to use Swift Package Manager and add NetworkManager as a dependency of your package in Package.swift:
.package(url: "https://github.com/NPasini/NetworkManager.git", from: "1.0.0"),
First thing you have to do is to create the model parsing the response:
struct YourResponse {
let example: String
init(example: String) {
self.example = example
}
}
extension YourResponse: CustomDecodable {
static func decode(_ data: Data) -> CustomDecodable? {
return try? JSONDecoder().decode(YourResponse.self, from: data)
}
}
Then you have to create your request:
class YourRequest: GetRequest<YourResponse> {
init() {
super.init(host: "yourhost.com", path: "path", version: "1")
}
}
Finally you have to use the APIPerformer to connect to the endpoint:
let subscriptionToEndpoint: SignalProducer<Result<YourResponse, NSError>, NSError> = SignalProducer {
(observer, lifetime) in
let subscription = APIPerformer.shared.performApi(YourRequest(), QoS: .default, completionQueue: .global(qos: .userInteractive)) { (result: Result<YourResponse, NSError>) in
switch result {
case .success(let response):
observer.send(value: Result.success(response))
observer.sendCompleted()
case .failure(let error):
observer.send(value: Result.failure(error))
observer.sendCompleted()
}
}
lifetime.observeEnded {
subscription.dispose()
}
}
Nicolò Pasini
link |
Stars: 1 |
Last commit: 2 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics