File tree Expand file tree Collapse file tree 3 files changed +7
-6
lines changed Expand file tree Collapse file tree 3 files changed +7
-6
lines changed Original file line number Diff line number Diff line change 14
14
15
15
// TODO: remove @testable after async/await API is public
16
16
@testable import AsyncHTTPClient
17
- import NIOCore
18
17
import Foundation
18
+ import NIOCore
19
19
import NIOFoundationCompat
20
20
21
21
#if compiler(>=5.5.2) && canImport(_Concurrency)
Original file line number Diff line number Diff line change @@ -24,13 +24,14 @@ let package = Package(
24
24
] ,
25
25
dependencies: [
26
26
. package ( url: " https://github.com/apple/swift-nio.git " , . branch( " main " ) ) ,
27
-
27
+
28
28
// in real-world projects this would be
29
29
// .package(url: "https://github.com/swift-server/async-http-client.git", from: "1.9.0")
30
30
. package ( name: " async-http-client " , path: " ../ " ) ,
31
31
] ,
32
32
targets: [
33
33
// MARK: - Examples
34
+
34
35
. executableTarget(
35
36
name: " GetHTML " ,
36
37
dependencies: [
Original file line number Diff line number Diff line change @@ -27,20 +27,20 @@ struct StreamingByteCounter {
27
27
let request = HTTPClientRequest ( url: " https://apple.com " )
28
28
let response = try await httpClient. execute ( request, timeout: . seconds( 30 ) )
29
29
print ( " HTTP head " , response)
30
-
30
+
31
31
// if defined, the content-length headers announces the size of the body
32
32
let expectedBytes = response. headers. first ( name: " content-length " ) . flatMap ( Int . init)
33
-
33
+
34
34
var receivedBytes = 0
35
35
// asynchronously iterates over all body fragments
36
36
// this loop will automatically propagate backpressure correctly
37
37
for try await buffer in response. body {
38
38
// For this example, we are just interested in the size of the fragment
39
39
receivedBytes += buffer. readableBytes
40
-
40
+
41
41
if let expectedBytes = expectedBytes {
42
42
// if the body size is known, we calculate a progress indicator
43
- let progress = Double ( receivedBytes) / Double( expectedBytes)
43
+ let progress = Double ( receivedBytes) / Double( expectedBytes)
44
44
print ( " progress: \( Int ( progress * 100 ) ) % " )
45
45
}
46
46
// in case backpressure is needed, all reads will be paused until returned future is resolved
You can’t perform that action at this time.
0 commit comments