Async/await http client using new concurrency model in Swift

Network layer for creating different set of network requests like GET, POST, PUT, DELETE etc customizable with coders

Features

  • Multiplatform
  • Stand alone package without any dependencies using just Apple’s facilities
  • Customizable for different requests schemes from classic CRUD Rest to what suits to you
  • Customizable in term of session
  • 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

1. Create

    let url = URL(string: "http://localhost:3000")
    let http = Http.Proxy(baseURL: url)

2. Use

GET

    try await http.get(path: "users")

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,
            _ taskDelegate: URLSessionTaskDelegate? = nil) async throws
        -> Http.Response<T> where T : Decodable

Http requests

Http requests

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.

Server instalation (NodeJS Express)

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

GitHub

View Github