@@ -74,7 +74,9 @@ You should always shut down `HTTPClient` instances you created using `try httpCl
74
74
## Usage guide
75
75
76
76
The default HTTP Method is ` GET ` . In case you need to have more control over the method, or you want to add headers or body, use the ` HTTPClientRequest ` struct:
77
+
77
78
#### Using Swift Concurrency
79
+
78
80
``` swift
79
81
import AsyncHTTPClient
80
82
@@ -99,6 +101,7 @@ try await httpClient.shutdown()
99
101
```
100
102
101
103
#### Using SwiftNIO EventLoopFuture
104
+
102
105
``` swift
103
106
import AsyncHTTPClient
104
107
@@ -151,28 +154,33 @@ The following example demonstrates how to count the number of bytes in a streami
151
154
#### Using Swift Concurrency
152
155
``` swift
153
156
let httpClient = HTTPClient (eventLoopGroupProvider : .createNew )
154
-
155
- let request = HTTPClientRequest (url : " https://apple.com/" )
156
- let response = try await httpClient.execute (request, timeout : .seconds (30 ))
157
- print (" HTTP head" , response)
158
-
159
- // if defined, the content-length headers announces the size of the body
160
- let expectedBytes = response.headers .first (name : " content-length" ).flatMap (Int .init )
161
-
162
- var receivedBytes = 0
163
- // asynchronously iterates over all body fragments.
164
- for try await buffer in response.body {
165
- // For this example, we are just interested in the size of the fragment
166
- receivedBytes += buffer.readableBytes
167
-
168
- if let expectedBytes = expectedBytes {
169
- // if the body size is known, we calculate a progress indicator
170
- let progress = Double (receivedBytes)/ Double (expectedBytes)
171
- print (" progress: \( Int (progress * 100 )) %" )
157
+ do {
158
+ let request = HTTPClientRequest (url : " https://apple.com/" )
159
+ let response = try await httpClient.execute (request, timeout : .seconds (30 ))
160
+ print (" HTTP head" , response)
161
+
162
+ // if defined, the content-length headers announces the size of the body
163
+ let expectedBytes = response.headers .first (name : " content-length" ).flatMap (Int .init )
164
+
165
+ var receivedBytes = 0
166
+ // asynchronously iterates over all body fragments
167
+ // this loop will automatically propagate backpressure correctly
168
+ for try await buffer in response.body {
169
+ // for this example, we are just interested in the size of the fragment
170
+ receivedBytes += buffer.readableBytes
171
+
172
+ if let expectedBytes = expectedBytes {
173
+ // if the body size is known, we calculate a progress indicator
174
+ let progress = Double (receivedBytes)/ Double (expectedBytes)
175
+ print (" progress: \( Int (progress * 100 )) %" )
176
+ }
172
177
}
173
- // in case backpressure is needed, all reads will be paused until the end of the for loop.
178
+ print (" did receive \( receivedBytes ) bytes" )
179
+ } catch {
180
+ print (" request failed:" , error)
174
181
}
175
- print (" did receive \( receivedBytes ) bytes" )
182
+ // it is important to shutdown the httpClient after all requests are done, even if one failed
183
+ try await httpClient.shutdown ()
176
184
```
177
185
178
186
#### Using HTTPClientResponseDelegate and SwiftNIO EventLoopFuture
0 commit comments