Skip to content

Commit 3785391

Browse files
authored
review fixes
1 parent cee4bef commit 3785391

File tree

1 file changed

+67
-43
lines changed

1 file changed

+67
-43
lines changed

Diff for: README.md

+67-43
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,69 @@
11
# swift-nio-http-client
22
This package provides simple HTTP Client library built on top of SwiftNIO.
33

4-
## Getting Started
4+
This library provides the following:
5+
1. Asynchronous and non-blocking request methods
6+
2. Simple follow-redirects (cookie headers are dropped)
7+
3. Streaming body download
8+
4. TLS support
9+
5. Cookie parsing (but not storage)
510

6-
Add the following entry in your <code>Package.swift</code> to start using <code>HTTPClient</code>:
11+
---
712

8-
### Swift 5
13+
**NOTE**: You will need [Xcode 10.2](https://itunes.apple.com/us/app/xcode/id497799835) or [Swift 5.0](https://swift.org/download/#swift-50) to try out `swift-nio-htt-client`.
914

10-
```swift
11-
dependencies: [
12-
.package(url: "https://github.com/swift-server/swift-nio-http-client.git", from: "1.0.0")
13-
]
14-
```
15+
---
1516

16-
## Status
17+
## Getting Started
1718

18-
This library provides the following:
19-
1. Async single request methods
20-
2. Simple follow-redirect support (cookie headers are dropped)
21-
3. Body download streaming
22-
4. TLS support
23-
5. Cookie parsing (but not storage)
19+
#### Adding the dependency
20+
Add the following entry in your <code>Package.swift</code> to start using <code>HTTPClient</code>:
2421

25-
## How to use
22+
```swift
23+
// it's early days here so we haven't tagged a version yet, but will soon
24+
.package(url: "https://github.com/swift-server/swift-nio-http-client.git", .branch("master"))
25+
```
26+
and ```SwiftNIOHTTP``` dependency to your target:
27+
```swift
28+
.target(name: "MyApp", dependencies: ["SwiftNIOHTTP"]),
29+
```
2630

27-
### Request-Response API
31+
#### Request-Response API
2832
The code snippet below illustrates how to make a simple GET request to a remote server:
2933

30-
```import HTTPClient
34+
```swift
35+
import HTTPClient
3136

3237
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
33-
let response = try httpClient.get(url: "https://swift.org").wait()
34-
35-
if response.status == .ok {
36-
// handle the response
38+
httpClient.get(url: "https://swift.org").whenComplete { result in
39+
switch result {
40+
case .failure(let error):
41+
// process error
42+
case .success(let response):
43+
if let response.status == .ok {
44+
// handle response
45+
} else {
46+
// handle remote error
47+
}
48+
}
3749
}
38-
39-
// close the client
40-
try? httpClient.syncShutdown()
4150
```
4251

43-
It is important to close client instance after use to cleanly shutdown underlying NIO ```EventLoopGroup```. Alternatively, you can provide shared ```EventLoopGroup```:
52+
It is important to close client instance after use to cleanly shutdown underlying NIO ```EventLoopGroup```:
53+
```
54+
try? httpClient.syncShutdown()
4455
```
56+
Alternatively, you can provide shared ```EventLoopGroup```:
57+
```swift
4558
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(userProvidedGroup))
4659
```
4760
In this case shutdown of the client is not neccecary.
4861

49-
Library provides methods for most HTTP-methods. In case you need to have more control over the method, or you want to add headers or body, use ```HTTPRequest``` struct:
50-
```import HTTPClient
62+
## Usage guide
63+
64+
Most common HTTP methods are supported out of the box. In case you need to have more control over the method, or you want to add headers or body, use ```HTTPRequest``` struct:
65+
```swift
66+
import HTTPClient
5167

5268
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
5369
defer {
@@ -58,35 +74,43 @@ var request = try HTTPRequest(url: "https://swift.org", method: .POST)
5874
request.headers.add(name: "User-Agent", value: "Swift HTTPClient")
5975
request.body = .string("some-body")
6076

61-
let response = try httpClient.execute(request: request).wait()
62-
63-
if response.status == .ok {
64-
// handle the response
77+
httpClient.execute(request: request).whenComplete { result in
78+
switch result {
79+
case .failure(let error):
80+
// process error
81+
case .success(let response):
82+
if let response.status == .ok {
83+
// handle response
84+
} else {
85+
// handle remote error
86+
}
87+
}
6588
}
6689
```
6790

68-
### Redirect following
69-
To enable follow-redirects behaviour, enable in using client configuration:
70-
```
91+
### Redirects following
92+
Enable follow-redirects behavior using the client configuration:
93+
```swift
7194
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew,
7295
configuration: HTTPClientConfiguration(followRedirects: true))
7396
```
7497

7598
### Timeouts
76-
Timeouts (connect and read) can be set in the client configuration:
77-
```
99+
Timeouts (connect and read) can also be set using the client configuration:
100+
```swift
101+
let timeout = Timeout(connectTimeout: .seconds(1), readTimeout: .seconds(1))
78102
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew,
79-
configuration: HTTPClientConfiguration(timeout: Timeout(connectTimeout: .seconds(1),
80-
readTimeout: .seconds(1))))
103+
configuration: HTTPClientConfiguration(timeout: timeout))
81104
```
82105
or on per-request basis:
83-
```
84-
let response = try httpClient.execute(request: request, timeout: Timeout(connectTimeout: .seconds(1), readTimeout: .seconds(1))).wait()
106+
```swift
107+
let timeout = Timeout(connectTimeout: .seconds(1), readTimeout: .seconds(1))
108+
let response = try httpClient.execute(request: request, timeout: timeout).wait()
85109
```
86110

87111
### Streaming
88-
In case greater control over body processing is needed or you want to process HTTP Reponse body in a streaming manner, following delegate protocol could be used (example shows how to count bytes in response body without copiying it to the memory):
89-
```
112+
When dealing with larger amount of data, it's critical to steam the response body instead of aggregating it-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:
113+
```swift
90114
class CountingDelegate: HTTPResponseDelegate {
91115
typealias Response = Int
92116

0 commit comments

Comments
 (0)