AnyRequest
public struct AnyRequest : Request
This struct can be used to construct requests easily without having to define a custom entity
conforming to the Request
protocol and defining some HttpService
. In a larger application,
you should refrain from using this struct, however, it might be useful in small projects and
when writing tests.
This request fixes the result type to Data
such that the user can decode to the desired type
via calling the decode(type:decoder:)
function on the returned Response
publisher when
scheduling the request.
As this request struct abstracts away the HttpService
in favor for a simpler interface,
scheduling can be performed even easier.
Note that this entity does not allow to make insecure requests over HTTP (only HTTPS).
-
Declaration
Swift
public typealias Result = Data
-
Declaration
Swift
public let routes: HttpRoute
-
Declaration
Swift
public let method: HttpMethod
-
Declaration
Swift
public let query: HttpQuery
-
Declaration
Swift
public let header: HttpHeader
-
Declaration
Swift
public let body: HttpBody
-
Declaration
Swift
public let acceptedStatusCodes: CountableClosedRange<Int>
-
Declaration
Swift
public let priority: RequestPriority
-
Initializes a new request for a particular URL.
Declaration
Swift
public init(_ method: HttpMethod = .get, url: UrlConvertible, query: HttpQuery = [:], header: HttpHeader = [:], body: HttpBody = HttpData.Empty(), acceptedStatusCodes: CountableClosedRange<Int> = 200...299, priority: RequestPriority = .default)
Parameters
method
The HTTP method for the request. Defaults to GET.
url
The URL of the request.
query
The request’s query parameters. Defaults to no parameters.
header
The request’s headers. Defaults to no header fields.
body
The request’s body. Defaults to an empty body.
acceptedStatusCodes
Acceptable status codes for a successful response. Defaults to all 2xx status codes.
priority
The priority of the request. Defaults to
.default
. -
Initializes a new request based on a predefined
HttpService
.Declaration
Swift
public init(_ method: HttpMethod = .get, routes: HttpRoute, query: HttpQuery = [:], header: HttpHeader = [:], body: HttpBody = HttpData.Empty(), acceptedStatusCodes: CountableClosedRange<Int> = 200...299, priority: RequestPriority = .default, service: HttpService)
Parameters
method
The HTTP method for the request. Defaults to GET.
routes
The routing paths for the request. The final URL is constructed by making use of the given
service
.query
The request’s query parameters. Defaults to no parameters.
header
The request’s headers. Defaults to no header fields.
body
The request’s body. Defaults to an empty body.
acceptedStatusCodes
Acceptable status codes for a successful response. Defaults to all 2xx status codes.
priority
The priority of the request. Defaults to
.default
.service
The service representing an API.
-
Schedules the request and, as expected, returns a
Response
publisher. As the service is transparently constructed when initializing the request, there is no need to pass a service in this case. This implies that the user should not use theschedule(with:)
method.Declaration
Swift
public func schedule() -> Response<`Self`>