123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704 |
- #if compiler(>=5.6.0) && canImport(_Concurrency)
- import Foundation
- @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
- extension Request {
-
-
-
-
-
- public func uploadProgress(bufferingPolicy: StreamOf<Progress>.BufferingPolicy = .unbounded) -> StreamOf<Progress> {
- stream(bufferingPolicy: bufferingPolicy) { [unowned self] continuation in
- uploadProgress(queue: .singleEventQueue) { progress in
- continuation.yield(progress)
- }
- }
- }
-
-
-
-
-
- public func downloadProgress(bufferingPolicy: StreamOf<Progress>.BufferingPolicy = .unbounded) -> StreamOf<Progress> {
- stream(bufferingPolicy: bufferingPolicy) { [unowned self] continuation in
- downloadProgress(queue: .singleEventQueue) { progress in
- continuation.yield(progress)
- }
- }
- }
-
-
-
-
-
- public func urlRequests(bufferingPolicy: StreamOf<URLRequest>.BufferingPolicy = .unbounded) -> StreamOf<URLRequest> {
- stream(bufferingPolicy: bufferingPolicy) { [unowned self] continuation in
- onURLRequestCreation(on: .singleEventQueue) { request in
- continuation.yield(request)
- }
- }
- }
-
-
-
-
-
- public func urlSessionTasks(bufferingPolicy: StreamOf<URLSessionTask>.BufferingPolicy = .unbounded) -> StreamOf<URLSessionTask> {
- stream(bufferingPolicy: bufferingPolicy) { [unowned self] continuation in
- onURLSessionTaskCreation(on: .singleEventQueue) { task in
- continuation.yield(task)
- }
- }
- }
-
-
-
-
-
- public func cURLDescriptions(bufferingPolicy: StreamOf<String>.BufferingPolicy = .unbounded) -> StreamOf<String> {
- stream(bufferingPolicy: bufferingPolicy) { [unowned self] continuation in
- cURLDescription(on: .singleEventQueue) { description in
- continuation.yield(description)
- }
- }
- }
- private func stream<T>(of type: T.Type = T.self,
- bufferingPolicy: StreamOf<T>.BufferingPolicy = .unbounded,
- yielder: @escaping (StreamOf<T>.Continuation) -> Void) -> StreamOf<T> {
- StreamOf<T>(bufferingPolicy: bufferingPolicy) { [unowned self] continuation in
- yielder(continuation)
-
- onFinish {
- continuation.finish()
- }
- }
- }
- }
- @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
- public struct DataTask<Value> {
-
- public var response: DataResponse<Value, AFError> {
- get async {
- if shouldAutomaticallyCancel {
- return await withTaskCancellationHandler {
- await task.value
- } onCancel: {
- self.cancel()
- }
- } else {
- return await task.value
- }
- }
- }
-
- public var result: Result<Value, AFError> {
- get async { await response.result }
- }
-
- public var value: Value {
- get async throws {
- try await result.get()
- }
- }
- private let request: DataRequest
- private let task: Task<DataResponse<Value, AFError>, Never>
- private let shouldAutomaticallyCancel: Bool
- fileprivate init(request: DataRequest, task: Task<DataResponse<Value, AFError>, Never>, shouldAutomaticallyCancel: Bool) {
- self.request = request
- self.task = task
- self.shouldAutomaticallyCancel = shouldAutomaticallyCancel
- }
-
- public func cancel() {
- task.cancel()
- }
-
- public func resume() {
- request.resume()
- }
-
- public func suspend() {
- request.suspend()
- }
- }
- @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
- extension DataRequest {
-
-
-
-
-
-
-
-
-
-
-
- public func serializingData(automaticallyCancelling shouldAutomaticallyCancel: Bool = false,
- dataPreprocessor: DataPreprocessor = DataResponseSerializer.defaultDataPreprocessor,
- emptyResponseCodes: Set<Int> = DataResponseSerializer.defaultEmptyResponseCodes,
- emptyRequestMethods: Set<HTTPMethod> = DataResponseSerializer.defaultEmptyRequestMethods) -> DataTask<Data> {
- serializingResponse(using: DataResponseSerializer(dataPreprocessor: dataPreprocessor,
- emptyResponseCodes: emptyResponseCodes,
- emptyRequestMethods: emptyRequestMethods),
- automaticallyCancelling: shouldAutomaticallyCancel)
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public func serializingDecodable<Value: Decodable>(_ type: Value.Type = Value.self,
- automaticallyCancelling shouldAutomaticallyCancel: Bool = false,
- dataPreprocessor: DataPreprocessor = DecodableResponseSerializer<Value>.defaultDataPreprocessor,
- decoder: DataDecoder = JSONDecoder(),
- emptyResponseCodes: Set<Int> = DecodableResponseSerializer<Value>.defaultEmptyResponseCodes,
- emptyRequestMethods: Set<HTTPMethod> = DecodableResponseSerializer<Value>.defaultEmptyRequestMethods) -> DataTask<Value> {
- serializingResponse(using: DecodableResponseSerializer<Value>(dataPreprocessor: dataPreprocessor,
- decoder: decoder,
- emptyResponseCodes: emptyResponseCodes,
- emptyRequestMethods: emptyRequestMethods),
- automaticallyCancelling: shouldAutomaticallyCancel)
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public func serializingString(automaticallyCancelling shouldAutomaticallyCancel: Bool = false,
- dataPreprocessor: DataPreprocessor = StringResponseSerializer.defaultDataPreprocessor,
- encoding: String.Encoding? = nil,
- emptyResponseCodes: Set<Int> = StringResponseSerializer.defaultEmptyResponseCodes,
- emptyRequestMethods: Set<HTTPMethod> = StringResponseSerializer.defaultEmptyRequestMethods) -> DataTask<String> {
- serializingResponse(using: StringResponseSerializer(dataPreprocessor: dataPreprocessor,
- encoding: encoding,
- emptyResponseCodes: emptyResponseCodes,
- emptyRequestMethods: emptyRequestMethods),
- automaticallyCancelling: shouldAutomaticallyCancel)
- }
-
-
-
-
-
-
-
-
-
- public func serializingResponse<Serializer: ResponseSerializer>(using serializer: Serializer,
- automaticallyCancelling shouldAutomaticallyCancel: Bool = false)
- -> DataTask<Serializer.SerializedObject> {
- dataTask(automaticallyCancelling: shouldAutomaticallyCancel) {
- self.response(queue: .singleEventQueue,
- responseSerializer: serializer,
- completionHandler: $0)
- }
- }
-
-
-
-
-
-
-
-
-
-
- public func serializingResponse<Serializer: DataResponseSerializerProtocol>(using serializer: Serializer,
- automaticallyCancelling shouldAutomaticallyCancel: Bool = false)
- -> DataTask<Serializer.SerializedObject> {
- dataTask(automaticallyCancelling: shouldAutomaticallyCancel) {
- self.response(queue: .singleEventQueue,
- responseSerializer: serializer,
- completionHandler: $0)
- }
- }
- private func dataTask<Value>(automaticallyCancelling shouldAutomaticallyCancel: Bool,
- forResponse onResponse: @escaping (@escaping (DataResponse<Value, AFError>) -> Void) -> Void)
- -> DataTask<Value> {
- let task = Task {
- await withTaskCancellationHandler {
- await withCheckedContinuation { continuation in
- onResponse {
- continuation.resume(returning: $0)
- }
- }
- } onCancel: {
- self.cancel()
- }
- }
- return DataTask<Value>(request: self, task: task, shouldAutomaticallyCancel: shouldAutomaticallyCancel)
- }
- }
- @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
- public struct DownloadTask<Value> {
-
- public var response: DownloadResponse<Value, AFError> {
- get async {
- if shouldAutomaticallyCancel {
- return await withTaskCancellationHandler {
- await task.value
- } onCancel: {
- self.cancel()
- }
- } else {
- return await task.value
- }
- }
- }
-
- public var result: Result<Value, AFError> {
- get async { await response.result }
- }
-
- public var value: Value {
- get async throws {
- try await result.get()
- }
- }
- private let task: Task<AFDownloadResponse<Value>, Never>
- private let request: DownloadRequest
- private let shouldAutomaticallyCancel: Bool
- fileprivate init(request: DownloadRequest, task: Task<AFDownloadResponse<Value>, Never>, shouldAutomaticallyCancel: Bool) {
- self.request = request
- self.task = task
- self.shouldAutomaticallyCancel = shouldAutomaticallyCancel
- }
-
- public func cancel() {
- task.cancel()
- }
-
- public func resume() {
- request.resume()
- }
-
- public func suspend() {
- request.suspend()
- }
- }
- @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
- extension DownloadRequest {
-
-
-
-
-
-
-
-
-
-
-
- public func serializingData(automaticallyCancelling shouldAutomaticallyCancel: Bool = false,
- dataPreprocessor: DataPreprocessor = DataResponseSerializer.defaultDataPreprocessor,
- emptyResponseCodes: Set<Int> = DataResponseSerializer.defaultEmptyResponseCodes,
- emptyRequestMethods: Set<HTTPMethod> = DataResponseSerializer.defaultEmptyRequestMethods) -> DownloadTask<Data> {
- serializingDownload(using: DataResponseSerializer(dataPreprocessor: dataPreprocessor,
- emptyResponseCodes: emptyResponseCodes,
- emptyRequestMethods: emptyRequestMethods),
- automaticallyCancelling: shouldAutomaticallyCancel)
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public func serializingDecodable<Value: Decodable>(_ type: Value.Type = Value.self,
- automaticallyCancelling shouldAutomaticallyCancel: Bool = false,
- dataPreprocessor: DataPreprocessor = DecodableResponseSerializer<Value>.defaultDataPreprocessor,
- decoder: DataDecoder = JSONDecoder(),
- emptyResponseCodes: Set<Int> = DecodableResponseSerializer<Value>.defaultEmptyResponseCodes,
- emptyRequestMethods: Set<HTTPMethod> = DecodableResponseSerializer<Value>.defaultEmptyRequestMethods) -> DownloadTask<Value> {
- serializingDownload(using: DecodableResponseSerializer<Value>(dataPreprocessor: dataPreprocessor,
- decoder: decoder,
- emptyResponseCodes: emptyResponseCodes,
- emptyRequestMethods: emptyRequestMethods),
- automaticallyCancelling: shouldAutomaticallyCancel)
- }
-
-
-
-
-
-
-
-
- public func serializingDownloadedFileURL(automaticallyCancelling shouldAutomaticallyCancel: Bool = false) -> DownloadTask<URL> {
- serializingDownload(using: URLResponseSerializer(),
- automaticallyCancelling: shouldAutomaticallyCancel)
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public func serializingString(automaticallyCancelling shouldAutomaticallyCancel: Bool = false,
- dataPreprocessor: DataPreprocessor = StringResponseSerializer.defaultDataPreprocessor,
- encoding: String.Encoding? = nil,
- emptyResponseCodes: Set<Int> = StringResponseSerializer.defaultEmptyResponseCodes,
- emptyRequestMethods: Set<HTTPMethod> = StringResponseSerializer.defaultEmptyRequestMethods) -> DownloadTask<String> {
- serializingDownload(using: StringResponseSerializer(dataPreprocessor: dataPreprocessor,
- encoding: encoding,
- emptyResponseCodes: emptyResponseCodes,
- emptyRequestMethods: emptyRequestMethods),
- automaticallyCancelling: shouldAutomaticallyCancel)
- }
-
-
-
-
-
-
-
-
-
- public func serializingDownload<Serializer: ResponseSerializer>(using serializer: Serializer,
- automaticallyCancelling shouldAutomaticallyCancel: Bool = false)
- -> DownloadTask<Serializer.SerializedObject> {
- downloadTask(automaticallyCancelling: shouldAutomaticallyCancel) {
- self.response(queue: .singleEventQueue,
- responseSerializer: serializer,
- completionHandler: $0)
- }
- }
-
-
-
-
-
-
-
-
-
-
-
- public func serializingDownload<Serializer: DownloadResponseSerializerProtocol>(using serializer: Serializer,
- automaticallyCancelling shouldAutomaticallyCancel: Bool = false)
- -> DownloadTask<Serializer.SerializedObject> {
- downloadTask(automaticallyCancelling: shouldAutomaticallyCancel) {
- self.response(queue: .singleEventQueue,
- responseSerializer: serializer,
- completionHandler: $0)
- }
- }
- private func downloadTask<Value>(automaticallyCancelling shouldAutomaticallyCancel: Bool,
- forResponse onResponse: @escaping (@escaping (DownloadResponse<Value, AFError>) -> Void) -> Void)
- -> DownloadTask<Value> {
- let task = Task {
- await withTaskCancellationHandler {
- await withCheckedContinuation { continuation in
- onResponse {
- continuation.resume(returning: $0)
- }
- }
- } onCancel: {
- self.cancel()
- }
- }
- return DownloadTask<Value>(request: self, task: task, shouldAutomaticallyCancel: shouldAutomaticallyCancel)
- }
- }
- @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
- public struct DataStreamTask {
-
- public typealias Stream<Success, Failure: Error> = StreamOf<DataStreamRequest.Stream<Success, Failure>>
- private let request: DataStreamRequest
- fileprivate init(request: DataStreamRequest) {
- self.request = request
- }
-
-
-
-
-
-
-
-
- public func streamingData(automaticallyCancelling shouldAutomaticallyCancel: Bool = true, bufferingPolicy: Stream<Data, Never>.BufferingPolicy = .unbounded) -> Stream<Data, Never> {
- createStream(automaticallyCancelling: shouldAutomaticallyCancel, bufferingPolicy: bufferingPolicy) { onStream in
- self.request.responseStream(on: .streamCompletionQueue(forRequestID: request.id), stream: onStream)
- }
- }
-
-
-
-
-
-
-
- public func streamingStrings(automaticallyCancelling shouldAutomaticallyCancel: Bool = true, bufferingPolicy: Stream<String, Never>.BufferingPolicy = .unbounded) -> Stream<String, Never> {
- createStream(automaticallyCancelling: shouldAutomaticallyCancel, bufferingPolicy: bufferingPolicy) { onStream in
- self.request.responseStreamString(on: .streamCompletionQueue(forRequestID: request.id), stream: onStream)
- }
- }
-
-
-
-
-
-
-
-
-
- public func streamingDecodables<T>(_ type: T.Type = T.self,
- automaticallyCancelling shouldAutomaticallyCancel: Bool = true,
- bufferingPolicy: Stream<T, AFError>.BufferingPolicy = .unbounded)
- -> Stream<T, AFError> where T: Decodable {
- streamingResponses(serializedUsing: DecodableStreamSerializer<T>(),
- automaticallyCancelling: shouldAutomaticallyCancel,
- bufferingPolicy: bufferingPolicy)
- }
-
-
-
-
-
-
-
-
-
- public func streamingResponses<Serializer: DataStreamSerializer>(serializedUsing serializer: Serializer,
- automaticallyCancelling shouldAutomaticallyCancel: Bool = true,
- bufferingPolicy: Stream<Serializer.SerializedObject, AFError>.BufferingPolicy = .unbounded)
- -> Stream<Serializer.SerializedObject, AFError> {
- createStream(automaticallyCancelling: shouldAutomaticallyCancel, bufferingPolicy: bufferingPolicy) { onStream in
- self.request.responseStream(using: serializer,
- on: .streamCompletionQueue(forRequestID: request.id),
- stream: onStream)
- }
- }
- private func createStream<Success, Failure: Error>(automaticallyCancelling shouldAutomaticallyCancel: Bool = true,
- bufferingPolicy: Stream<Success, Failure>.BufferingPolicy = .unbounded,
- forResponse onResponse: @escaping (@escaping (DataStreamRequest.Stream<Success, Failure>) -> Void) -> Void)
- -> Stream<Success, Failure> {
- StreamOf(bufferingPolicy: bufferingPolicy) {
- guard shouldAutomaticallyCancel,
- request.isInitialized || request.isResumed || request.isSuspended else { return }
- cancel()
- } builder: { continuation in
- onResponse { stream in
- continuation.yield(stream)
- if case .complete = stream.event {
- continuation.finish()
- }
- }
- }
- }
-
- public func cancel() {
- request.cancel()
- }
-
- public func resume() {
- request.resume()
- }
-
- public func suspend() {
- request.suspend()
- }
- }
- @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
- extension DataStreamRequest {
-
-
-
- public func streamTask() -> DataStreamTask {
- DataStreamTask(request: self)
- }
- }
- extension DispatchQueue {
- fileprivate static let singleEventQueue = DispatchQueue(label: "org.alamofire.concurrencySingleEventQueue",
- attributes: .concurrent)
- fileprivate static func streamCompletionQueue(forRequestID id: UUID) -> DispatchQueue {
- DispatchQueue(label: "org.alamofire.concurrencyStreamCompletionQueue-\(id)", target: .singleEventQueue)
- }
- }
- @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
- public struct StreamOf<Element>: AsyncSequence {
- public typealias AsyncIterator = Iterator
- public typealias BufferingPolicy = AsyncStream<Element>.Continuation.BufferingPolicy
- fileprivate typealias Continuation = AsyncStream<Element>.Continuation
- private let bufferingPolicy: BufferingPolicy
- private let onTermination: (() -> Void)?
- private let builder: (Continuation) -> Void
- fileprivate init(bufferingPolicy: BufferingPolicy = .unbounded,
- onTermination: (() -> Void)? = nil,
- builder: @escaping (Continuation) -> Void) {
- self.bufferingPolicy = bufferingPolicy
- self.onTermination = onTermination
- self.builder = builder
- }
- public func makeAsyncIterator() -> Iterator {
- var continuation: AsyncStream<Element>.Continuation?
- let stream = AsyncStream<Element> { innerContinuation in
- continuation = innerContinuation
- builder(innerContinuation)
- }
- return Iterator(iterator: stream.makeAsyncIterator()) {
- continuation?.finish()
- self.onTermination?()
- }
- }
- public struct Iterator: AsyncIteratorProtocol {
- private final class Token {
- private let onDeinit: () -> Void
- init(onDeinit: @escaping () -> Void) {
- self.onDeinit = onDeinit
- }
- deinit {
- onDeinit()
- }
- }
- private var iterator: AsyncStream<Element>.AsyncIterator
- private let token: Token
- init(iterator: AsyncStream<Element>.AsyncIterator, onCancellation: @escaping () -> Void) {
- self.iterator = iterator
- token = Token(onDeinit: onCancellation)
- }
- public mutating func next() async -> Element? {
- await iterator.next()
- }
- }
- }
- #endif
|