Skip to content

Commit 992f006

Browse files
committedAug 17, 2021
Added a ful usage example and made clear that request Body is namespaced in HTTPClientRequest
1 parent 110ab5e commit 992f006

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed
 

‎docs/async-await.md

+29-6
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ If the library code throws from the `HTTPClientRequest` creation or the request
6565
The new `HTTPClientRequest` has a new body type, that is wrapper around an internal enum. This allows us to evolve this type for use-cases that we are not aware of today.
6666

6767
```swift
68-
public struct Body {
69-
static func bytes<S: Sequence>(_ sequence: S) -> Body where S.Element == UInt8
70-
71-
static func stream<S: AsyncSequence>(_ sequence: S) -> Body where S.Element == ByteBuffer
72-
73-
static func stream<S: AsyncSequence>(_ sequence: S) -> Body where S.Element == UInt8
68+
extension HTTPClientRequest {
69+
public struct Body {
70+
static func bytes<S: Sequence>(_ sequence: S) -> Body where S.Element == UInt8
71+
72+
static func stream<S: AsyncSequence>(_ sequence: S) -> Body where S.Element == ByteBuffer
73+
74+
static func stream<S: AsyncSequence>(_ sequence: S) -> Body where S.Element == UInt8
75+
}
7476
}
7577
```
7678

@@ -145,6 +147,27 @@ extension HTTPClient {
145147
}
146148
```
147149

150+
Usage example:
151+
152+
```swift
153+
var request = HTTPClientRequest(url: "https://swift.org")
154+
request.method = .POST
155+
request.headers = [
156+
"content-type": "text/plain; charset=UTF-8"
157+
"x-my-fancy-header": "super-awesome"
158+
]
159+
request.body = .sequence("Hello world!".utf8)
160+
161+
var response = try await client.execute(request, deadline: .now() + .seconds(5))
162+
163+
switch response.status {
164+
case .ok:
165+
let body = try await response.body.collect(maxBytes: 1024 * 1024)
166+
default:
167+
throw MyUnexpectedHTTPStatusError
168+
}
169+
```
170+
148171
- **Why do we have a deadline in the function signature?**
149172
Task deadlines are not part of the Swift 5.5 release. However we think that they are an important tool to not overload the http client accidentally. For this reason we will not default them.
150173
- **What happened to the Logger?** We will use Task locals to propagate the logger metadata. @slashmo and @ktoso are currently working on this.

0 commit comments

Comments
 (0)