AnyRequest

public struct AnyRequest<S> : Request where S : HttpService

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).

Types

  • Declaration

    Swift

    public typealias Result = Data

Properties

Initialization

  • 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(),
                url: UrlConvertible? = nil,
                acceptedStatusCodes: CountableClosedRange<Int> = 200...299,
                priority: RequestPriority = .default,
                service: S)

    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.

Instance Methods

  • 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 the schedule(with:) method.

    Declaration

    Swift

    public func schedule() -> Response<`Self`, S>

Available where S == AnyHttpService

  • 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.