Skip to content

Addressing README.md code snippets as per issue#69 #70

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

Merged
merged 2 commits into from
Jul 17, 2019
Merged
Changes from all commits
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
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ httpClient.get(url: "https://swift.org").whenComplete { result in
case .failure(let error):
// process error
case .success(let response):
if let response.status == .ok {
if response.status == .ok {
// handle response
} else {
// handle remote error
Expand Down Expand Up @@ -69,7 +69,7 @@ defer {
try? httpClient.syncShutdown()
}

var request = try HTTPClient.HTTPRequest(url: "https://swift.org", method: .POST)
var request = try HTTPClient.Request(url: "https://swift.org", method: .POST)
request.headers.add(name: "User-Agent", value: "Swift HTTPClient")
request.body = .string("some-body")

Expand All @@ -78,7 +78,7 @@ httpClient.execute(request: request).whenComplete { result in
case .failure(let error):
// process error
case .success(let response):
if let response.status == .ok {
if response.status == .ok {
// handle response
} else {
// handle remote error
Expand Down Expand Up @@ -110,6 +110,9 @@ httpClient.execute(request: request, timeout: timeout)
### Streaming
When dealing with larger amount of data, it's critical to stream the response body instead of aggregating in-memory. Handling a response stream is done using a delegate protocol. The following example demonstrates how to count the number of bytes in a streaming response body:
```swift
import NIO
import NIOHTTP1

class CountingDelegate: HTTPClientResponseDelegate {
typealias Response = Int

Expand Down Expand Up @@ -154,7 +157,7 @@ class CountingDelegate: HTTPClientResponseDelegate {
let request = try HTTPClient.Request(url: "https://swift.org")
let delegate = CountingDelegate()

try httpClient.execute(request: request, delegate: delegate).future.whenSuccess { count in
httpClient.execute(request: request, delegate: delegate).futureResult.whenSuccess { count in
print(count)
}
```