-
Notifications
You must be signed in to change notification settings - Fork 125
[RFC] Add async/await proposal #406
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7ec050b
Add async/await proposal
fabianfett e2f078e
PR review
fabianfett f96567c
Update docs/async-await.md
fabianfett 110ab5e
HTTPClientRequest & HTTPClientResponse
fabianfett 992f006
Added a ful usage example and made clear that request Body is namespa…
fabianfett 22d8b45
Add FAQ about convenience APIs
fabianfett 9687247
Added a FileHandle stream example
fabianfett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
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 | ||
} | ||
} | ||
``` | ||
|
||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.