DownloadResponse
public struct DownloadResponse<Success, Failure> : Sendable where Success : Sendable, Failure : Error
extension DownloadResponse: CustomStringConvertible, CustomDebugStringConvertible
Used to store all data associated with a serialized response of a download request.
-
The URL request sent to the server.
Declaration
Swift
public let request: URLRequest? -
The server’s response to the URL request.
Declaration
Swift
public let response: HTTPURLResponse? -
The final destination URL of the data returned from the server after it is moved.
Declaration
Swift
public let fileURL: URL? -
The resume data generated if the request was cancelled.
Declaration
Swift
public let resumeData: Data? -
The final metrics of the response.
Note
Due toFB7624529, collection ofURLSessionTaskMetricson watchOS is currently disabled.`Declaration
Swift
public let metrics: URLSessionTaskMetrics? -
The time taken to serialize the response.
Declaration
Swift
public let serializationDuration: TimeInterval -
The result of response serialization.
Declaration
Swift
public let result: Result<Success, Failure> -
Returns the associated value of the result if it is a success,
nilotherwise.Declaration
Swift
public var value: Success? { get } -
Returns the associated error value if the result if it is a failure,
nilotherwise.Declaration
Swift
public var error: Failure? { get } -
Creates a
DownloadResponseinstance with the specified parameters derived from response serialization.Declaration
Swift
public init(request: URLRequest?, response: HTTPURLResponse?, fileURL: URL?, resumeData: Data?, metrics: URLSessionTaskMetrics?, serializationDuration: TimeInterval, result: Result<Success, Failure>)Parameters
requestThe
URLRequestsent to the server.responseThe
HTTPURLResponsefrom the server.fileURLThe final destination URL of the data returned from the server after it is moved.
resumeDataThe resume
Datagenerated if the request was cancelled.metricsThe
URLSessionTaskMetricsof theDownloadRequest.serializationDurationThe duration taken by serialization.
resultThe
Resultof response serialization. -
The textual representation used when written to an output stream, which includes whether the result was a success or failure.
Declaration
Swift
public var description: String { get } -
The debug textual representation used when written to an output stream, which includes the URL request, the URL response, the temporary and destination URLs, the resume data, the durations of the network and serialization actions, and the response serialization result.
Declaration
Swift
public var debugDescription: String { get } -
Evaluates the given closure when the result of this
DownloadResponseis a success, passing the unwrapped result value as a parameter.Use the
mapmethod with a closure that does not throw. For example:let possibleData: DownloadResponse<Data> = ... let possibleInt = possibleData.map { $0.count }Declaration
Swift
public func map<NewSuccess>(_ transform: (Success) -> NewSuccess) -> DownloadResponse<NewSuccess, Failure> where NewSuccess : SendableParameters
transformA closure that takes the success value of the instance’s result.
Return Value
A
DownloadResponsewhose result wraps the value returned by the given closure. If this instance’s result is a failure, returns a response wrapping the same failure. -
Evaluates the given closure when the result of this
DownloadResponseis a success, passing the unwrapped result value as a parameter.Use the
tryMapmethod with a closure that may throw an error. For example:let possibleData: DownloadResponse<Data> = ... let possibleObject = possibleData.tryMap { try JSONSerialization.jsonObject(with: $0) }Declaration
Swift
public func tryMap<NewSuccess>(_ transform: (Success) throws -> NewSuccess) -> DownloadResponse<NewSuccess, any Error> where NewSuccess : SendableParameters
transformA closure that takes the success value of the instance’s result.
Return Value
A success or failure
DownloadResponsedepending on the result of the given closure. If this instance’s result is a failure, returns the same failure. -
Evaluates the specified closure when the
DownloadResponseis a failure, passing the unwrapped error as a parameter.Use the
mapErrorfunction with a closure that does not throw. For example:let possibleData: DownloadResponse<Data> = ... let withMyError = possibleData.mapError { MyError.error($0) }Declaration
Swift
public func mapError<NewFailure>(_ transform: (Failure) -> NewFailure) -> DownloadResponse<Success, NewFailure> where NewFailure : ErrorParameters
transformA closure that takes the error of the instance.
Return Value
A
DownloadResponseinstance containing the result of the transform. -
Evaluates the specified closure when the
DownloadResponseis a failure, passing the unwrapped error as a parameter.Use the
tryMapErrorfunction with a closure that may throw an error. For example:let possibleData: DownloadResponse<Data> = ... let possibleObject = possibleData.tryMapError { try someFailableFunction(taking: $0) }Declaration
Swift
public func tryMapError<NewFailure>(_ transform: (Failure) throws -> NewFailure) -> DownloadResponse<Success, any Error> where NewFailure : ErrorParameters
transformA throwing closure that takes the error of the instance.
Return Value
A
DownloadResponseinstance containing the result of the transform.
View on GitHub
Install in Dash