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
try await Http.Get.from(url, retry: 5, validate: [.status(200)])
try await Http.Post.from(url, taskDelegate : TaskDelegate())
try await Http.Put.from(url, body: data)
try await Http.Delete.from(url)
Fast-track functions return (Data, URLResponse) if you need to validate status code you can use func validateStatus 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)
2. Use
GET
try await http.get(path: "users")
GET with retry
try await http.get(path: "users", retry : 5)
GET with validate status code 200..<300
try await http.get(path: "users", validate: [.status(.range(200..<300))])
POST
try await http.post(
path: "users",
body: data,
query: [("name", "Igor"), ("page","1")],
headers: ["Content-Type": "application/json"])
POST with Delegate collecting metrics
try await http.post(path: "users", taskDelegate: DelegatePickingUpMetrics())
PUT
try await http.put(path: "users", body: data)
DELETE
try await http.delete(path: "users")
Custom request
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 |
By range
try await http.get(path: path, validate: [.status(.range(200..<300))])
By predicate
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.

Documentation(API)
- You need to have Xcode 13 installed in order to have access to Documentation Compiler (DocC)
- Go to Product > Build Documentation or ⌃⇧⌘ D
SwiftUI example for the package
Async http client example
Used by packages
Replicate toolkit for swift
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