Request
public final class Request : Operation
extension Request: NSCopying
The core class of TermiNetwork. It handles the request creation and its execution.
-
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 }
-
Initializes a Request.
Declaration
Swift
public init(method: Method, url: String, headers: [String: String]? = nil, params: [String: Any?]? = nil, configuration: Configuration? = nil)Parameters
methodA Method to use, for example: .get, .post, etc.
urlThe URL of the request.
headersA Dictionary of header values, etc. “Content-type”: “text/html”
paramsThe parameters of the request. (optional)
configurationA 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
endpointa EndpointProtocol enum value.
environmentSpecifies a different environment to use than the global setted environment.
-
Converts a Request instance an URLRequest instance.
Declaration
Swift
public func asRequest() throws -> URLRequest -
Cancels a request
Declaration
Swift
public override func cancel()
-
Set the queue in which the request will be executed.
Declaration
Swift
public func queue(_ queue: Queue) -> RequestParameters
queueA Queue object.
Return Value
A Request instance.
-
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()
-
Executed when the request is failed.
Declaration
Swift
@discardableResult public func failure(responseHandler: @escaping FailureCallbackWithoutType) -> RequestParameters
responseHandlerThe completion handler with the error.
Return Value
The Request object.
-
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>) -> RequestParameters
responseTypeThe type of the model that will be deserialized and will be passed to the success block.
responseHandlerThe 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>) -> RequestParameters
responseTypeThe type of the model that will be deserialized and will be passed to the success block.
responseHandlerThe completion handler that provides the deserialized object and the error.
Return Value
The Request object.
-
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>) -> RequestParameters
transformerThe transformer type that handles the transformation.
responseHandlerThe 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>) -> RequestParameters
transformerThe transformer type that handles the transformation.
responseHandlerThe completion handler with the deserialized object as ToType.
Return Value
The Request object.
-
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>) -> RequestParameters
responseTypeOne of UIImage or NSImage or types.
responseHandlerThe completion handler with the Image object.
Return Value
The Request object.
-
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>) -> RequestParameters
responseTypeA type of String.
responseHandlerThe 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>) -> RequestParameters
responseTypeA type of String.
responseHandlerThe completion handler with the String object if possible, nil otherwise.
Return Value
The Request object.
-
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>) -> RequestParameters
responseTypeA type of Data.
responseHandlerThe 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>) -> RequestParameters
responseTypeA type of String.
responseHandlerThe completion handler with the Data object if possible, nil otherwise.
Return Value
The Request object.
-
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>) -> RequestParameters
responseTypeThe type of the model that will be deserialized and will be passed to the success block.
progressUpdatespecifies a progress callback to get upload progress updates.
responseHandlerThe completion handler with the deserialized object
Return Value
The Request object.
-
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>) -> RequestParameters
transformerThe transformer type that handles the transformation.
progressUpdatespecifies a progress callback to get upload progress updates.
responseHandlerThe completion handler with the deserialized object as ToType.
Return Value
The Request object.
-
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) -> RequestParameters
responseTypeA type of String.
progressUpdatespecifies a progress callback to get upload progress updates.
responseHandlerThe completion handler with the String object.
Return Value
The Request object.
-
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>) -> RequestParameters
responseTypeA type of Data.
progressUpdatespecifies a progress callback to get upload progress updates.
responseHandlerThe completion handler with the Data object.
Return Value
The Request object.
-
Executed when the download request is succeeded.
Declaration
Swift
@discardableResult func download(destinationPath: String, progressUpdate: ProgressCallbackType?, completionHandler: @escaping SuccessCallbackWithoutType) -> RequestParameters
destinationPathThe destination file path to save the file.
progressUpdatespecifies a progress callback to get upload progress updates.
completionHandlerThe completion handler with the Data object.
Return Value
The Request object.
-
asyncUpload(as:AsynchronousprogressUpdate: ) 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 -> TParameters
responseTypeThe type of the model that will be deserialized and will be passed to the success block.
progressUpdatespecifies a progress callback to get upload progress updates.
Return Value
The decodable type.
-
asyncUpload(using:AsynchronousprogressUpdate: ) 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 -> ToParameters
usingThe transformer type.
progressUpdatespecifies a progress callback to get upload progress updates.
Return Value
The decoded type based on the transformer.
-
asyncUplaod(as:AsynchronousprogressUpdate: ) 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 -> StringParameters
asA type of String.
progressUpdatespecifies a progress callback to get upload progress updates.
Return Value
The String response.
-
asyncUpload(as:AsynchronousprogressUpdate: ) 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 -> DataParameters
responseTypeA type of Data.
progressUpdatespecifies a progress callback to get upload progress updates.
Return Value
The Data response.
-
asyncDownload(destinationPath:AsynchronousprogressUpdate: ) 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 throwsParameters
destinationPathThe destination file path to save the file.
progressUpdatespecifies 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 : DecodableParameters
asThe decodable type that will be deserialized.
Return Value
The inferred decodable type.
-
async()AsynchronousExecutes 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 : DecodableReturn 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 -> StringParameters
asThe 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 -> DataParameters
asThe Data type (Data.self).
Return Value
The response data as string.
-
async(as:Asynchronous) -
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 : DecodableParameters
usingThe transformer type.
Return Value
The response data as string.
-
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) -> SelfParameters
headersCallbackA closure that provides the response headers or an error.
View on GitHub
Install in Dash
Request Class Reference