Skip to content

Commit 9bdd6ef

Browse files
committed
updated extensions
1 parent 414d01d commit 9bdd6ef

File tree

4 files changed

+42
-23
lines changed

4 files changed

+42
-23
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the AsyncHTTPClient open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import NIOCore
16+
import NIOHTTP1
17+
import Foundation
18+
internal import NIOFoundationCompat
19+
20+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
21+
extension HTTPClientResponse {
22+
/// Response body as `ByteBuffer`.
23+
/// - Parameter maxBytes: The maximum number of bytes this method is allowed to accumulate.
24+
/// - Returns: Bytes collected over time
25+
public func bytes(upTo maxBytes: Int) async throws -> ByteBuffer {
26+
let expectedBytes = self.headers.first(name: "content-length").flatMap(Int.init) ?? maxBytes
27+
return try await self.body.collect(upTo: min(expectedBytes, maxBytes))
28+
}
29+
}
30+
31+
32+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
33+
extension HTTPClientResponse {
34+
/// Response body as `Data`.
35+
/// - Parameter maxBytes: The maximum number of bytes this method is allowed to accumulate.
36+
/// - Returns: Bytes collected over time
37+
public func data(upTo maxBytes: Int) async throws -> Data? {
38+
var bytes = try await self.bytes(upTo: maxBytes)
39+
return bytes.readData(length: bytes.readableBytes)
40+
}
41+
}

Sources/AsyncHTTPClient/AsyncAwait/HTTPClientResponse.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,6 @@ extension HTTPClientResponse {
187187
}
188188
}
189189

190-
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
191-
extension HTTPClientResponse {
192-
/// Response body as `ByteBuffer`.
193-
/// - Parameter maxBytes: The maximum number of bytes this method is allowed to accumulate.
194-
/// - Returns: Bytes collected over time
195-
public func bytes(upTo maxBytes: Int) async throws -> ByteBuffer {
196-
let expectedBytes = self.headers.first(name: "content-length").flatMap(Int.init) ?? maxBytes
197-
return try await self.body.collect(upTo: expectedBytes)
198-
}
199-
}
200-
201190
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
202191
@usableFromInline
203192
typealias TransactionBody = NIOThrowingAsyncSequenceProducer<

Sources/AsyncHTTPClient/FoundationExtensions.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,3 @@ extension HTTPClient.Body {
7373
self.bytes(data)
7474
}
7575
}
76-
77-
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
78-
extension HTTPClientResponse {
79-
/// Response body as `Data`.
80-
/// - Parameter maxBytes: The maximum number of bytes this method is allowed to accumulate.
81-
/// - Returns: Bytes collected over time
82-
public func data(upTo maxBytes: Int) async throws -> Data? {
83-
var bytes = try await self.bytes(upTo: maxBytes)
84-
return bytes.readData(length: bytes.readableBytes)
85-
}
86-
}

Tests/AsyncHTTPClientTests/AsyncAwaitEndToEndTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ final class AsyncAwaitEndToEndTests: XCTestCase {
10361036
) else { return }
10371037
XCTAssertEqual(response.headers["content-length"], ["4"])
10381038
guard let body = await XCTAssertNoThrowWithResult(
1039-
try await response.bytes(upTo: 3)
1039+
try await response.bytes(upTo: 5)
10401040
) else { return }
10411041
XCTAssertEqual(body, ByteBuffer(string: "1234"))
10421042

0 commit comments

Comments
 (0)