You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+67-43
Original file line number
Diff line number
Diff line change
@@ -1,53 +1,69 @@
1
1
# swift-nio-http-client
2
2
This package provides simple HTTP Client library built on top of SwiftNIO.
3
3
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)
5
10
6
-
Add the following entry in your <code>Package.swift</code> to start using <code>HTTPClient</code>:
11
+
---
7
12
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`.
The code snippet below illustrates how to make a simple GET request to a remote server:
29
33
30
-
```import HTTPClient
34
+
```swift
35
+
importHTTPClient
31
36
32
37
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
+
iflet response.status == .ok {
44
+
// handle response
45
+
} else {
46
+
// handle remote error
47
+
}
48
+
}
37
49
}
38
-
39
-
// close the client
40
-
try? httpClient.syncShutdown()
41
50
```
42
51
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()
44
55
```
56
+
Alternatively, you can provide shared ```EventLoopGroup```:
57
+
```swift
45
58
let httpClient =HTTPClient(eventLoopGroupProvider: .shared(userProvidedGroup))
46
59
```
47
60
In this case shutdown of the client is not neccecary.
48
61
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
+
importHTTPClient
51
67
52
68
let httpClient =HTTPClient(eventLoopGroupProvider: .createNew)
let timeout =Timeout(connectTimeout: .seconds(1), readTimeout: .seconds(1))
108
+
let response =try httpClient.execute(request: request, timeout: timeout).wait()
85
109
```
86
110
87
111
### 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:
0 commit comments