Skip to content

Commit 3329370

Browse files
committed
HTTPClientRequest
1 parent 99bd384 commit 3329370

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
#if compiler(>=5.5) && canImport(_Concurrency)
16+
import NIOCore
17+
18+
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
19+
extension HTTPClientRequest {
20+
struct Body {
21+
internal enum Mode {
22+
case asyncSequence(length: Int?, (ByteBufferAllocator) async throws -> ByteBuffer?)
23+
case sequence(length: Int?, (ByteBufferAllocator) -> ByteBuffer)
24+
case byteBuffer(ByteBuffer)
25+
}
26+
27+
var mode: Mode
28+
29+
private init(_ mode: Mode) {
30+
self.mode = mode
31+
}
32+
}
33+
}
34+
35+
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
36+
extension HTTPClientRequest.Body {
37+
static func byteBuffer(_ byteBuffer: ByteBuffer) -> Self {
38+
self.init(.byteBuffer(byteBuffer))
39+
}
40+
41+
static func bytes<Bytes>(
42+
length: Int? = nil,
43+
_ bytes: Bytes
44+
) -> Self where Bytes: Sequence, Bytes.Element == UInt8 {
45+
self.init(.sequence(length: length) { allocator in
46+
if let buffer = bytes.withContiguousStorageIfAvailable({ allocator.buffer(bytes: $0) }) {
47+
// fastpath
48+
return buffer
49+
}
50+
// potentially really slow path
51+
return allocator.buffer(bytes: bytes)
52+
})
53+
}
54+
55+
static func bytes<Bytes>(
56+
_ bytes: Bytes
57+
) -> Self where Bytes: RandomAccessCollection, Bytes.Element == UInt8 {
58+
self.init(.sequence(length: bytes.count) { allocator in
59+
if let buffer = bytes.withContiguousStorageIfAvailable({ allocator.buffer(bytes: $0) }) {
60+
// fastpath
61+
return buffer
62+
}
63+
// potentially really slow path
64+
return allocator.buffer(bytes: bytes)
65+
})
66+
}
67+
68+
/// This method should never be used and was always deprecated.
69+
/// The whole purpose of this overload is to prevent users from providing a redundant length if `Bytes` conforms to
70+
/// `RandomAccessCollection` because it already provide a property `count` to get the length in O(**1**).
71+
/// - Note: `length` is ignored in favour of `bytes.count`
72+
@available(*, deprecated, message: "no need to manually specify `length` because we automatically use `bytes.count` as the `length`")
73+
static func bytes<Bytes>(
74+
length: Int,
75+
_ collection: Bytes
76+
) -> Self where Bytes: RandomAccessCollection, Bytes.Element == UInt8 {
77+
return .bytes(collection)
78+
}
79+
80+
static func stream<SequenceOfBytes>(
81+
length: Int? = nil,
82+
_ sequenceOfBytes: SequenceOfBytes
83+
) -> Self where SequenceOfBytes: AsyncSequence, SequenceOfBytes.Element == ByteBuffer {
84+
var iterator = sequenceOfBytes.makeAsyncIterator()
85+
let body = self.init(.asyncSequence(length: length) { _ -> ByteBuffer? in
86+
try await iterator.next()
87+
})
88+
return body
89+
}
90+
91+
static func stream<Bytes>(
92+
length: Int? = nil,
93+
_ bytes: Bytes
94+
) -> Self where Bytes: AsyncSequence, Bytes.Element == UInt8 {
95+
var iterator = bytes.makeAsyncIterator()
96+
let body = self.init(.asyncSequence(length: nil) { allocator -> ByteBuffer? in
97+
var buffer = allocator.buffer(capacity: 1024) // TODO: Magic number
98+
while buffer.writableBytes > 0, let byte = try await iterator.next() {
99+
buffer.writeInteger(byte)
100+
}
101+
if buffer.readableBytes > 0 {
102+
return buffer
103+
}
104+
return nil
105+
})
106+
return body
107+
}
108+
}
109+
110+
#endif
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
#if compiler(>=5.5) && canImport(_Concurrency)
16+
import NIOHTTP1
17+
18+
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
19+
struct HTTPClientRequest {
20+
var url: String
21+
var method: HTTPMethod
22+
var headers: HTTPHeaders
23+
24+
var body: Body?
25+
26+
init(url: String) {
27+
self.url = url
28+
self.method = .GET
29+
self.headers = .init()
30+
self.body = .none
31+
}
32+
}
33+
34+
#endif

0 commit comments

Comments
 (0)