RequestManager
open class RequestManagerIntroduction
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 a RequestResponse and  a RequestError.
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 ResponseValidatorexamines 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 injects User-Agent, Accept-Encoding, and Accept-Language headers into every request it makes by default.
You can disable this behavior by setting the shouldInjectDefaultHeaders property to false
Logging
RequestManager support logging via Apple Unified Logging
To enable this, use one of the initializers that accept a log subsystem or an OSLog instance
- 
                  
                  Create a RequestManagerDeclarationSwift public convenience init(host: String)ParametershostThe host to send requests 
- 
                  
                  Create a RequestManagerusing the default logDeclarationSwift public convenience init(host: String, subsystem: String)ParametershostThe host to send requests subsystemThe subsystem to send logs 
- 
                  
                  Create a RequestManagerusing a custom logDeclarationSwift public convenience init(host: String, log: OSLog)ParametershostThe host to send requests logThe log 
- 
                  
                  The host DeclarationSwift public let host: String
- 
                  
                  The log to use DeclarationSwift public let log: OSLog?
- 
                  
                  Headers to add to every request DeclarationSwift open var additionalHeaders: RequestHeaders
- 
                  
                  Undocumented DeclarationSwift open var requestAuthentication: RequestAuthentication?
- 
                  
                  Whether or not to inject Ombi’s default headers DeclarationSwift open var shouldInjectDefaultHeaders: Bool
- 
                  
                  Make a request DeclarationSwift public final func makeRequest<T, S>(_ requestable: T, authentication: RequestAuthentication? = nil, retries: Int = 0, sla: S.SchedulerTimeType.Stride = .seconds(180.0), on scheduler: S, fallback: T.Response? = nil) -> AnyPublisher<T.Response, T.Failure> where T: Requestable, S: SchedulerParametersrequestableThe Requestableto requestretriesThe number of retries slaThe SLA execute all retries before timing out schedulerThe scheduler to use Return ValueA Publisherto observe request responses or errors
- 
                  
                  Make a request DeclarationSwift public final func makeRequest<T>(_ requestable: T, authentication: RequestAuthentication? = nil, retries: Int = 0, sla: TimeInterval = 180.0, fallback: T.Response? = nil) -> AnyPublisher<T.Response, T.Failure> where T: RequestableParametersrequestableThe Requestableto requestretriesThe number of retries slaThe SLA to use before timing out Return ValueA Publisherto observe request responses or errors
 View on GitHub
View on GitHub Install in Dash
Install in Dash RequestManager Class Reference
        RequestManager Class Reference