Swiftpack.co - NPasini/NetworkManager as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by NPasini.
NPasini/NetworkManager 2.0.0
A Swift Package containing the implementation of a Network Manager based on URLSession
⭐️ 1
🕓 2 years ago
iOS
.package(url: "https://github.com/NPasini/NetworkManager.git", from: "2.0.0")

NetworkManager

This Swift Package provides the implementation of a Network Manager for dispatching URL Requests based on URLSession.

Getting Started

The following are the main concepts about the NetworkManager:

  • APIPerformer is the singleton instance which manages the connection to the url endpoints and retrieve the answer;
  • APIRequest is the base class for creating a request;
  • CustomDecodable is the protocol to which each response object should conform;
  • A NetworkError is returned in case of the process fails.

This Package relies on ReactiveSwift and OSLogger.

Installation

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"),

Example

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()
      }
}

Author

Nicolò Pasini

GitHub

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

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