ServiceRequest
public protocol ServiceRequest
A protocol for requests to be made on a Service
.
Use this protocol to define requests, which can be any of the HTTPRequestMethod
types. The ServiceRequest.httpMethod
is the only property that you must provide a value for- all others provide a default implementation.
Requests can be customized with:
- Grouping in an Endpoint - see
ServiceRequest.endpoint
- Path components - see
ServiceRequest.pathComponents
. - Query parameters - see
ServiceRequest.queryParameters
. - Headers - see
ServiceRequest.headers
. - Content type (this value will be added to the
URLRequest.allHTTPHeaders
field) - seeServiceRequest.contentType
. - Request body - see
ServiceRequest.body
.
To make a request, simply call the request()
method on the Service
. There are three versions of this method:
- A closure based method which executes the closure on completion of the request -
Service.request(_:session:autoResumeTask:completion:)
. - An asynchronous throwing method using Swift concurrency (Available when using Swift 5.5 or greater on Linux, iOS 13+, watchOS 6+, macOS10.15+, tvOS 13+) -
Service.request(_:session:)
. - A method which returns a Combine publisher (available on iOS 13+, watchOS 6+, tvOS 13+, macOS 10.15) -
Service.requestPublisher(_:session:)
.
-
endpoint
Default implementationThe endpoint this request is associated with
See also
Endpoint.path
Default Implementation
Declaration
Swift
var endpoint: Endpoint.Type? { get }
-
The type of request
Declaration
Swift
var httpMethod: HTTPRequestMethod { get }
-
pathComponents
Default implementationPath components of the request will be appended to the base URL.
Array elements are separated by a
/
in the final request URL. Defaults to an empty array (no parameters).Note
If an
endpoint
is specified, theEndpoint.path
value will be automatically set as the first path item before any values specified here. In this case, do not add the endpoint path to thepathComponents
or it will be duplicated.See also
Default Implementation
No path components (only the
Service.baseURL
will be used).Declaration
Swift
var pathComponents: [String] { get }
-
queryParameters
Default implementationQuery parameters of the request. Default is an empty array (no parameters)
Default Implementation
No query parameters
Declaration
Swift
var queryParameters: [URLQueryItem] { get }
-
headers
Default implementationHTTP headers of the request. Default is an empty array (no headers)
Note
Do not setContent-Type
header field values here; instead use thecontentType
property. The contents of that property will be added to theURLRequest.allHTTPHeaderFields
property.Default Implementation
An empty dictionary
Declaration
Swift
var headers: [String : String] { get }
-
contentType
Default implementationThe content type of the request. The default is
application/json
.Note
This value is added as an HTTP header on the URLRequest.Default Implementation
Returns
RequestContentType.applicationJSON
Declaration
Swift
var contentType: RequestContentType? { get }
-
body
Default implementationBody of the request
Default Implementation
Returns
nil
.Declaration
Swift
var body: Data? { get }