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
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(route: RouteProtocol, environment: Environment? = Environment.current)
Parameters
route
a RouteProtocol enum value
environment
Specifies 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) -> Request
Parameters
queue
A 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()
-
Adds a request to a queue and starts a download process for Decodable types.
Declaration
Swift
@available(*, deprecated, message: "Use success(responseType:﹚ and failure(responseType:error:﹚ methods instead.") @discardableResult public func start<T: Decodable>(queue: Queue? = Queue.shared, responseType: T.Type, onSuccess: SuccessCallback<T>?, onFailure: FailureCallback? = nil) -> Request
Parameters
queue
A Queue instance. If no queue is specified it uses the default one.
responseType
The type of the model that will be deserialized and will be passed to the success block.
onSuccess
specifies a success callback of type SuccessCallback
. onFailure
specifies a failure callback of type FailureCallback
. Return Value
The Request object.
-
Adds a request to a queue and starts its execution for Transformer types.
Declaration
Swift
@available(*, deprecated, message: "Use success(responseType:﹚ and failure(responseType:error:﹚ methods instead.") @discardableResult public func start<FromType: Decodable, ToType>(queue: Queue? = Queue.shared, transformer: Transformer<FromType, ToType>.Type, onSuccess: SuccessCallback<ToType>?, onFailure: FailureCallback? = nil) -> Request
Parameters
queue
A Queue instance. If no queue is specified it uses the default one.
transformer
The transformer object that handles the transformation.
onSuccess
specifies a success callback of type SuccessCallback
. onFailure
specifies a failure callback of type FailureCallback
. Return Value
The Request object.
-
Adds a request to a queue and starts its execution for UIImage|NSImage responses.
Declaration
Swift
@available(*, deprecated, message: "Use success(responseType:﹚ and failure(responseType:error:﹚ methods instead.") @discardableResult public func start<T: ImageType>(queue: Queue? = Queue.shared, responseType: T.Type, onSuccess: SuccessCallback<T>?, onFailure: FailureCallback? = nil) -> Request
Parameters
queue
A Queue instance. If no queue is specified it uses the default one.
responseType
The response type is UIImage.self or NSImage.self.
onSuccess
specifies a success callback of type SuccessCallback
. onFailure
specifies a failure callback of type FailureCallback
. Return Value
The Request object.
-
Adds a request to a queue and starts its execution for String responses.
Declaration
Swift
@available(*, deprecated, message: "Use success(responseType:﹚ and failure(responseType:error:﹚ methods instead.") @discardableResult public func start(queue: Queue? = Queue.shared, responseType: String.Type, onSuccess: SuccessCallback<String>?, onFailure: FailureCallback? = nil) -> Request
Parameters
queue
A Queue instance. If no queue is specified it uses the default one.
responseType
The response type is String.self.
onSuccess
specifies a success callback of type SuccessCallback
onFailure
specifies a failure callback of type FailureCallback
-
Adds a request to a queue and starts its execution for Data responses.
Declaration
Swift
@available(*, deprecated, message: "Use success(responseType:﹚ and failure(responseType:error:﹚ methods instead.") @discardableResult public func start(queue: Queue? = Queue.shared, responseType: Data.Type, onSuccess: SuccessCallback<Data>?, onFailure: FailureCallback? = nil) -> Request
Parameters
queue
A Queue instance. If no queue is specified it uses the default one.
responseType
The response type is Data.self.
onSuccess
specifies a success callback of type SuccessCallback
onFailure
specifies a failure callback of type FailureCallback
-
Starts a request without callbacks.
Declaration
Parameters
queue
A Queue instance. If no queue is specified it uses the default one.
-
Adds a request to a queue and starts an upload process for Data types.
Declaration
Swift
@available(*, deprecated, message: "Use upload(responseType:﹚ and failure(responseType:error:﹚ methods instead.") @discardableResult public func startUpload(queue: Queue? = Queue.shared, responseType: Data.Type, progressUpdate: ProgressCallbackType?, onSuccess: SuccessCallback<Data>?, onFailure: FailureCallback? = nil) -> Request
Parameters
queue
A Queue instance. If no queue is specified it uses the default one.
responseType
The type of the model that will be deserialized and will be passed to the success block.
progress
specifies a progress callback to get upload progress updates.
onSuccess
specifies a success callback of type SuccessCallback
. onFailure
specifies a failure callback of type FailureCallback
. Return Value
The Request object.
-
Adds a request to a queue and starts an upload process for Decodable types.
Declaration
Swift
@available(*, deprecated, message: "Use upload(responseType:﹚ and failure(responseType:error:﹚ methods instead.") @discardableResult public func startUpload<T: Decodable>(queue: Queue? = Queue.shared, responseType: T.Type, progressUpdate: ProgressCallbackType?, onSuccess: SuccessCallback<T>?, onFailure: FailureCallback? = nil) -> Request
Parameters
queue
A Queue instance. If no queue is specified it uses the default one.
responseType
The type of the model that will be deserialized and will be passed to the success block.
progress
specifies a progress callback to get upload progress updates.
onSuccess
specifies a success callback of type SuccessCallback
. onFailure
specifies a failure callback of type FailureCallback
. Return Value
The Request object.
-
Adds a request to a queue and starts its execution for Transformer types.
Declaration
Swift
@available(*, deprecated, message: "Use upload(responseType:﹚ and failure(responseType:error:﹚ methods instead.") @discardableResult public func startUpload<FromType: Decodable, ToType>(queue: Queue? = Queue.shared, transformer: Transformer<FromType, ToType>.Type, progressUpdate: ProgressCallbackType?, onSuccess: SuccessCallback<ToType>?, onFailure: FailureCallback? = nil) -> Request
Parameters
queue
A Queue instance. If no queue is specified it uses the default one.
transformer
The transformer object that handles the transformation.
progressUpdate
specifies a progress callback to get upload progress updates.
onSuccess
specifies a success callback of type SuccessCallback
. onFailure
specifies a failure callback of type FailureCallback
. Return Value
The Request object.
-
Adds a request to a queue and starts its execution for String types.
Declaration
Swift
@available(*, deprecated, message: "Use download(responseType:﹚ and failure(responseType:error:﹚ methods instead.") @discardableResult public func startDownload(queue: Queue? = Queue.shared, filePath: String, progressUpdate: ProgressCallbackType?, onSuccess: DownloadSuccessCallback?, onFailure: FailureCallback? = nil) -> Request
Parameters
queue
A Queue instance. If no queue is specified it uses the default one.
filePath
The destination file path to save the file.
responseType
The type of the model that will be deserialized and will be passed to the success block.
progress
specifies a progress callback to get upload progress updates.
onSuccess
specifies a success callback of type SuccessCallback
. onFailure
specifies a failure callback of type FailureCallback
. Return Value
The Request object.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
Executed when the upload request is succeeded and the response has successfully deserialized.
Declaration
Swift
@discardableResult public 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.
-
Executed when the request is succeeded and the response has successfully transformed.
Declaration
Swift
@discardableResult public 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.
-
Executed when the request is succeeded and the response is String type.
Declaration
Swift
@discardableResult public 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.
-
Executed when the request is succeeded and the response is Data type.
Declaration
Swift
@discardableResult public 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.
-
Executed when the download request is succeeded.
Declaration
Swift
@discardableResult public func download(filePath: String, progressUpdate: ProgressCallbackType?, completionHandler: @escaping SuccessCallbackWithoutType) -> Request
Parameters
filePath
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.
-
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
func responseHeaders(_ headersCallback: @escaping ([String : String]?, TNError?) -> Void)
Parameters
headersCallback
A closure that provides the response headers or an error.