Classes
The following classes are available globally.
-
Introduction
An object used to execute
Requestable(s)
Basic usage might look like this:
struct MyRequest: Requestable { ... } let manager = RequestManager(host: "https://api.myapp.com") let request = MyRequest() let cancellable = manager.makeRequest(request, retries: 3, sla: .seconds(120), on: DispathQueue.main) .removeErrors() .map(\.body) .sink { responseBody in ... }
Publisher
The publisher created by the
makeRequest
methods return aRequestResponse
and aRequestError
. See the documentation on both of these generic types for more information.Request Lifecycle & Error Handling
The request lifecycle creates many different points of failure, and provides users with a robust error model to determin what went wrong and where
- The request is created. Possible failures inlcude an invalid URL, or a failure to encode the
RequestBody
, if one is present - The request is excecuted. Possible failures include a broken network connection, a missing host, or a timeout.
- The request is parsed. The only possible failure here would be an attempt to decode the response data
- The request is validated. The provided
ResponseValidator
examines the response’s headers, status code, and body and choose to manipulate it or throw and error. See the “Validation step below for more information.
When the manager encounters any of the errors describer above, you can choose to retry to request. The number of retries is specified as a paramater in the
makeRequest<T, S>(_:retries:sla:on:fallback:)
method. You can also specify an SLA by which all retries must complete, and an optional fallback response to use. The request object itself can also contain a fallback response.Additional Headers
In addition to the headers specified in the
Requestable
, you can have the manager inject its own headers on every request it makes. This can be useful for handling things like authentication.let manager = RequestManager(host: "https://www.apple.com") manager.additionalHeaders = [.authorization, .authorization(username: "myusername", password: "mypassword"] // these headers will be injected into every request
Default Headers
RequestManager
automatically injectsUser-Agent
,Accept-Encoding
, andAccept-Language
headers into every request it makes by default. You can disable this behavior by setting theshouldInjectDefaultHeaders
property tofalse
Logging
See moreRequestManager
support logging via Apple Unified Logging To enable this, use one of the initializers that accept a log subsystem or anOSLog
instanceDeclaration
Swift
open class RequestManager
- The request is created. Possible failures inlcude an invalid URL, or a failure to encode the