RequestBuilder
public class RequestBuilder
extension RequestBuilder: CustomStringConvertible
A builder for URLRequests to send through a NetworkClient.
Use the makePOST(url:json:) / makeGET(url:etag:) factories to start a request, chain the
builder methods to configure it, and hand the builder to NetworkClient.sendRequest(_:completion:)
(which builds it for you). Request bodies are uncompressed by default; opt into gzip
compression with gzip().
-
HTTP methods supported by RequestBuilder.
See moreDeclaration
Swift
public enum HTTPMethod : String -
Creates a builder for a custom HTTP request.
For common GET and POST requests, consider using the
makeGET(url:etag:)ormakePOST(url:json:)factory methods instead.Declaration
Swift
public init(url: URLConvertible, method: HTTPMethod? = nil)Parameters
urlthe destination URL or URL-convertible value.
methodthe HTTP method (defaults to GET if nil).
-
Creates a builder for a POST request with a JSON body.
The body is uncompressed by default. Call
gzip()on the returned builder to opt into gzip compression (e.g. for endpoints that support it).Declaration
Swift
public static func makePOST(url: URLConvertible, json: DataObject) -> RequestBuilderParameters
urlthe
URLConvertibleused to build the destinationURL.jsonthe
DataObjectto send as a JSON body.Return Value
a
RequestBuilderconfigured for the POST request. -
Creates a builder for a GET request.
Declaration
Swift
public static func makeGET(url: URLConvertible, etag: String? = nil) -> RequestBuilderParameters
urlthe
URLConvertibleused to build the destinationURL.etagan optional etag added as the
If-None-Matchheader to avoid fetching a cached resource.Return Value
a
RequestBuilderconfigured for the GET request. -
Enables or disables gzip compression of the request body.
When enabled,
build()compresses the body and sets theContent-Encoding: gzipheader. If compression fails, the request falls back to the uncompressed body without that header.Declaration
Swift
@discardableResult public func gzip(_ shouldGzip: Bool = true) -> RequestBuilderParameters
shouldGziptrueto compress the body (the default),falseto leave it uncompressed.Return Value
this builder, to allow chaining.
-
Adds the given headers to the request.
Declaration
Swift
@discardableResult public func additionalHeaders(_ additionalHeaders: [String : String]?) -> RequestBuilderParameters
additionalHeadersa dictionary of header fields and values to add, if any.
Return Value
this builder, to allow chaining.
-
Sets the value for a single header field.
Declaration
Swift
@discardableResult public func header(_ value: String?, forField field: String) -> SelfParameters
valuethe value to set, or
nilto remove the header.fieldthe header field name.
Return Value
this builder, to allow chaining.
-
Builds the configured
URLRequest.Throws
an error if the URL is malformed or the body cannot be encoded.Declaration
Swift
public func build() throws -> URLRequestReturn Value
the built
URLRequest. -
Declaration
Swift
public var description: String { get }
View on GitHub