Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Add async/await proposal #406

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions docs/async-await.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ If the library code throws from the `HTTPClientRequest` creation or the request
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.

```swift
public struct Body {
static func bytes<S: Sequence>(_ sequence: S) -> Body where S.Element == UInt8

static func stream<S: AsyncSequence>(_ sequence: S) -> Body where S.Element == ByteBuffer

static func stream<S: AsyncSequence>(_ sequence: S) -> Body where S.Element == UInt8
extension HTTPClientRequest {
public struct Body {
fabianfett marked this conversation as resolved.
Show resolved Hide resolved
static func bytes<S: Sequence>(_ sequence: S) -> Body where S.Element == UInt8

static func stream<S: AsyncSequence>(_ sequence: S) -> Body where S.Element == ByteBuffer

static func stream<S: AsyncSequence>(_ sequence: S) -> Body where S.Element == UInt8
}
}
```

Expand Down Expand Up @@ -145,6 +147,27 @@ extension HTTPClient {
}
```

Usage example:

```swift
var request = HTTPClientRequest(url: "https://swift.org")
request.method = .POST
request.headers = [
"content-type": "text/plain; charset=UTF-8"
"x-my-fancy-header": "super-awesome"
]
request.body = .sequence("Hello world!".utf8)

var response = try await client.execute(request, deadline: .now() + .seconds(5))

switch response.status {
case .ok:
let body = try await response.body.collect(maxBytes: 1024 * 1024)
default:
throw MyUnexpectedHTTPStatusError
}
```

- **Why do we have a deadline in the function signature?**
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we can't commit to making them happen for Swift 6, it's a clear and important thing we want to support in swift concurrency. So I agree not doing another "our own" thing until then is the right call here.

- **What happened to the Logger?** We will use Task locals to propagate the logger metadata. @slashmo and @ktoso are currently working on this.
Expand Down