URLSession

extension URLSession
  • Executes a RequestConvertible request and attempts to decode the response body.

    Declaration

    Swift

    public func perform<R: RequestConvertible>(
      _ request: R,
      validateResponse validationBlock: ((HTTPURLResponse) throws -> Bool)? = nil,
      decodingQueue: DispatchQueue = .global(qos: .userInitiated),
      callbackQueue: DispatchQueue = .main,
      configureTask: (URLSessionTask) -> Void = { _ in },
      completionHandler: @escaping (NetworkResult<R.Resource>) -> Void
    )

    Parameters

    request

    A HTTP request to execute.

    validateResponse

    A block that validates the received response. This block can throw a custom error or return false to indicate an invalid response. If it returns false, the request will fail with a RequestError.unacceptableResponse error. Default is nil for no validation.

    decodingQueue

    A DispatchQueue to decode the response body on. Default .global(.userInitiated).

    callbackQueue

    A DispatchQueue to run completionHandler on. Default .main.

    configureTask

    A block that receives the URLSessionTask for the request. This block will only be called if the RequestType is successfully converted to a URLRequest. You do not need to call resume() on the provided task. The default implementation does nothing.

    completionHandler

    A block executed when the request completes and the response body has been decoded.