Request
open class Request
Request
is the common superclass of all Alamofire request types and provides common state, delegate, and callback
handling.
-
State of the
Request
, with managed transitions between states set when callingresume()
,suspend()
, orcancel()
on theRequest
.- initialized: Initial state of the
Request
. - resumed: Set when
resume()
is called. Any tasks created for theRequest
will haveresume()
called on them in this state. - suspended: Set when
suspend()
is called. Any tasks created for theRequest
will havesuspend()
called on them in this state. - cancelled: Set when
cancel()
is called. Any tasks created for theRequest
will havecancel()
called on them. Unlikeresumed
orsuspended
, once in thecancelled
state, theRequest
can no longer transition to any other state.
Declaration
Swift
public enum State
- initialized: Initial state of the
-
UUID
prividing a unique identifier for theRequest
, used in theHashable
andEquatable
conformances.Declaration
Swift
public let id: UUID
-
The serial queue for all internal async actions.
Declaration
Swift
public let underlyingQueue: DispatchQueue
-
The queue used for all serialization actions. By default it’s a serial queue that targets
underlyingQueue
.Declaration
Swift
public let serializationQueue: DispatchQueue
-
EventMonitor
used for event callbacks.Declaration
Swift
public let eventMonitor: EventMonitor?
-
The
Request
‘s delegate.Declaration
Swift
public weak var delegate: RequestDelegate?
-
Returns whether
state
is.cancelled
.Declaration
Swift
public var isCancelled: Bool { get }
-
Returns whether
state is
.resumed`.Declaration
Swift
public var isResumed: Bool { get }
-
Returns whether
state
is.suspended
.Declaration
Swift
public var isSuspended: Bool { get }
-
Returns whether
state
is.initialized
.Declaration
Swift
public var isInitialized: Bool { get }
-
Closure type executed when monitoring the upload or download progress of a request.
Declaration
Swift
public typealias ProgressHandler = (Progress) -> Void
-
Progress
of the upload of the body of the executedURLRequest
. Reset to0
if theRequest
is retried.Declaration
Swift
public let uploadProgress: Progress
-
Progress
of the download of any response data. Reset to0
if theRequest
is retried.Declaration
Swift
public let downloadProgress: Progress
-
URLCredential
used for authentication challenges. Created by calling one of theauthenticate
methods.Declaration
Swift
public private(set) var credential: URLCredential? { get set }
-
All
URLRequests
created on behalf of theRequest
, including original and adapted requests.Declaration
Swift
public var requests: [URLRequest] { get }
-
First
URLRequest
created on behalf of theRequest
. May not be the first one actually executed.Declaration
Swift
public var firstRequest: URLRequest? { get }
-
Last
URLRequest
created on behalf of theRequest
.Declaration
Swift
public var lastRequest: URLRequest? { get }
-
Current
URLRequest
created on behalf of theRequest
.Declaration
Swift
public var request: URLRequest? { get }
-
URLRequest
s from all of theURLSessionTask
s executed on behalf of theRequest
.Declaration
Swift
public var performedRequests: [URLRequest] { get }
-
HTTPURLResponse
received from the server, if any. If theRequest
was retried, this is the response of the lastURLSessionTask
.Declaration
Swift
public var response: HTTPURLResponse? { get }
-
All
URLSessionTask
s created on behalf of theRequest
.Declaration
Swift
public var tasks: [URLSessionTask] { get }
-
First
URLSessionTask
created on behalf of theRequest
.Declaration
Swift
public var firstTask: URLSessionTask? { get }
-
Last
URLSessionTask
crated on behalf of theRequest
.Declaration
Swift
public var lastTask: URLSessionTask? { get }
-
Current
URLSessionTask
created on behalf of theRequest
.Declaration
Swift
public var task: URLSessionTask? { get }
-
All
URLSessionTaskMetrics
gathered on behalf of theRequest
. Should correspond to thetasks
created.Declaration
Swift
public var allMetrics: [URLSessionTaskMetrics] { get }
-
First
URLSessionTaskMetrics
gathered on behalf of theRequest
.Declaration
Swift
public var firstMetrics: URLSessionTaskMetrics? { get }
-
Last
URLSessionTaskMetrics
gathered on behalf of theRequest
.Declaration
Swift
public var lastMetrics: URLSessionTaskMetrics? { get }
-
Current
URLSessionTaskMetrics
gathered on behalf of theRequest
.Declaration
Swift
public var metrics: URLSessionTaskMetrics? { get }
-
Number of times the
Request
has been retried.Declaration
Swift
public var retryCount: Int { get }
-
Error
returned from Alamofire internally, from the network request directly, or any validators executed.Declaration
Swift
fileprivate(set) public var error: Error? { get set }
-
Default initializer for the
Request
superclass.Declaration
Swift
public init(id: UUID = UUID(), underlyingQueue: DispatchQueue, serializationQueue: DispatchQueue, eventMonitor: EventMonitor?, delegate: RequestDelegate)
Parameters
id
UUID
used for theHashable
andEquatable
implementations. Defaults to a randomUUID
.underlyingQueue
DispatchQueue
on which all internalRequest
work is performed.serializationQueue
DispatchQueue
on which all serialization work is performed. Targets theunderlyingQueue
when created by aSessionManager
.eventMonitor
EventMonitor
used for event callbacks from internalRequest
actions.delegate
RequestDelegate
that provides an interface to actions not performed by theRequest
.
-
Cancels the
Request
. Once cancelled, aRequest
can no longer be resumed or suspended.Declaration
Swift
@discardableResult open func cancel() -> Self
Return Value
The
Request
. -
Suspends the
Request
.Declaration
Swift
@discardableResult open func suspend() -> Self
Return Value
The
Request
. -
Resumes the
Request
.Declaration
Swift
@discardableResult open func resume() -> Self
Return Value
The
Request
.
-
Associates a credential using the provided values with the
Request
.Declaration
Swift
@discardableResult open func authenticate(username: String, password: String, persistence: URLCredential.Persistence = .forSession) -> Self
Parameters
username
The username.
password
The password.
persistence
The
URLCredential.Persistence
for the createdURLCredential
.Return Value
The
Request
. -
Associates the provided credential with the
Request
.Declaration
Swift
@discardableResult open func authenticate(with credential: URLCredential) -> Self
Parameters
credential
The
URLCredential
.Return Value
The
Request
. -
Sets a closure to be called periodically during the lifecycle of the
Request
as data is read from the server.Only the last closure provided is used.
Declaration
Swift
@discardableResult open func downloadProgress(queue: DispatchQueue = .main, closure: @escaping ProgressHandler) -> Self
Parameters
queue
The
DispatchQueue
to execute the closure on. Defaults to.main
.closure
The code to be executed periodically as data is read from the server.
queue
The
DispatchQueue
to execute the closure on. Defaults to.main
.closure
The code to be executed periodically as data is read from the server.
Return Value
The
Request
. -
Sets a closure to be called periodically during the lifecycle of the
Request
as data is sent to the server.Only the last closure provided is used.
Declaration
Swift
@discardableResult open func uploadProgress(queue: DispatchQueue = .main, closure: @escaping ProgressHandler) -> Self
Parameters
queue
The
DispatchQueue
to execute the closure on. Defaults to.main
.closure
The closure to be executed periodically as data is sent to the server.
Return Value
The
Request
.
-
Posted when a
Request
‘s task is resumed. TheNotification
contains the resumedRequest
.Declaration
Swift
static let didResume: Notification.Name
-
Posted when a
Request
‘s task is suspended. TheNotification
contains the suspendedRequest
.Declaration
Swift
static let didSuspend: Notification.Name
-
Posted when a
Request
is cancelled. TheNotification
contains the cancelledRequest
.Declaration
Swift
static let didCancel: Notification.Name
-
Posted when a
Request
‘s task is completed. TheNotification
contains the completedRequest
.Declaration
Swift
static let didComplete: Notification.Name
-
Declaration
Swift
public static func == (lhs: Request, rhs: Request) -> Bool
-
Declaration
Swift
public var hashValue: Int { get }
-
A textual representation of this instance, including the
HTTPMethod
andURL
if theURLRequest
has been created, as well as the response status code, if a response has been received.Declaration
Swift
public var description: String { get }
-
A textual representation of this instance in the form of a cURL command.
Declaration
Swift
public var debugDescription: String { get }
-
Used to represent whether a validation succeeded or failed.
Declaration
Swift
public typealias ValidationResult = Result<Void>