-
Closure type handling
DataStreamRequest.Streamvalues.Declaration
Swift
public typealias Handler<Success, Failure> = @Sendable (Stream<Success, Failure>) throws -> Void where Failure : Error -
Type encapsulating an
See moreEventas it flows through the stream, as well as aCancellationTokenwhich 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 moreResultof processing streamedDataor 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 moreDataStreamRequestwhen the stream was completed.Declaration
Swift
public struct Completion : Sendable -
Type used to cancel an ongoing stream.
See moreDeclaration
Swift
public struct CancellationToken : Sendable -
URLRequestConvertiblevalue used to createURLRequests 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
URLRequestandHTTPURLResponsereceived for the instance using the providedValidationclosure.Declaration
Swift
@discardableResult public func validate(_ validation: @escaping Validation) -> SelfParameters
validationValidationclosure used to validate the request and response.Return Value
The
DataStreamRequest. -
Produces an
InputStreamthat receives theDatareceived by the instance.Note
The
InputStreamproduced 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 hasstartRequestsImmediatelyset totrue.Declaration
Swift
public func asInputStream(bufferSize: Int = 1024) -> InputStream?Parameters
bufferSizeSize, in bytes, of the buffer between the
OutputStreamandInputStream.Return Value
The
InputStreambound to the internalOutboundStream. -
Sets a closure called whenever the
DataRequestproduces anHTTPURLResponseand providing a completion handler to return aResponseDispositionvalue.Declaration
Swift
@discardableResult @_disfavoredOverload @preconcurrency public func onHTTPResponse( on queue: DispatchQueue = .main, perform handler: @escaping @Sendable (_ response: HTTPURLResponse, _ completionHandler: @escaping @Sendable (ResponseDisposition) -> Void) -> Void ) -> SelfParameters
queueDispatchQueueon which the closure will be called..mainby default.handlerClosure called when the instance produces an
HTTPURLResponse. ThecompletionHandlerprovided MUST be called, otherwise the request will never complete.Return Value
The instance.
-
Sets a closure called whenever the
DataRequestproduces anHTTPURLResponse.Declaration
Swift
@discardableResult @preconcurrency public func onHTTPResponse(on queue: DispatchQueue = .main, perform handler: @escaping @Sendable (HTTPURLResponse) -> Void) -> SelfParameters
queueDispatchQueueon which the closure will be called..mainby default.handlerClosure called when the instance produces an
HTTPURLResponse.Return Value
The instance.
-
Adds a
StreamHandlerwhich performs no parsing on incomingData.Declaration
Swift
@discardableResult @preconcurrency public func responseStream(on queue: DispatchQueue = .main, stream: @escaping Handler<Data, Never>) -> SelfParameters
queueDispatchQueueon which to performStreamHandlerclosure.streamStreamHandlerclosure called asDatais received. May be called multiple times.Return Value
The
DataStreamRequest. -
Adds a
StreamHandlerwhich uses the providedDataStreamSerializerto process incomingData.Declaration
Swift
@discardableResult @preconcurrency public func responseStream<Serializer: DataStreamSerializer>(using serializer: Serializer, on queue: DispatchQueue = .main, stream: @escaping Handler<Serializer.SerializedObject, AFError>) -> SelfParameters
serializerDataStreamSerializerused to process incomingData. Its work is done on theserializationQueue.queueDispatchQueueon which to performStreamHandlerclosure.streamStreamHandlerclosure called asDatais received. May be called multiple times.Return Value
The
DataStreamRequest. -
Adds a
StreamHandlerwhich parses incomingDataas a UTF8String.Declaration
Swift
@discardableResult @preconcurrency public func responseStreamString(on queue: DispatchQueue = .main, stream: @escaping Handler<String, Never>) -> SelfParameters
queueDispatchQueueon which to performStreamHandlerclosure.streamStreamHandlerclosure called asDatais received. May be called multiple times.Return Value
The
DataStreamRequest. -
Adds a
StreamHandlerwhich parses incomingDatausing 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: SendableParameters
typeDecodabletype to parse incomingDatainto.queueDispatchQueueon which to performStreamHandlerclosure.decoderDataDecoderused to decode the incomingData.preprocessorDataPreprocessorused to process the incomingDatabefore it’s passed to thedecoder.streamStreamHandlerclosure called asDatais received. May be called multiple times.Return Value
The
DataStreamRequest.
-
Creates a
DataStreamPublisherfor this instance using the givenDataStreamSerializerandDispatchQueue.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
serializerDataStreamSerializerused to serialize the streamedData.queueDispatchQueueon which theDataRequest.Streamvalues will be published..mainby default.Return Value
The
DataStreamPublisher. -
Creates a
DataStreamPublisherfor this instance which uses aPassthroughStreamSerializerto streamDataunserialized.Declaration
Swift
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) public func publishData(queue: DispatchQueue = .main) -> DataStreamPublisher<Data>Parameters
queueDispatchQueueon which theDataRequest.Streamvalues will be published..mainby default.Return Value
The
DataStreamPublisher. -
Creates a
DataStreamPublisherfor this instance which uses aStringStreamSerializerto serialize streamDatavalues intoStringvalues.Declaration
Swift
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) public func publishString(queue: DispatchQueue = .main) -> DataStreamPublisher<String>Parameters
queueDispatchQueueon which theDataRequest.Streamvalues will be published..mainby default.Return Value
The
DataStreamPublisher. -
Creates a
DataStreamPublisherfor this instance which uses aDecodableStreamSerializerwith the provided parameters to serialize streamDatavalues 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
typeDecodabletype to which to decode streamData. Inferred from the context by default.queueDispatchQueueon which theDataRequest.Streamvalues will be published..mainby default.decoderDataDecoderinstance used to decode streamData.JSONDecoder()by default.preprocessorDataPreprocessorwhich filters incoming streamDatabefore serialization.PassthroughPreprocessor()by default.Return Value
The
DataStreamPublisher.
-
Creates a
StreamOf<HTTPURLResponse>for the instance’s responses.Declaration
Parameters
bufferingPolicyBufferingPolicythat determines the stream’s buffering behavior..unboundedby default.Return Value
-
Sets an async closure returning a
Request.ResponseDisposition, called whenever theDataStreamRequestproduces anHTTPURLResponse.Note
Most requests will only produce a single response for each outgoing attempt (initial + retries). However, some types of response may trigger multiple
HTTPURLResponses, 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) -> SelfParameters
handlerAsync closure executed when a new
HTTPURLResponseis received and returning aResponseDispositionvalue. 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
DataStreamRequestproduces anHTTPURLResponse.Note
Most requests will only produce a single response for each outgoing attempt (initial + retries). However, some types of response may trigger multiple
HTTPURLResponses, 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) -> SelfParameters
handlerAsync closure executed when a new
HTTPURLResponseis 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
DataStreamTaskused toawaitstreams of serialized values.Declaration
Swift
public func streamTask() -> DataStreamTaskReturn Value
The
DataStreamTask.
-
A closure used to validate a request that takes a
URLRequestandHTTPURLResponseand returns whether the request was valid.Declaration
Swift
public typealias Validation = @Sendable (_ 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 @preconcurrency public func validate<S>(statusCode acceptableStatusCodes: S) -> Self where S : Sendable, S : Sequence, S.Element == IntParameters
acceptableStatusCodesSequenceof 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 @preconcurrency public func validate<S>(contentType acceptableContentTypes: @escaping @Sendable @autoclosure () -> S) -> Self where S : Sendable, S : Sequence, S.Element == StringParameters
contentTypeThe 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() -> SelfReturn Value
The instance.
View on GitHub
Install in Dash