HTTPTransaction

open class HTTPTransaction<HTTPResponseDataType>: DataTransaction

A DataTransaction that uses a URLRequest to request data from (and potentially send data to) an HTTP(S)-based service.

A successful transaction produces an instance of ResponseDataType.

  • The CacheStorageHook that will be passed the ResponseDataType when a transaction completes successfully.

    Declaration

    Swift

    public var storeInCache: CacheStorageHook?
  • The signature of a function to be called upon successful completion of a transaction, to allow cache storage of the transaction response.

    Declaration

    Swift

    public typealias CacheStorageHook = (HTTPTransaction<ResponseDataType>, ResponseDataType, HTTPResponseMetadata) -> Void
  • The signature of a function used to configure the URLRequest prior to issuing the transaction.

    Declaration

    Swift

    public typealias RequestConfigurator = (HTTPTransaction<ResponseDataType>, inout URLRequest) throws -> Void
  • The signature of a function used to validate the response received by an HTTP transaction.

    Declaration

    Swift

    public typealias ResponseValidator = (HTTPTransaction<ResponseDataType>, HTTPURLResponse, HTTPResponseMetadata, Data) throws -> Void
  • The signature of a function used to construct URL instances for the transaction.

    Declaration

    Swift

    public typealias URLConstructor = (HTTPTransaction<ResponseDataType>) throws -> URL
  • If the payload processor succeeds, the results are passed to the payload validator, giving the transaction one final chance to sanity-check the data and bail if there’s a problem.

    Declaration

    Swift

    public typealias PayloadValidator = (HTTPTransaction<ResponseDataType>, ResponseDataType, Data, HTTPResponseMetadata) throws -> Void
  • The signature of a function used to construct URLRequests for the transaction.

    Declaration

    Swift

    public typealias RequestConstructor = (HTTPTransaction<ResponseDataType>, URL) throws -> URLRequest
  • The signature of a payload processing function. This function accepts binary Data and attempts to convert it to ResponseDataType.

    Declaration

    Swift

    public typealias PayloadProcessor = (HTTPTransaction<ResponseDataType>, Data, HTTPResponseMetadata) throws -> ResponseDataType
  • Indicates the type of transaction provided by the implementation.

    See more

    Declaration

    Swift

    public enum TransactionType
  • url

    The URL of the service to be used by the transaction.

    Declaration

    Swift

    public let url: URL
  • The HTTP request method to use for the transaction.

    Declaration

    Swift

    public let method: HTTPRequestMethod
  • Optional data to send to the service when executing the transaction.

    Declaration

    Swift

    public let uploadData: Data?
  • The MIME type of the uploadData (if any). If present, this value is sent as HTTP request’s Content-Type header.

    Declaration

    Swift

    public let contentType: MIMEType?
  • Indicates the type of transaction provided by the receiver.

    Declaration

    Swift

    public let transactionType: TransactionType
  • A function called to construct the URL used for the transaction. The default implementation simply returns the value of the transaction’s url property.

    Declaration

    Swift

    public var constructURL: URLConstructor = { txn in
  • A function called to construct the URLRequest used to execute the transaction. The default implementation returns a simple URLRequest constructed from the passed-in URL.

    Declaration

    Swift

    public var constructRequest: RequestConstructor = { _, url in
  • A function called to configure the URLRequest prior to executing the transaction. The default implementation does nothing.

    Declaration

    Swift

    public var configureRequest: RequestConfigurator = { _, _ in }
  • The PayloadProcessor that will be used to produce the receiver’s ResponseDataType upon successful completion of the transaction.

    Declaration

    Swift

    public var processPayload: PayloadProcessor = { txn, data, _ in
  • The PayloadValidator used to validate the ResponseDataType produced by the PayloadProcessor upon successful completion of the transaction.

    Declaration

    Swift

    public var validatePayload: PayloadValidator = { _, _, _, _ in }
  • The URLSessionConfiguration used to create the URLSession for the transaction.

    Declaration

    Swift

    public var sessionConfiguration: URLSessionConfiguration = .default
  • A ResponseValidator function used to validate the HTTP response received when executing a transaction.

    Declaration

    Swift

    public var validateResponse: ResponseValidator = { _, resp, meta, data in
  • Initializes a new transaction that will connect to the given service.

    Declaration

    Swift

    public init(url: URL, method: HTTPRequestMethod? = nil, upload data: Data? = nil, contentType: MIMEType? = nil, transactionType: TransactionType = .api, processingQueue queue: DispatchQueue = .transactionProcessing)
  • Initializes a new transaction that will connect to the given service.

    Declaration

    Swift

    open func cancel()
  • Configures the URLRequest.

    Note

    Subclasses overriding this function should call super.

    Declaration

    Swift

    open func configure(request: inout URLRequest)

    Parameters

    request

    The URLRequest to configure.

  • Configures the URLRequest.

    Note

    Subclasses overriding this function should call super.

    Declaration

    Swift

    open func executeTransaction(completion: @escaping Callback)

    Parameters

    completion

    A function that will be called upon completion of the transaction.

  • Declaration

    Swift

    open func transactionCompleted(_ result: Result)

    Parameters

    result

    The instance of ResponseDataType that resulted from the transaction.