Request
open class Request
extension Request: CustomStringConvertible
extension Request: CustomDebugStringConvertible
Responsible for sending a request and receiving the response and associated data from the server, as well as
managing its underlying URLSessionTask
.
-
A closure executed when monitoring upload or download progress of a request.
Declaration
Swift
public typealias ProgressHandler = (Progress) -> Void
-
The delegate for the underlying task.
Declaration
Swift
open internal(set) var delegate: TaskDelegate { get set }
-
The underlying task.
Declaration
Swift
open var task: URLSessionTask? { get }
-
The session belonging to the underlying task.
Declaration
Swift
public let session: URLSession
-
The request sent or to be sent to the server.
Declaration
Swift
open var request: URLRequest? { get }
-
The response received from the server, if any.
Declaration
Swift
open var response: HTTPURLResponse? { get }
-
The number of times the request has been retried.
Declaration
Swift
open internal(set) var retryCount: UInt { get }
-
Associates an HTTP Basic credential with the request.
Declaration
Swift
@discardableResult open func authenticate( user: String, password: String, persistence: URLCredential.Persistence = .forSession) -> Self
Parameters
user
The user.
password
The password.
persistence
The URL credential persistence.
.ForSession
by default.Return Value
The request.
-
Associates a specified credential with the request.
Declaration
Swift
@discardableResult open func authenticate(usingCredential credential: URLCredential) -> Self
Parameters
credential
The credential.
Return Value
The request.
-
Returns a base64 encoded basic authentication credential as an authorization header tuple.
Declaration
Swift
open class func authorizationHeader(user: String, password: String) -> (key: String, value: String)?
Parameters
user
The user.
password
The password.
Return Value
A tuple with Authorization header and credential value if encoding succeeds,
nil
otherwise.
-
Resumes the request.
Declaration
Swift
open func resume()
-
Suspends the request.
Declaration
Swift
open func suspend()
-
Cancels the request.
Declaration
Swift
open func cancel()
-
The textual representation used when written to an output stream, which includes the HTTP method and URL, as well as the response status code if a response has been received.
Declaration
Swift
open var description: String { get }
-
The textual representation used when written to an output stream, in the form of a cURL command.
Declaration
Swift
open var debugDescription: String { get }
-
Returns a result data type that contains the response data as-is.
Declaration
Swift
public static func serializeResponseData(response: HTTPURLResponse?, data: Data?, error: Error?) -> Result<Data>
Parameters
response
The response from the server.
data
The data returned from the server.
error
The error already encountered if it exists.
Return Value
The result data type.
-
Returns a result string type initialized from the response data with the specified string encoding.
Declaration
Swift
public static func serializeResponseString( encoding: String.Encoding?, response: HTTPURLResponse?, data: Data?, error: Error?) -> Result<String>
Parameters
encoding
The string encoding. If
nil
, the string encoding will be determined from the server response, falling back to the default HTTP default character set, ISO-8859-1.response
The response from the server.
data
The data returned from the server.
error
The error already encountered if it exists.
Return Value
The result data type.
-
Returns a JSON object contained in a result type constructed from the response data using
JSONSerialization
with the specified reading options.Declaration
Swift
public static func serializeResponseJSON( options: JSONSerialization.ReadingOptions, response: HTTPURLResponse?, data: Data?, error: Error?) -> Result<Any>
Parameters
options
The JSON serialization reading options. Defaults to
.allowFragments
.response
The response from the server.
data
The data returned from the server.
error
The error already encountered if it exists.
Return Value
The result data type.
-
Returns a plist object contained in a result type constructed from the response data using
PropertyListSerialization
with the specified reading options.Declaration
Swift
public static func serializeResponsePropertyList( options: PropertyListSerialization.ReadOptions, response: HTTPURLResponse?, data: Data?, error: Error?) -> Result<Any>
Parameters
options
The property list reading options. Defaults to
[]
.response
The response from the server.
data
The data returned from the server.
error
The error already encountered if it exists.
Return Value
The result data type.