-
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
Changes from 1 commit
7ec050b
e2f078e
f96567c
110ab5e
992f006
22d8b45
9687247
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,7 +147,7 @@ extension HTTPClient { | |
} | ||
``` | ||
|
||
Usage example: | ||
Simple usage example: | ||
|
||
```swift | ||
var request = HTTPClientRequest(url: "https://swift.org") | ||
|
@@ -168,6 +168,40 @@ default: | |
} | ||
``` | ||
|
||
Stream upload and download example using the new `FileHandle` api: | ||
|
||
```swift | ||
let readHandle = FileHandle(forReadingFrom: URL(string: "file:///tmp/readfile")!)) | ||
let writeHandle = FileHandle(forWritingTo: URL(string: "file:///tmp/writefile")!)) | ||
|
||
var request = HTTPClientRequest(url: "https://swift.org/echo") | ||
request.method = .POST | ||
request.headers = [ | ||
"content-type": "text/plain; charset=UTF-8" | ||
"x-my-fancy-header": "super-awesome" | ||
] | ||
request.body = .stream(readHandle.bytes) | ||
|
||
var response = try await client.execute(request, deadline: .now() + .seconds(5)) | ||
|
||
switch response.status { | ||
case .ok: | ||
Task { | ||
var streamIterator = response.body.makeAsyncIterator() | ||
writeHandle.writeabilityHandler = { handle in | ||
switch try await streamIterator.next() { | ||
case .some(let buffer): | ||
handle.write(buffer.readData(buffer.readableBytes)!) | ||
case .none: | ||
handle.close() | ||
} | ||
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. I suppose we can use 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. In general, yes because |
||
} | ||
} | ||
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. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"new" where, in http client?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think what this refers to is the new async APIs on Foundation's
FileHandle
type.See: https://developer.apple.com/documentation/foundation/filehandle/3766681-bytes