Skip to content

Commit 6e974af

Browse files
committed
fix review comments
1 parent 7989a97 commit 6e974af

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

README.md

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ You should always shut down `HTTPClient` instances you created using `try httpCl
7474
## Usage guide
7575

7676
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+
7778
#### Using Swift Concurrency
79+
7880
```swift
7981
import AsyncHTTPClient
8082

@@ -99,6 +101,7 @@ try await httpClient.shutdown()
99101
```
100102

101103
#### Using SwiftNIO EventLoopFuture
104+
102105
```swift
103106
import AsyncHTTPClient
104107

@@ -151,28 +154,33 @@ The following example demonstrates how to count the number of bytes in a streami
151154
#### Using Swift Concurrency
152155
```swift
153156
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+
}
172177
}
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)
174181
}
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()
176184
```
177185

178186
#### Using HTTPClientResponseDelegate and SwiftNIO EventLoopFuture

0 commit comments

Comments
 (0)