目录
目录README.md

NetworkService

CI codecov

Async wrapper and dependency injection layer for URLSession. 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 result: Result<Foo, NetworkService.Failure> = await networkService.get(url)
let foo = try result.get()
print(foo.bar)

POST

let result: Result<Foo, NetworkService.Failure> = await networkService.post(foo, to: url)
let foo = try result.get()
print(foo.bar)

PUT

let result: Result<Foo, NetworkService.Failure> = await networkService.put(foo, to: url)
let foo = try result.get()
print(foo.bar)

DELETE

let result: Result<Foo, NetworkService.Failure> = await networkService.get(url)
let foo = try result.get()
print(foo.bar)

Start

var request = URLRequest(url: url)
request.method = .GET
let result = await networkService.start(request)
let foo = try result.get()
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.

关于
200.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号