-
Closure type handling
DataStreamRequest.Stream
values.Declaration
Swift
public typealias Handler<Success, Failure> = @Sendable (Stream<Success, Failure>) throws -> Void where Failure : Error
-
Type encapsulating an
See moreEvent
as it flows through the stream, as well as aCancellationToken
which can be used to stop the stream at any time.Declaration
Swift
public struct Stream<Success, Failure> : Sendable where Success : Sendable, Failure : Error
-
Type representing an event flowing through the stream. Contains either the
See moreResult
of processing streamedData
or the completion of the stream.Declaration
Swift
public enum Event<Success, Failure> : Sendable where Success : Sendable, Failure : Error
-
Value containing the state of a
See moreDataStreamRequest
when the stream was completed.Declaration
Swift
public struct Completion : Sendable
-
Type used to cancel an ongoing stream.
See moreDeclaration
Swift
public struct CancellationToken : Sendable
-
URLRequestConvertible
value used to createURLRequest
s for this instance.Declaration
Swift
public let convertible: any URLRequestConvertible
-
Whether or not the instance will be cancelled if stream parsing encounters an error.
Declaration
Swift
public let automaticallyCancelOnStreamError: Bool
-
Validates the
URLRequest
andHTTPURLResponse
received for the instance using the providedValidation
closure.Declaration
Swift
@discardableResult public func validate(_ validation: @escaping Validation) -> Self
Parameters
validation
Validation
closure used to validate the request and response.Return Value
The
DataStreamRequest
. -
Produces an
InputStream
that receives theData
received by the instance.Note
The
InputStream
produced by this method must haveopen()
called before being able to readData
. Additionally, this method will automatically callresume()
on the instance, regardless of whether or not the creating session hasstartRequestsImmediately
set totrue
.Declaration
Swift
public func asInputStream(bufferSize: Int = 1024) -> InputStream?
Parameters
bufferSize
Size, in bytes, of the buffer between the
OutputStream
andInputStream
.Return Value
The
InputStream
bound to the internalOutboundStream
. -
Sets a closure called whenever the
DataRequest
produces anHTTPURLResponse
and providing a completion handler to return aResponseDisposition
value.Declaration
Swift
@discardableResult @_disfavoredOverload @preconcurrency public func onHTTPResponse( on queue: DispatchQueue = .main, perform handler: @escaping @Sendable (_ response: HTTPURLResponse, _ completionHandler: @escaping @Sendable (ResponseDisposition) -> Void) -> Void ) -> Self
Parameters
queue
DispatchQueue
on which the closure will be called..main
by default.handler
Closure called when the instance produces an
HTTPURLResponse
. ThecompletionHandler
provided MUST be called, otherwise the request will never complete.Return Value
The instance.
-
Sets a closure called whenever the
DataRequest
produces anHTTPURLResponse
.Declaration
Swift
@discardableResult @preconcurrency public func onHTTPResponse(on queue: DispatchQueue = .main, perform handler: @escaping @Sendable (HTTPURLResponse) -> Void) -> Self
Parameters
queue
DispatchQueue
on which the closure will be called..main
by default.handler
Closure called when the instance produces an
HTTPURLResponse
.Return Value
The instance.
-
Adds a
StreamHandler
which performs no parsing on incomingData
.Declaration
Swift
@discardableResult @preconcurrency public func responseStream(on queue: DispatchQueue = .main, stream: @escaping Handler<Data, Never>) -> Self
Parameters
queue
DispatchQueue
on which to performStreamHandler
closure.stream
StreamHandler
closure called asData
is received. May be called multiple times.Return Value
The
DataStreamRequest
. -
Adds a
StreamHandler
which uses the providedDataStreamSerializer
to process incomingData
.Declaration
Swift
@discardableResult @preconcurrency public func responseStream<Serializer: DataStreamSerializer>(using serializer: Serializer, on queue: DispatchQueue = .main, stream: @escaping Handler<Serializer.SerializedObject, AFError>) -> Self
Parameters
serializer
DataStreamSerializer
used to process incomingData
. Its work is done on theserializationQueue
.queue
DispatchQueue
on which to performStreamHandler
closure.stream
StreamHandler
closure called asData
is received. May be called multiple times.Return Value
The
DataStreamRequest
. -
Adds a
StreamHandler
which parses incomingData
as a UTF8String
.Declaration
Swift
@discardableResult @preconcurrency public func responseStreamString(on queue: DispatchQueue = .main, stream: @escaping Handler<String, Never>) -> Self
Parameters
queue
DispatchQueue
on which to performStreamHandler
closure.stream
StreamHandler
closure called asData
is received. May be called multiple times.Return Value
The
DataStreamRequest
. -
Adds a
StreamHandler
which parses incomingData
using the providedDataDecoder
.Declaration
Swift
@discardableResult @preconcurrency public func responseStreamDecodable<T: Decodable>(of type: T.Type = T.self, on queue: DispatchQueue = .main, using decoder: any DataDecoder = JSONDecoder(), preprocessor: any DataPreprocessor = PassthroughPreprocessor(), stream: @escaping Handler<T, AFError>) -> Self where T: Sendable
Parameters
type
Decodable
type to parse incomingData
into.queue
DispatchQueue
on which to performStreamHandler
closure.decoder
DataDecoder
used to decode the incomingData
.preprocessor
DataPreprocessor
used to process the incomingData
before it’s passed to thedecoder
.stream
StreamHandler
closure called asData
is received. May be called multiple times.Return Value
The
DataStreamRequest
.
-
Creates a
DataStreamPublisher
for this instance using the givenDataStreamSerializer
andDispatchQueue
.Declaration
Swift
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) public func publishStream<Serializer: DataStreamSerializer>(using serializer: Serializer, on queue: DispatchQueue = .main) -> DataStreamPublisher<Serializer.SerializedObject>
Parameters
serializer
DataStreamSerializer
used to serialize the streamedData
.queue
DispatchQueue
on which theDataRequest.Stream
values will be published..main
by default.Return Value
The
DataStreamPublisher
. -
Creates a
DataStreamPublisher
for this instance which uses aPassthroughStreamSerializer
to streamData
unserialized.Declaration
Swift
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) public func publishData(queue: DispatchQueue = .main) -> DataStreamPublisher<Data>
Parameters
queue
DispatchQueue
on which theDataRequest.Stream
values will be published..main
by default.Return Value
The
DataStreamPublisher
. -
Creates a
DataStreamPublisher
for this instance which uses aStringStreamSerializer
to serialize streamData
values intoString
values.Declaration
Swift
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) public func publishString(queue: DispatchQueue = .main) -> DataStreamPublisher<String>
Parameters
queue
DispatchQueue
on which theDataRequest.Stream
values will be published..main
by default.Return Value
The
DataStreamPublisher
. -
Creates a
DataStreamPublisher
for this instance which uses aDecodableStreamSerializer
with the provided parameters to serialize streamData
values into the provided type.Declaration
Swift
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) public func publishDecodable<T: Decodable>(type: T.Type = T.self, queue: DispatchQueue = .main, decoder: any DataDecoder = JSONDecoder(), preprocessor: any DataPreprocessor = PassthroughPreprocessor()) -> DataStreamPublisher<T>
Parameters
type
Decodable
type to which to decode streamData
. Inferred from the context by default.queue
DispatchQueue
on which theDataRequest.Stream
values will be published..main
by default.decoder
DataDecoder
instance used to decode streamData
.JSONDecoder()
by default.preprocessor
DataPreprocessor
which filters incoming streamData
before serialization.PassthroughPreprocessor()
by default.Return Value
The
DataStreamPublisher
.
-
Creates a
StreamOf<HTTPURLResponse>
for the instance’s responses.Declaration
Parameters
bufferingPolicy
BufferingPolicy
that determines the stream’s buffering behavior..unbounded
by default.Return Value
-
Sets an async closure returning a
Request.ResponseDisposition
, called whenever theDataStreamRequest
produces anHTTPURLResponse
.Note
Most requests will only produce a single response for each outgoing attempt (initial + retries). However, some types of response may trigger multiple
HTTPURLResponse
s, such as multipart streams, where responses after the first will contain the part headers.Declaration
Swift
@discardableResult @_disfavoredOverload public func onHTTPResponse(perform handler: @escaping @Sendable (HTTPURLResponse) async -> ResponseDisposition) -> Self
Parameters
handler
Async closure executed when a new
HTTPURLResponse
is received and returning aResponseDisposition
value. This value determines whether to continue the request or cancel it as ifcancel()
had been called on the instance. Note, this closure is called on an arbitrary thread, so any synchronous calls in it will execute in that context.Return Value
The instance.
-
Sets an async closure called whenever the
DataStreamRequest
produces anHTTPURLResponse
.Note
Most requests will only produce a single response for each outgoing attempt (initial + retries). However, some types of response may trigger multiple
HTTPURLResponse
s, such as multipart streams, where responses after the first will contain the part headers.Declaration
Swift
@discardableResult public func onHTTPResponse(perform handler: @escaping @Sendable (HTTPURLResponse) async -> Void) -> Self
Parameters
handler
Async closure executed when a new
HTTPURLResponse
is received. Note, this closure is called on an arbitrary thread, so any synchronous calls in it will execute in that context.Return Value
The instance.
-
Creates a
DataStreamTask
used toawait
streams of serialized values.Declaration
Swift
public func streamTask() -> DataStreamTask
Return Value
The
DataStreamTask
.
-
A closure used to validate a request that takes a
URLRequest
andHTTPURLResponse
and returns whether the request was valid.Declaration
Swift
public typealias Validation = (_ request: URLRequest?, _ response: HTTPURLResponse) -> ValidationResult
-
Validates that the response has a status code in the specified sequence.
If validation fails, subsequent calls to response handlers will have an associated error.
Declaration
Swift
@discardableResult public func validate<S>(statusCode acceptableStatusCodes: S) -> Self where S : Sequence, S.Element == Int
Parameters
acceptableStatusCodes
Sequence
of acceptable response status codes.Return Value
The instance.
-
Validates that the response has a content type in the specified sequence.
If validation fails, subsequent calls to response handlers will have an associated error.
Declaration
Swift
@discardableResult public func validate<S>(contentType acceptableContentTypes: @escaping @autoclosure () -> S) -> Self where S : Sequence, S.Element == String
Parameters
contentType
The acceptable content types, which may specify wildcard types and/or subtypes.
Return Value
The request.
-
Validates that the response has a status code in the default acceptable range of 200…299, and that the content type matches any specified in the Accept HTTP header field.
If validation fails, subsequent calls to response handlers will have an associated error.
Declaration
Swift
@discardableResult public func validate() -> Self
Return Value
The instance.