1
1
// NetworkServiceClient+Start.swift
2
2
// NetworkService
3
3
//
4
- // Copyright © 2022 MFB Technologies, Inc. All rights reserved.
4
+ // Copyright © 2023 MFB Technologies, Inc. All rights reserved.
5
5
//
6
6
// This source code is licensed under the MIT license found in the
7
7
// LICENSE file in the root directory of this source tree.
@@ -28,38 +28,34 @@ extension NetworkServiceClient {
28
28
29
29
private func response( _ request: URLRequest ) async throws -> ( Data , URLResponse ) {
30
30
let session = getSession ( )
31
- if #available( iOS 15 , watchOS 8 , macOS 12 , macCatalyst 15 , tvOS 15 , * ) {
32
- return try await session. data ( for: request)
33
- } else {
34
- var task : URLSessionDataTask ?
35
- var shouldCancel : Bool = false
36
- let onCancel = {
37
- if let task = task {
38
- task. cancel ( )
39
- } else {
40
- shouldCancel = true
41
- }
31
+ var task : URLSessionDataTask ?
32
+ var shouldCancel : Bool = false
33
+ let onCancel = {
34
+ if let task = task {
35
+ task. cancel ( )
36
+ } else {
37
+ shouldCancel = true
42
38
}
43
- return try await withTaskCancellationHandler (
44
- handler: { onCancel ( ) } ,
45
- operation: {
46
- try await withCheckedThrowingContinuation { continuation in
47
- task = session. dataTask ( with: request, completionHandler: { _data, _urlResponse, _error in
48
- guard let data = _data, let urlResponse = _urlResponse else {
49
- return continuation. resume ( throwing: _error ?? URLError ( . badServerResponse) )
50
- }
51
- continuation. resume ( returning: ( data, urlResponse) )
52
- } )
53
-
54
- if shouldCancel {
55
- task? . cancel ( )
56
- } else {
57
- task? . resume ( )
39
+ }
40
+ return try await withTaskCancellationHandler (
41
+ handler: { onCancel ( ) } ,
42
+ operation: {
43
+ try await withCheckedThrowingContinuation { continuation in
44
+ task = session. dataTask ( with: request, completionHandler: { _data, _urlResponse, _error in
45
+ guard let data = _data, let urlResponse = _urlResponse else {
46
+ return continuation. resume ( throwing: _error ?? URLError ( . badServerResponse) )
58
47
}
48
+ continuation. resume ( returning: ( data, urlResponse) )
49
+ } )
50
+
51
+ if shouldCancel {
52
+ task? . cancel ( )
53
+ } else {
54
+ task? . resume ( )
59
55
}
60
56
}
61
- )
62
- }
57
+ }
58
+ )
63
59
}
64
60
}
65
61
0 commit comments