|
1 | 1 | # swift-nio-http-client
|
| 2 | +This package provides simple HTTP Client library built on top of SwiftNIO. |
2 | 3 |
|
3 |
| -Swift HTTP Client library built on top of SwiftNIO |
| 4 | +## Getting Started |
| 5 | + |
| 6 | +Add the following entry in your <code>Package.swift</code> to start using <code>HTTPClient</code>: |
| 7 | + |
| 8 | +### Swift 5 |
| 9 | + |
| 10 | +```swift |
| 11 | +dependencies: [ |
| 12 | + .package(url: "https://github.com/swift-server/swift-nio-http-client.git", from: "1.0.0") |
| 13 | +] |
| 14 | +``` |
| 15 | + |
| 16 | +## Status |
| 17 | + |
| 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) |
| 24 | + |
| 25 | +## How to use |
| 26 | + |
| 27 | +### Request-Response API |
| 28 | +The code snippet below illustrates how to make a simple GET request to a remote server: |
| 29 | + |
| 30 | +```import HTTPClient |
| 31 | +
|
| 32 | +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 |
| 37 | +} |
| 38 | +
|
| 39 | +// close the client |
| 40 | +try? httpClient.syncShutdown() |
| 41 | +``` |
| 42 | + |
| 43 | +It is important to close client instance after use to cleanly shutdown underlying NIO ```EventLoopGroup```. Alternatively, you can provide shared ```EventLoopGroup```: |
| 44 | +``` |
| 45 | +let httpClient = HTTPClient(eventLoopGroupProvider: .shared(userProvidedGroup)) |
| 46 | +``` |
| 47 | +In this case shutdown of the client is not neccecary. |
| 48 | + |
| 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 |
| 51 | +
|
| 52 | +let httpClient = HTTPClient(eventLoopGroupProvider: .createNew) |
| 53 | +defer { |
| 54 | + try? httpClient.syncShutdown() |
| 55 | +} |
| 56 | +
|
| 57 | +var request = try HTTPRequest(url: "https://swift.org", method: .POST) |
| 58 | +request.headers.add(name: "User-Agent", value: "Swift HTTPClient") |
| 59 | +request.body = .string("some-body") |
| 60 | +
|
| 61 | +let response = try httpClient.execute(request: request).wait() |
| 62 | +
|
| 63 | +if response.status == .ok { |
| 64 | + // handle the response |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### Redirect following |
| 69 | +To enable follow-redirects behaviour, enable in using client configuration: |
| 70 | +``` |
| 71 | +let httpClient = HTTPClient(eventLoopGroupProvider: .createNew, |
| 72 | + configuration: HTTPClientConfiguration(followRedirects: true)) |
| 73 | +``` |
| 74 | + |
| 75 | +### Timeouts |
| 76 | +Timeouts (connect and read) can be set in the client configuration: |
| 77 | +``` |
| 78 | +let httpClient = HTTPClient(eventLoopGroupProvider: .createNew, |
| 79 | + configuration: HTTPClientConfiguration(timeout: Timeout(connectTimeout: .seconds(1), |
| 80 | + readTimeout: .seconds(1)))) |
| 81 | +``` |
| 82 | +or on per-request basis: |
| 83 | +``` |
| 84 | +let response = try httpClient.execute(request: request, timeout: Timeout(connectTimeout: .seconds(1), readTimeout: .seconds(1))).wait() |
| 85 | +``` |
| 86 | + |
| 87 | +### 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 | +``` |
| 90 | +class CountingDelegate: HTTPResponseDelegate { |
| 91 | + typealias Response = Int |
| 92 | +
|
| 93 | + var count = 0 |
| 94 | + |
| 95 | + func didTransmitRequestBody() { |
| 96 | + // this is executed when request is sent, called once |
| 97 | + } |
| 98 | +
|
| 99 | + func didReceiveHead(_ head: HTTPResponseHead) { |
| 100 | + // this is executed when we receive HTTP Reponse head part of the request (it contains response code and headers), called once |
| 101 | + } |
| 102 | +
|
| 103 | + func didReceivePart(_ buffer: ByteBuffer) { |
| 104 | + // this is executed when we receive parts of the response body, could be called zero or more times |
| 105 | + count += buffer.readableBytes |
| 106 | + } |
| 107 | +
|
| 108 | + func didFinishRequest() throws -> Int { |
| 109 | + // this is called when request is fully read, called once, this is where you return a result or throw any errors you require to propagate to the client |
| 110 | + return count |
| 111 | + } |
| 112 | + |
| 113 | + func didReceiveError(_ error: Error) { |
| 114 | + // this is called when we receive any network-related error, called once |
| 115 | + } |
| 116 | +} |
| 117 | +
|
| 118 | +let request = try HTTPRequest(url: "https://swift.org") |
| 119 | +let delegate = CountingDelegate() |
| 120 | +
|
| 121 | +let count = try httpClient.execute(request: request, delegate: delegate).wait() |
| 122 | +``` |
0 commit comments