Swiftpack.co - MFB-Technologies-Inc/NetworkService as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by MFB-Technologies-Inc.
MFB-Technologies-Inc/NetworkService 3.0.0
A reactive library for using URLSession
⭐️ 2
🕓 20 hours ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/MFB-Technologies-Inc/NetworkService.git", from: "3.0.0")

NetworkService

CI codecov

Reactive wrapper for URLSession using Combine. At its core, the library consist of the NetworkServiceClient protocol along with a minimal implementation NetworkService.

TopLevelCodable

A notable convenience the library provides is the TopLevelCodable protocol that enables easy encoding and decoding of conforming types. The protocol associates a TopLevelEncoder and TopLevelDecoder with a given type so that it is used by the library without explicitly passing it as a parameter. Additionally, TopLevelEncodable and TopLevelDecodable are included.

Basic Usage

import NetworkService
let networkService = NetworkService()
let url = URL(string: "http://www.foobar.com")!
struct Foo: TopLevelCodable {
    static var encoder: JSONEncoder { JSONEncoder() }
    static var decoder: JSONDecoder { JSONDecoder() }
    let bar: Int
}
let foo = Foo(bar: 0)

GET

let publisher: AnyPublisher<Foo, Failuer> = networkService.get(url)
let cancellable = publisher.assertNoFailure().sink { foo in
    print(foo.bar)
}

POST

let publisher: AnyPublisher<Foo, Failuer> = networkService.post(foo, to: url)
let cancellable = publisher.assertNoFailure().sink { foo in
    print(foo.bar)
}

PUT

let publisher: AnyPublisher<Foo, Failuer> = networkService.put(foo, to: url)
let cancellable = publisher.assertNoFailure().sink { foo in
    print(foo.bar)
}

DELETE

let publisher: AnyPublisher<Foo, Failuer> = networkService.get(url)
let cancellable = publisher.assertNoFailure().sink { _ in }

Start

var request = URLRequest(url: url)
request.method = .GET
let publisher: AnyPublisher<Foo, Failuer> = networkService.start(request)
let cancellable = publisher.assertNoFailure().sink { foo in
    print(foo.bar)
}

NetworkServiceTestHelper

Provides MockNetworkService which is an implementation of NetworkServiceClient for testing. Supports defining set output values for all network functions, repeating values, and delaying responses.

Installation

Currently, only Swift Package Manager is supported.

GitHub

link
Stars: 2
Last commit: Yesterday
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

3.0.0 Release Async API
Yesterday

What's Changed

Full Changelog: https://github.com/MFB-Technologies-Inc/NetworkService/compare/2.1.4...3.0.0

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