Request

public final class Request : Operation
extension Request: NSCopying

The core class of TermiNetwork. It handles the request creation and its execution.

Public properties

  • The configuration of the request. This will be merged with the environment configuration if needed.

    Declaration

    Swift

    public var configuration: Configuration
  • The number of the retries initiated by interceptor.

    Declaration

    Swift

    public var retryCount: Int
  • The environment of the request.

    Declaration

    Swift

    public var environment: Environment?
  • An associated object with the request. Use this variable to optionaly assign an object to it, for later use.

    Declaration

    Swift

    public var associatedObject: AnyObject?
  • The headers of the request.

    Declaration

    Swift

    public var headers: [String : String]?
  • The parameters of the request.

    Declaration

    Swift

    public var params: [String : Any?]?
  • The random delay for mocked responses that is generated by TermiNetwork (readonly)

    Declaration

    Swift

    public internal(set) var mockDelay: TimeInterval? { get }

Initializers

  • Initializes a Request.

    Declaration

    Swift

    public init(method: Method,
                url: String,
                headers: [String: String]? = nil,
                params: [String: Any?]? = nil,
                configuration: Configuration? = nil)

    Parameters

    method

    A Method to use, for example: .get, .post, etc.

    url

    The URL of the request.

    headers

    A Dictionary of header values, etc. “Content-type”: “text/html”

    params

    The parameters of the request. (optional)

    configuration

    A configuration object (optional, e.g. if you want ot use custom configuration for the request).

  • Initializes a Request.

    Declaration

    Swift

    public convenience init(endpoint: EndpointProtocol,
                            environment: Environment? = Environment.current)

    Parameters

    endpoint

    a EndpointProtocol enum value.

    environment

    Specifies a different environment to use than the global setted environment.

Public methods

  • Converts a Request instance an URLRequest instance.

    Declaration

    Swift

    public func asRequest() throws -> URLRequest
  • Cancels a request

    Declaration

    Swift

    public override func cancel()

Queue

  • Set the queue in which the request will be executed.

    Declaration

    Swift

    public func queue(_ queue: Queue) -> Request

    Parameters

    queue

    A Queue object.

    Return Value

    A Request instance.

Operation

  • Overrides the start() function from Operation class. You should never call this function directly. If you want to start a request without callbacks use startEmpty() instead.

    Declaration

    Swift

    public override func start()

Empty

  • Executed when the request is failed.

    Declaration

    Swift

    @discardableResult
    public func failure(responseHandler: @escaping FailureCallbackWithoutType) -> Request

    Parameters

    responseHandler

    The completion handler with the error.

    Return Value

    The Request object.

Decodable

  • Executed when the request is succeeded and the response has successfully deserialized.

    Declaration

    Swift

    @discardableResult
    public func success<T: Decodable>(responseType: T.Type,
                                      responseHandler: @escaping SuccessCallback<T>) -> Request

    Parameters

    responseType

    The type of the model that will be deserialized and will be passed to the success block.

    responseHandler

    The completion handler with the deserialized object

    Return Value

    The Request object.

  • Executed when the request is failed. The response is being deserialized if possible, nil otherwise.

    Declaration

    Swift

    @discardableResult
    public func failure<T: Decodable>(responseType: T.Type,
                                      responseHandler: @escaping FailureCallbackWithType<T>) -> Request

    Parameters

    responseType

    The type of the model that will be deserialized and will be passed to the success block.

    responseHandler

    The completion handler that provides the deserialized object and the error.

    Return Value

    The Request object.

Transformer

  • Executed when the request is succeeded and the response has successfully transformed.

    Declaration

    Swift

    @discardableResult
    public func success<FromType: Decodable, ToType>(transformer: Transformer<FromType, ToType>.Type,
                                                     responseHandler: @escaping SuccessCallback<ToType>)
                                            -> Request

    Parameters

    transformer

    The transformer type that handles the transformation.

    responseHandler

    The completion handler with the deserialized object as ToType.

    Return Value

    The Request object.

  • Executed when the request is failed. The response is being transformed to ToType if possible, nil otherwise.

    Declaration

    Swift

    @discardableResult
    public func failure<FromType: Decodable, ToType>(transformer: Transformer<FromType, ToType>.Type,
                                                     responseHandler: @escaping FailureCallbackWithType<ToType>)
                                            -> Request

    Parameters

    transformer

    The transformer type that handles the transformation.

    responseHandler

    The completion handler with the deserialized object as ToType.

    Return Value

    The Request object.

Image

  • Executed when the request is succeeded and the response is a valid Image.

    Declaration

    Swift

    @discardableResult
    public func success(responseType: ImageType.Type,
                        responseHandler: @escaping SuccessCallback<ImageType>) -> Request

    Parameters

    responseType

    One of UIImage or NSImage or types.

    responseHandler

    The completion handler with the Image object.

    Return Value

    The Request object.

String

  • Executed when the request is succeeded and the response is a valid String.

    Declaration

    Swift

    @discardableResult
    public func success(responseType: String.Type,
                        responseHandler: @escaping SuccessCallback<String>) -> Request

    Parameters

    responseType

    A type of String.

    responseHandler

    The completion handler with the String object.

    Return Value

    The Request object.

  • Executed when the request is failed. The response is being converted to String value if possible.

    Declaration

    Swift

    @discardableResult
    public func failure(responseType: String.Type,
                        responseHandler: @escaping FailureCallbackWithType<String>) -> Request

    Parameters

    responseType

    A type of String.

    responseHandler

    The completion handler with the String object if possible, nil otherwise.

    Return Value

    The Request object.

Data

  • Executed when the request is succeeded and the response is Data type.

    Declaration

    Swift

    @discardableResult
    public func success(responseType: Data.Type,
                        responseHandler: @escaping SuccessCallback<Data>) -> Request

    Parameters

    responseType

    A type of Data.

    responseHandler

    The completion handler with the Data object.

    Return Value

    The Request object.

  • Executed when the request is failed. The response is being converted to Data value if possible.

    Declaration

    Swift

    @discardableResult
    public func failure(responseType: Data.Type,
                        responseHandler: @escaping FailureCallbackWithType<Data>) -> Request

    Parameters

    responseType

    A type of String.

    responseHandler

    The completion handler with the Data object if possible, nil otherwise.

    Return Value

    The Request object.

Upload - Decodable

  • Executed when the upload request is succeeded and the response has successfully deserialized.

    Declaration

    Swift

    @discardableResult
    func upload<T: Decodable>(responseType: T.Type,
                              progressUpdate: ProgressCallbackType?,
                              responseHandler: @escaping SuccessCallback<T>) -> Request

    Parameters

    responseType

    The type of the model that will be deserialized and will be passed to the success block.

    progressUpdate

    specifies a progress callback to get upload progress updates.

    responseHandler

    The completion handler with the deserialized object

    Return Value

    The Request object.

Upload - Transformers

  • Executed when the request is succeeded and the response has successfully transformed.

    Declaration

    Swift

    @discardableResult
    func upload<FromType: Decodable, ToType>(transformer: Transformer<FromType, ToType>.Type,
                                             progressUpdate: ProgressCallbackType?,
                                             responseHandler: @escaping SuccessCallback<ToType>)
                                            -> Request

    Parameters

    transformer

    The transformer type that handles the transformation.

    progressUpdate

    specifies a progress callback to get upload progress updates.

    responseHandler

    The completion handler with the deserialized object as ToType.

    Return Value

    The Request object.

Upload - String

  • Executed when the request is succeeded and the response is String type.

    Declaration

    Swift

    @discardableResult
    func upload(responseType: String.Type,
                progressUpdate: ProgressCallbackType?,
                responseHandler: @escaping (String) -> Void) -> Request

    Parameters

    responseType

    A type of String.

    progressUpdate

    specifies a progress callback to get upload progress updates.

    responseHandler

    The completion handler with the String object.

    Return Value

    The Request object.

Upload - Data

  • Executed when the request is succeeded and the response is Data type.

    Declaration

    Swift

    @discardableResult
    func upload(responseType: Data.Type,
                progressUpdate: ProgressCallbackType?,
                responseHandler: @escaping SuccessCallback<Data>) -> Request

    Parameters

    responseType

    A type of Data.

    progressUpdate

    specifies a progress callback to get upload progress updates.

    responseHandler

    The completion handler with the Data object.

    Return Value

    The Request object.

Download

  • Executed when the download request is succeeded.

    Declaration

    Swift

    @discardableResult
    func download(destinationPath: String,
                  progressUpdate: ProgressCallbackType?,
                  completionHandler: @escaping SuccessCallbackWithoutType) -> Request

    Parameters

    destinationPath

    The destination file path to save the file.

    progressUpdate

    specifies a progress callback to get upload progress updates.

    completionHandler

    The completion handler with the Data object.

    Return Value

    The Request object.

Upload Operations

  • Executed when the upload request is succeeded and the response has successfully deserialized.

    Declaration

    Swift

    @discardableResult
    func asyncUpload<T: Decodable>(as type: T.Type,
                                   progressUpdate: ProgressCallbackType?) async throws -> T

    Parameters

    responseType

    The type of the model that will be deserialized and will be passed to the success block.

    progressUpdate

    specifies a progress callback to get upload progress updates.

    Return Value

    The decodable type.

  • Executes an asynchronous upload request and returns the decodable type based on transformer.

    Throws

    A TNError in case of failure.

    Declaration

    Swift

    @discardableResult
    func asyncUpload<From: Decodable, To>(
        using transformer: Transformer<From, To>.Type,
        progressUpdate: ProgressCallbackType? = nil
    ) async throws -> To

    Parameters

    using

    The transformer type.

    progressUpdate

    specifies a progress callback to get upload progress updates.

    Return Value

    The decoded type based on the transformer.

  • Executed when the request is succeeded and the response is String type.

    Declaration

    Swift

    @discardableResult
    func asyncUplaod(as type: String.Type,
                     progressUpdate: ProgressCallbackType?) async throws -> String

    Parameters

    as

    A type of String.

    progressUpdate

    specifies a progress callback to get upload progress updates.

    Return Value

    The String response.

Upload - Data

  • Executed when the request is succeeded and the response is Data type.

    Declaration

    Swift

    @discardableResult
    func asyncUpload(as type: Data.Type,
                     progressUpdate: ProgressCallbackType?) async throws -> Data

    Parameters

    responseType

    A type of Data.

    progressUpdate

    specifies a progress callback to get upload progress updates.

    Return Value

    The Data response.

  • Executes an asynchronous download request and returns by throwing and error if it fails.

    Throws

    A TNError in case of failure.

    Declaration

    Swift

    func asyncDownload(
        destinationPath: String,
        progressUpdate: ProgressCallbackType? = nil
    ) async throws

    Parameters

    destinationPath

    The destination file path to save the file.

    progressUpdate

    specifies a progress callback to get upload progress updates.

    Return Value

    The response data as string.

  • async(as:) Asynchronous

    Executes an asynchronous request and returns the inferred decodable type.

    Throws

    A TNError in case of failure.

    Declaration

    Swift

    @discardableResult
    func async<T>(as type: T.Type) async throws -> T where T : Decodable

    Parameters

    as

    The decodable type that will be deserialized.

    Return Value

    The inferred decodable type.

  • async() Asynchronous

    Executes an asynchronous request and returns the inferred decodable type.

    Throws

    A TNError in case of failure.

    Declaration

    Swift

    @discardableResult
    func async<T>() async throws -> T where T : Decodable

    Return Value

    The inferred decodable type.

  • async(as:) Asynchronous

    Executes an asynchronous request and returns the data as String.

    Throws

    A TNError in case of failure.

    Declaration

    Swift

    @discardableResult
    func async(as type: String.Type) async throws -> String

    Parameters

    as

    The String type (String.self).

    Return Value

    The response data as string.

  • async(as:) Asynchronous

    Executes an asynchronous request and returns the data as Data.

    Throws

    A TNError in case of failure.

    Declaration

    Swift

    @discardableResult
    func async(as type: Data.Type) async throws -> Data

    Parameters

    as

    The Data type (Data.self).

    Return Value

    The response data as string.

  • async(as:) Asynchronous

    Executes an asynchronous request and returns the data as Image.

    Throws

    A TNError in case of failure.

    Declaration

    Swift

    @discardableResult
    func async(as type: ImageType.Type) async throws -> ImageType

    Parameters

    as

    The Image type (UImage.self/NSImage.self).

    Return Value

    The response data as string.

  • async(using:) Asynchronous

    Executes an asynchronous request and returns the decodable type based on transformer.

    Throws

    A TNError in case of failure.

    Declaration

    Swift

    @discardableResult
    func async<From, To>(using transformer: Transformer<From, To>.Type) async throws -> To where From : Decodable

    Parameters

    using

    The transformer type.

    Return Value

    The response data as string.

Helpers

  • Checks if the task has been cancelled and throws an Error in that case.

    Declaration

    Swift

    func checkTaskCancellation() throws
  • Clones a Request instance.

    Declaration

    Swift

    public func copy(with zone: NSZone? = nil) -> Any
  • Reads the response headers from request after its completion.

    Declaration

    Swift

    @discardableResult
    func responseHeaders(_ headersCallback: @escaping ([String : String]?, TNError?) -> Void) -> Self

    Parameters

    headersCallback

    A closure that provides the response headers or an error.