|
13 | 13 | import Foundation |
14 | 14 | import NIOCore |
15 | 15 | import NIOHTTP1 |
16 | | - import NIOFoundationCompat |
17 | 16 |
|
18 | 17 | extension HTTPClient { |
19 | 18 | func fetch<T: Decodable>( |
|
32 | 31 | } |
33 | 32 |
|
34 | 33 | if let body { |
35 | | - request.body = .bytes(ByteBuffer(data: body)) |
| 34 | + request.body = .bytes(ByteBuffer(bytes: body)) |
36 | 35 | request.headers.add(name: "Content-Type", value: "application/json") |
37 | 36 | } |
38 | 37 |
|
39 | 38 | let response = try await self.execute(request, timeout: .seconds(180)) |
40 | 39 |
|
41 | 40 | guard (200 ..< 300).contains(response.status.code) else { |
42 | | - let bodyData = try await Data(buffer: response.body.collect(upTo: 1024 * 1024)) |
| 41 | + let bodyData = try await Data(response.body.collect(upTo: 1024 * 1024).readableBytesView) |
43 | 42 | if let errorString = String(data: bodyData, encoding: .utf8) { |
44 | 43 | throw HTTPClientError.httpError(statusCode: Int(response.status.code), detail: errorString) |
45 | 44 | } |
46 | 45 | throw HTTPClientError.httpError(statusCode: Int(response.status.code), detail: "Invalid response") |
47 | 46 | } |
48 | 47 |
|
49 | | - let bodyData = try await Data(buffer: response.body.collect(upTo: 1024 * 1024)) |
| 48 | + let bodyData = try await Data(response.body.collect(upTo: 1024 * 1024).readableBytesView) |
50 | 49 |
|
51 | 50 | let decoder = JSONDecoder() |
52 | 51 | decoder.dateDecodingStrategy = dateDecodingStrategy |
|
80 | 79 | } |
81 | 80 |
|
82 | 81 | if let body { |
83 | | - request.body = .bytes(ByteBuffer(data: body)) |
| 82 | + request.body = .bytes(ByteBuffer(bytes: body)) |
84 | 83 | request.headers.add(name: "Content-Type", value: "application/json") |
85 | 84 | } |
86 | 85 |
|
87 | 86 | let response = try await self.execute(request, timeout: .seconds(60)) |
88 | 87 |
|
89 | 88 | guard (200 ..< 300).contains(response.status.code) else { |
90 | | - let bodyData = try await Data(buffer: response.body.collect(upTo: 1024 * 1024)) |
| 89 | + let bodyData = try await Data(response.body.collect(upTo: 1024 * 1024).readableBytesView) |
91 | 90 | if let errorString = String(data: bodyData, encoding: .utf8) { |
92 | 91 | throw HTTPClientError.httpError( |
93 | 92 | statusCode: Int(response.status.code), |
|
149 | 148 | } |
150 | 149 |
|
151 | 150 | if let body { |
152 | | - request.body = .bytes(ByteBuffer(data: body)) |
| 151 | + request.body = .bytes(ByteBuffer(bytes: body)) |
153 | 152 | request.headers.add(name: "Content-Type", value: "application/json") |
154 | 153 | } |
155 | 154 |
|
156 | 155 | let response = try await self.execute(request, timeout: .seconds(60)) |
157 | 156 |
|
158 | 157 | guard (200 ..< 300).contains(response.status.code) else { |
159 | | - let bodyData = try await Data(buffer: response.body.collect(upTo: 1024 * 1024)) |
| 158 | + let bodyData = try await Data(response.body.collect(upTo: 1024 * 1024).readableBytesView) |
160 | 159 | if let errorString = String(data: bodyData, encoding: .utf8) { |
161 | 160 | throw HTTPClientError.httpError( |
162 | 161 | statusCode: Int(response.status.code), |
|
0 commit comments