DataStreamRequest

public final class DataStreamRequest : Request

Request subclass which streams HTTP response Data through a Handler closure.

  • Closure type handling DataStreamRequest.Stream values.

    Declaration

    Swift

    public typealias Handler<Success, Failure> = (Stream<Success, Failure>) throws -> Void where Failure : Error
  • Type encapsulating an Event as it flows through the stream, as well as a CancellationToken which can be used to stop the stream at any time.

    See more

    Declaration

    Swift

    public struct Stream<Success, Failure> where Failure : Error
  • Type representing an event flowing through the stream. Contains either the Result of processing streamed Data or the completion of the stream.

    See more

    Declaration

    Swift

    public enum Event<Success, Failure> where Failure : Error
  • Value containing the state of a DataStreamRequest when the stream was completed.

    See more

    Declaration

    Swift

    public struct Completion
  • Type used to cancel an ongoing stream.

    See more

    Declaration

    Swift

    public struct CancellationToken
  • URLRequestConvertible value used to create URLRequests for this instance.

    Declaration

    Swift

    public let convertible: URLRequestConvertible
  • Whether or not the instance will be cancelled if stream parsing encounters an error.

    Declaration

    Swift

    public let automaticallyCancelOnStreamError: Bool
  • Validates the URLRequest and HTTPURLResponse received for the instance using the provided Validation 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 the Data received by the instance.

    Note

    The InputStream produced by this method must have open() called before being able to read Data. Additionally, this method will automatically call resume() on the instance, regardless of whether or not the creating session has startRequestsImmediately set to true.

    Declaration

    Swift

    public func asInputStream(bufferSize: Int = 1024) -> InputStream?

    Parameters

    bufferSize

    Size, in bytes, of the buffer between the OutputStream and InputStream.

    Return Value

    The InputStream bound to the internal OutboundStream.

DataRequest / UploadRequest

DataStreamRequest

  • Adds a StreamHandler which performs no parsing on incoming Data.

    Declaration

    Swift

    @discardableResult
    public func responseStream(on queue: DispatchQueue = .main, stream: @escaping Handler<Data, Never>) -> Self

    Parameters

    queue

    DispatchQueue on which to perform StreamHandler closure.

    stream

    StreamHandler closure called as Data is received. May be called multiple times.

    Return Value

    The DataStreamRequest.

  • Adds a StreamHandler which uses the provided DataStreamSerializer to process incoming Data.

    Declaration

    Swift

    @discardableResult
    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 incoming Data. Its work is done on the serializationQueue.

    queue

    DispatchQueue on which to perform StreamHandler closure.

    stream

    StreamHandler closure called as Data is received. May be called multiple times.

    Return Value

    The DataStreamRequest.

  • Adds a StreamHandler which parses incoming Data as a UTF8 String.

    Declaration

    Swift

    @discardableResult
    public func responseStreamString(on queue: DispatchQueue = .main,
                                     stream: @escaping Handler<String, Never>) -> Self

    Parameters

    queue

    DispatchQueue on which to perform StreamHandler closure.

    stream

    StreamHandler closure called as Data is received. May be called multiple times.

    Return Value

    The DataStreamRequest.

  • Adds a StreamHandler which parses incoming Data using the provided DataDecoder.

    Declaration

    Swift

    @discardableResult
    public func responseStreamDecodable<T: Decodable>(of type: T.Type = T.self,
                                                      on queue: DispatchQueue = .main,
                                                      using decoder: DataDecoder = JSONDecoder(),
                                                      preprocessor: DataPreprocessor = PassthroughPreprocessor(),
                                                      stream: @escaping Handler<T, AFError>) -> Self

    Parameters

    type

    Decodable type to parse incoming Data into.

    queue

    DispatchQueue on which to perform StreamHandler closure.

    decoder

    DataDecoder used to decode the incoming Data.

    preprocessor

    DataPreprocessor used to process the incoming Data before it’s passed to the decoder.

    stream

    StreamHandler closure called as Data is received. May be called multiple times.

    Return Value

    The DataStreamRequest.

  • A closure used to validate a request that takes a URLRequest and HTTPURLResponse 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

    statusCode

    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: @autoclosure @escaping () -> 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.