Skip to content

Repository files navigation

DataRequest

Modularization of data requests using Alamofire with concurrency.

This package encourages a design pattern where the description of an endpoint is encapsulated into the properties of a structure. A similar design to a SwiftUI View. It adds rather than replaces; direct use of Alamofire (or URLSession) is still encouraged. There is also some helpful shorthand.

Example Usage

Define a decodable model returned in a response:

structModel:Decodable,Sendable{...}

Specify the configuration of the request endpoint:

structGetModel:DecodableRequest{typealiasResponseBody=ModelvarurlComponents:URLComponents{...}varmethod:HTTPMethod{...}varheaders:HTTPHeaders{...}varbody:HTTPBody?{...}}

Finally, execute the request in an asynchronous environment:

letmodel=tryawaitGetModel().request()

Installation

Add to the Package.swift:

dependencies:[.package(
url:"https://github.com/BenShutt/DataRequest.git",
branch:"main")]

Uploads

A DecodableRequest is a URLRequestMaker with the configuration properties of a data request defaulted. Since URLRequestMaker conforms to URLRequestConvertible you can use Alamofire directly:

AF.upload(data, with:Endpoint()).uploadProgress{ progress in...}.decodeValue()

where Endpoint is some URLRequestMaker.

Debug Logging

To log request/responses, make a new Session instance (managing its lifecycle accordingly) with a ResponseEventMonitor event monitor. The ResponseEventMonitor logs the debugDescription of the DataResponse. For example:

extensionSession{staticletdebug=Session(eventMonitors:[ResponseEventMonitor()])}

This can be returned in the session property of the DecodableRequest.

GitHub Actions

The .github/workflows/swift.yml GitHub action checks that the Swift package builds and the tests pass using a swift docker container.

About

Modularization of data requests using Alamofire with concurrency

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages