@@ -53,7 +53,9 @@ public protocol HTTPClientProtocol: Sendable {
53
53
}
54
54
55
55
extension HTTPClientProtocol {
56
- static func with< Result: Sendable > ( _ body: @Sendable ( any HTTPClientProtocol ) async throws -> Result ) async throws
56
+ static func with< Result: Sendable > (
57
+ _ body: @Sendable ( any HTTPClientProtocol ) async throws -> Result
58
+ ) async throws
57
59
-> Result
58
60
{
59
61
try await self . with ( http1Only: false , body)
@@ -63,107 +65,112 @@ extension HTTPClientProtocol {
63
65
extension FilePath : @unchecked Sendable { }
64
66
65
67
#if canImport(AsyncHTTPClient)
66
- import AsyncHTTPClient
67
-
68
- extension FileDownloadDelegate . Progress : @unchecked Sendable { }
69
-
70
- extension HTTPClient : HTTPClientProtocol {
71
- public static func with< Result: Sendable > (
72
- http1Only: Bool , _ body: @Sendable ( any HTTPClientProtocol ) async throws -> Result
73
- ) async throws -> Result {
74
- var configuration = HTTPClient . Configuration ( redirectConfiguration: . follow( max: 5 , allowCycles: false ) )
75
- if http1Only {
76
- configuration. httpVersion = . http1Only
77
- }
78
- let client = HTTPClient ( eventLoopGroupProvider: . singleton, configuration: configuration)
79
- return try await withAsyncThrowing {
80
- try await body ( client)
81
- } defer: {
82
- try await client. shutdown ( )
68
+ import AsyncHTTPClient
69
+
70
+ extension FileDownloadDelegate . Progress : @unchecked Sendable { }
71
+
72
+ extension HTTPClient : HTTPClientProtocol {
73
+ public static func with< Result: Sendable > (
74
+ http1Only: Bool , _ body: @Sendable ( any HTTPClientProtocol ) async throws -> Result
75
+ ) async throws -> Result {
76
+ var configuration = HTTPClient . Configuration (
77
+ redirectConfiguration: . follow( max: 5 , allowCycles: false ) )
78
+ if http1Only {
79
+ configuration. httpVersion = . http1Only
80
+ }
81
+ let client = HTTPClient ( eventLoopGroupProvider: . singleton, configuration: configuration)
82
+ return try await withAsyncThrowing {
83
+ try await body ( client)
84
+ } defer: {
85
+ try await client. shutdown ( )
86
+ }
83
87
}
84
- }
85
88
86
- public func get( url: String ) async throws -> ( status: NIOHTTP1 . HTTPResponseStatus , body: NIOCore . ByteBuffer ? ) {
87
- let response = try await self . get ( url: url) . get ( )
88
- return ( status: response. status, body: response. body)
89
- }
89
+ public func get( url: String ) async throws -> (
90
+ status: NIOHTTP1 . HTTPResponseStatus , body: NIOCore . ByteBuffer ?
91
+ ) {
92
+ let response = try await self . get ( url: url) . get ( )
93
+ return ( status: response. status, body: response. body)
94
+ }
90
95
91
- public func head( url: String , headers: NIOHTTP1 . HTTPHeaders ) async throws -> Bool {
92
- var headRequest = HTTPClientRequest ( url: url)
93
- headRequest. method = . HEAD
94
- headRequest. headers = [ " Accept " : " */* " , " User-Agent " : " Swift SDK Generator " ]
95
- return try await self . execute ( headRequest, deadline: . distantFuture) . status == . ok
96
- }
96
+ public func head( url: String , headers: NIOHTTP1 . HTTPHeaders ) async throws -> Bool {
97
+ var headRequest = HTTPClientRequest ( url: url)
98
+ headRequest. method = . HEAD
99
+ headRequest. headers = [ " Accept " : " */* " , " User-Agent " : " Swift SDK Generator " ]
100
+ return try await self . execute ( headRequest, deadline: . distantFuture) . status == . ok
101
+ }
97
102
98
- public func downloadFile(
99
- from url: URL ,
100
- to path: FilePath
101
- ) async throws {
102
- try await withCheckedThrowingContinuation { ( continuation: CheckedContinuation < ( ) , Error > ) in
103
- do {
104
- let delegate = try FileDownloadDelegate (
105
- path: path. string,
106
- reportHead: { task, responseHead in
107
- if responseHead. status != . ok {
108
- task. fail ( reason: GeneratorError . fileDownloadFailed ( url, responseHead. status. description) )
103
+ public func downloadFile(
104
+ from url: URL ,
105
+ to path: FilePath
106
+ ) async throws {
107
+ try await withCheckedThrowingContinuation { ( continuation: CheckedContinuation < ( ) , Error > ) in
108
+ do {
109
+ let delegate = try FileDownloadDelegate (
110
+ path: path. string,
111
+ reportHead: { task, responseHead in
112
+ if responseHead. status != . ok {
113
+ task. fail (
114
+ reason: GeneratorError . fileDownloadFailed ( url, responseHead. status. description) )
115
+ }
116
+ }
117
+ )
118
+ let request = try HTTPClient . Request ( url: url)
119
+
120
+ execute ( request: request, delegate: delegate) . futureResult. whenComplete {
121
+ switch $0 {
122
+ case let . failure( error) :
123
+ continuation. resume ( throwing: error)
124
+ case . success:
125
+ continuation. resume ( returning: ( ) )
109
126
}
110
127
}
111
- )
112
- let request = try HTTPClient . Request ( url: url)
113
-
114
- execute ( request: request, delegate: delegate) . futureResult. whenComplete {
115
- switch $0 {
116
- case let . failure( error) :
117
- continuation. resume ( throwing: error)
118
- case . success:
119
- continuation. resume ( returning: ( ) )
120
- }
128
+ } catch {
129
+ continuation. resume ( throwing: error)
121
130
}
122
- } catch {
123
- continuation. resume ( throwing: error)
124
131
}
125
132
}
126
- }
127
133
128
- public func streamDownloadProgress(
129
- from url: URL ,
130
- to path: FilePath
131
- ) -> AsyncThrowingStream < DownloadProgress , any Error > {
132
- . init { continuation in
133
- do {
134
- let delegate = try FileDownloadDelegate (
135
- path: path. string,
136
- reportHead: {
137
- if $0. status != . ok {
138
- continuation
139
- . finish ( throwing: FileOperationError . downloadFailed ( url, $0. status. description) )
134
+ public func streamDownloadProgress(
135
+ from url: URL ,
136
+ to path: FilePath
137
+ ) -> AsyncThrowingStream < DownloadProgress , any Error > {
138
+ . init { continuation in
139
+ do {
140
+ let delegate = try FileDownloadDelegate (
141
+ path: path. string,
142
+ reportHead: {
143
+ if $0. status != . ok {
144
+ continuation
145
+ . finish ( throwing: FileOperationError . downloadFailed ( url, $0. status. description) )
146
+ }
147
+ } ,
148
+ reportProgress: {
149
+ continuation. yield (
150
+ DownloadProgress ( totalBytes: $0. totalBytes, receivedBytes: $0. receivedBytes)
151
+ )
152
+ }
153
+ )
154
+ let request = try HTTPClient . Request ( url: url)
155
+
156
+ execute ( request: request, delegate: delegate) . futureResult. whenComplete {
157
+ switch $0 {
158
+ case let . failure( error) :
159
+ continuation. finish ( throwing: error)
160
+ case let . success( finalProgress) :
161
+ continuation. yield (
162
+ DownloadProgress (
163
+ totalBytes: finalProgress. totalBytes, receivedBytes: finalProgress. receivedBytes)
164
+ )
165
+ continuation. finish ( )
140
166
}
141
- } ,
142
- reportProgress: {
143
- continuation. yield (
144
- DownloadProgress ( totalBytes: $0. totalBytes, receivedBytes: $0. receivedBytes)
145
- )
146
- }
147
- )
148
- let request = try HTTPClient . Request ( url: url)
149
-
150
- execute ( request: request, delegate: delegate) . futureResult. whenComplete {
151
- switch $0 {
152
- case let . failure( error) :
153
- continuation. finish ( throwing: error)
154
- case let . success( finalProgress) :
155
- continuation. yield (
156
- DownloadProgress ( totalBytes: finalProgress. totalBytes, receivedBytes: finalProgress. receivedBytes)
157
- )
158
- continuation. finish ( )
159
167
}
168
+ } catch {
169
+ continuation. finish ( throwing: error)
160
170
}
161
- } catch {
162
- continuation. finish ( throwing: error)
163
171
}
164
172
}
165
173
}
166
- }
167
174
#endif
168
175
169
176
struct OfflineHTTPClient : HTTPClientProtocol {
@@ -189,11 +196,15 @@ struct OfflineHTTPClient: HTTPClientProtocol {
189
196
}
190
197
}
191
198
192
- public func get( url: String ) async throws -> ( status: NIOHTTP1 . HTTPResponseStatus , body: NIOCore . ByteBuffer ? ) {
193
- throw FileOperationError . downloadFailed ( URL ( string: url) !, " Cannot fetch file with offline client " )
199
+ public func get( url: String ) async throws -> (
200
+ status: NIOHTTP1 . HTTPResponseStatus , body: NIOCore . ByteBuffer ?
201
+ ) {
202
+ throw FileOperationError . downloadFailed (
203
+ URL ( string: url) !, " Cannot fetch file with offline client " )
194
204
}
195
205
196
206
public func head( url: String , headers: NIOHTTP1 . HTTPHeaders ) async throws -> Bool {
197
- throw FileOperationError . downloadFailed ( URL ( string: url) !, " Cannot fetch file with offline client " )
207
+ throw FileOperationError . downloadFailed (
208
+ URL ( string: url) !, " Cannot fetch file with offline client " )
198
209
}
199
210
}
0 commit comments