Async/await http client using new concurrency model in Swift
Network layer for running requests like GET, POST, PUT, DELETE etc customizable with coders. There’s ability to retry request with different strategies
Features
Multiplatform
You have fast-track functions to make requests immediately by url or build the infrastructure configuration that suits you
Stand alone package without any dependencies using just Apple’s facilities
Set up amount of attempts(retry) with “Exponential backoff” or “Constant backoff” strategy if request fails. Exponential backoff is a strategy in which you increase the delays between retries. Constant backoff is a strategy when delay between retries is a constant value
Different strategies to validate URLResponse
Customizable for different requests schemes from classic CRUD Rest to what suits to you
Customizable in terms of URLSession
Customizable in terms of URLSessionTaskDelegate, URLSessionDelegate
Based on interfaces not implementations
Customizable with coders You can easily change format from json to xml or text just changing the coder
Fast-track functions return (Data, URLResponse) if you need to validate status code you can use funcvalidateStatus check different strategies Http.Validate.Status
let (data, response) = try await Http.Get.from(url)
let rule : Http.Validate.Status = .range(200..<300)
try validateStatus(response, by: rule)
Extended track
1. Create
let url = URL(string: "http://localhost:3000")
let http = Http.Proxy(baseURL: url)
with custom configuration
let cfg = Http.Configuration(
reader: SomeReader(),
writer: SomeWriter(),
baseURL: baseURL,
session: session)
let http = Http.Proxy(config: cfg)
public func send<T>(
with request : URLRequest,
retry strategy : RetryService.Strategy = .exponential(),
_ validate : [Http.Validate] = [.status(200)],
_ taskDelegate: ITaskDelegate? = nil
) async throws -> Http.Response<T> where T : Decodable
Retry strategy
This package uses stand alone package providing retry policy. The service creates sequence of the delays (nanoseconds) according to chosen strategy for more details folow the link retry service
type
description
constant
The strategy implements constant backoff
exponential [default]
The strategy implements exponential backoff
Validate
Is an array of different rules to check URLResponse.
Currently is implemented for validating status code.
Status code
type
description
const(Int) [default] 200
Validate by exact value
range(Range) [default] 200..<300
Validate by range
predicate(Predicate)
Validate by predicate func if you need some specific processing logic
check(ErrorFn)
Check status and return custom error if status is not valid
let fn : (Int) -> Bool = { status in status == 201 }
try await http.get(path: path, validate: [.status(.predicate(fn))])
There’s an example replicate toolkit for swift how to use it with a custom response error format that has different format then the successful response
The concept
Proxy is defining a communication layer and responsible for exchanging data with data source. There might be Http proxy, File proxy etc or some flavors REST proxy, LongFile proxy.
Reader and Writer are used to interpret data.
Try it in the real environment
Simple server installation (mocking with NodeJS Express)
To try it in the real environment. I suggest installing the basic NodeJS Express boilerplate. Take a look on the video snippet how easy it is to get it through Webstorm that is accessible for free for a trial period.
Async/await http client using new concurrency model in Swift
Network layer for running requests like GET, POST, PUT, DELETE etc customizable with coders. There’s ability to retry request with different strategies
Features
Fast track
1. Use
Fast-track functions return (Data, URLResponse) if you need to validate status code you can use func validateStatus check different strategies Http.Validate.Status
Extended track
1. Create
with custom configuration
2. Use
GET
GET with retry
GET with validate status code 200..<300
POST
POST with Delegate collecting metrics
PUT
DELETE
Custom request
Retry strategy
This package uses stand alone package providing retry policy. The service creates sequence of the delays (nanoseconds) according to chosen strategy for more details folow the link retry service
Validate
Is an array of different rules to check URLResponse. Currently is implemented for validating status code.
Status code
By range
By predicate
There’s an example replicate toolkit for swift how to use it with a custom response error format that has different format then the successful response
The concept
Try it in the real environment
Simple server installation (mocking with NodeJS Express)
To try it in the real environment. I suggest installing the basic NodeJS Express boilerplate. Take a look on the video snippet how easy it is to get it through Webstorm that is accessible for free for a trial period.
Documentation(API)
SwiftUI example for the package
Async http client example
Used by packages
Replicate toolkit for swift