Swiftpack.co -  NPasini/NetworkManager as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
NPasini/NetworkManager
A Swift Package containing the implementation of a Network Manager based on URLSession
.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 weeks 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.

Related Packages

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