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 more

    Declaration

    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:) or makePOST(url:json:) factory methods instead.

    Declaration

    Swift

    public init(url: URLConvertible, method: HTTPMethod? = nil)

    Parameters

    url

    the destination URL or URL-convertible value.

    method

    the 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) -> RequestBuilder

    Parameters

    url

    the URLConvertible used to build the destination URL.

    json

    the DataObject to send as a JSON body.

    Return Value

    a RequestBuilder configured for the POST request.

  • Creates a builder for a GET request.

    Declaration

    Swift

    public static func makeGET(url: URLConvertible, etag: String? = nil) -> RequestBuilder

    Parameters

    url

    the URLConvertible used to build the destination URL.

    etag

    an optional etag added as the If-None-Match header to avoid fetching a cached resource.

    Return Value

    a RequestBuilder configured for the GET request.

  • Enables or disables gzip compression of the request body.

    When enabled, build() compresses the body and sets the Content-Encoding: gzip header. If compression fails, the request falls back to the uncompressed body without that header.

    Declaration

    Swift

    @discardableResult
    public func gzip(_ shouldGzip: Bool = true) -> RequestBuilder

    Parameters

    shouldGzip

    true to compress the body (the default), false to 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]?) -> RequestBuilder

    Parameters

    additionalHeaders

    a 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) -> Self

    Parameters

    value

    the value to set, or nil to remove the header.

    field

    the 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 -> URLRequest

    Return Value

    the built URLRequest.

  • Declaration

    Swift

    public var description: String { get }