Skip to content

Commit e823f58

Browse files
authored
feat: Add NIOHTTPClient based on async-http-client and SwiftNIO (#995)
1 parent a019370 commit e823f58

File tree

61 files changed

+1722
-568
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1722
-568
lines changed

Package.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@ let package = Package(
5353
.library(name: "SmithyCBOR", targets: ["SmithyCBOR"]),
5454
.library(name: "SmithyWaitersAPI", targets: ["SmithyWaitersAPI"]),
5555
.library(name: "SmithyTestUtil", targets: ["SmithyTestUtil"]),
56+
.library(name: "SmithySwiftNIO", targets: ["SmithySwiftNIO"]),
57+
.library(name: "SmithyTelemetryAPI", targets: ["SmithyTelemetryAPI"]),
58+
.library(name: "SmithyHTTPClientAPI", targets: ["SmithyHTTPClientAPI"]),
5659
],
5760
dependencies: {
5861
var dependencies: [Package.Dependency] = [
5962
.package(url: "https://github.com/awslabs/aws-crt-swift.git", exact: "0.54.2"),
6063
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
6164
.package(url: "https://github.com/open-telemetry/opentelemetry-swift", from: "1.13.0"),
65+
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.22.0"),
6266
]
6367

6468
let isDocCEnabled = ProcessInfo.processInfo.environment["AWS_SWIFT_SDK_ENABLE_DOCC"] != nil
@@ -74,10 +78,26 @@ let package = Package(
7478
.product(name: "Logging", package: "swift-log"),
7579
]
7680
),
81+
.target(
82+
name: "SmithyTelemetryAPI",
83+
dependencies: [
84+
"Smithy",
85+
]
86+
),
87+
.target(
88+
name: "SmithyHTTPClientAPI",
89+
dependencies: [
90+
"Smithy",
91+
"SmithyHTTPAPI",
92+
"SmithyTelemetryAPI",
93+
]
94+
),
7795
.target(
7896
name: "ClientRuntime",
7997
dependencies: [
8098
"Smithy",
99+
"SmithyTelemetryAPI",
100+
"SmithyHTTPClientAPI",
81101
"SmithyRetriesAPI",
82102
"SmithyRetries",
83103
"SmithyXML",
@@ -123,6 +143,17 @@ let package = Package(
123143
.copy("PrivacyInfo.xcprivacy")
124144
]
125145
),
146+
.target(
147+
name: "SmithySwiftNIO",
148+
dependencies: [
149+
"Smithy",
150+
"SmithyHTTPAPI",
151+
"SmithyStreams",
152+
"SmithyHTTPClientAPI",
153+
.product(name: "AsyncHTTPClient", package: "async-http-client"),
154+
],
155+
path: "Sources/SmithySwiftNIO"
156+
),
126157
.target(
127158
name: "SmithyRetriesAPI"
128159
),
@@ -268,6 +299,13 @@ let package = Package(
268299
],
269300
resources: [ .process("Resources") ]
270301
),
302+
.testTarget(
303+
name: "SmithySwiftNIOTests",
304+
dependencies: [
305+
"SmithySwiftNIO",
306+
"SmithyTestUtil",
307+
]
308+
),
271309
.testTarget(
272310
name: "SmithyCBORTests",
273311
dependencies: ["SmithyCBOR", "ClientRuntime", "SmithyTestUtil"]

Sources/ClientRuntime/Config/DefaultSDKRuntimeConfiguration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import protocol SmithyHTTPAuthAPI.AuthSchemeResolver
1313
import protocol SmithyHTTPAuthAPI.AuthSchemeResolverParameters
1414
import struct SmithyRetries.DefaultRetryStrategy
1515
import struct SmithyRetries.ExponentialBackoffStrategy
16+
import SmithyTelemetryAPI
1617
import protocol SmithyRetriesAPI.RetryErrorInfoProvider
1718
import protocol SmithyRetriesAPI.RetryStrategy
1819
import struct SmithyRetriesAPI.RetryStrategyOptions

Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import protocol SmithyHTTPAPI.HTTPClient
1919
import class SmithyHTTPAPI.HTTPRequest
2020
import class SmithyHTTPAPI.HTTPResponse
2121
import enum SmithyHTTPAPI.HTTPStatusCode
22+
import class SmithyHTTPClientAPI.HttpTelemetry
23+
import enum SmithyHTTPClientAPI.HttpMetricsAttributesKeys
2224
import class SmithyStreams.BufferedStream
25+
import SmithyTelemetryAPI
2326
#if os(Linux)
2427
import Glibc
2528
#elseif !os(Windows)

Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngineConfig.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* SPDX-License-Identifier: Apache-2.0.
44
*/
55

6+
import class SmithyHTTPClientAPI.HttpTelemetry
7+
68
struct CRTClientEngineConfig: Sendable {
79

810
/// Max connections the manager can contain per endpoint

Sources/ClientRuntime/Networking/Http/CRT/HTTP2Stream+ByteStream.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import AwsCommonRuntimeKit
99
import struct Smithy.Attributes
1010
import enum Smithy.ByteStream
11+
import class SmithyHTTPClientAPI.HttpTelemetry
12+
import enum SmithyHTTPClientAPI.HttpMetricsAttributesKeys
1113

1214
extension HTTP2Stream {
1315
/// Returns the recommended size, in bytes, for the data to write

Sources/ClientRuntime/Networking/Http/HttpClientConfiguration.swift

Lines changed: 4 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -5,87 +5,8 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8-
import AwsCommonRuntimeKit
9-
import struct Foundation.TimeInterval
10-
import enum Smithy.URIScheme
11-
import struct SmithyHTTPAPI.Headers
8+
import SmithyHTTPClientAPI
129

13-
public class HttpClientConfiguration {
14-
15-
/// The timeout for establishing a connection, in seconds.
16-
///
17-
/// If none is provided, the client will use default values based on the platform.
18-
public var connectTimeout: TimeInterval?
19-
20-
/// The timeout for socket, in seconds.
21-
/// Sets maximum time to wait between two data packets.
22-
/// Used to close stale connections that have no activity.
23-
///
24-
/// Defaults to 60 seconds if no value is provided.
25-
public var socketTimeout: TimeInterval
26-
27-
/// HTTP headers to be submitted with every HTTP request.
28-
///
29-
/// If none is provided, defaults to no extra headers.
30-
public var defaultHeaders: Headers
31-
32-
/// The maximum connections the HTTP client makes per host or per endpoint depending on the OS.
33-
///
34-
/// For Apple platforms, it will be per host.
35-
/// For Linux, it will be per endpoint (protocol + host + port).
36-
public var maxConnections: Int
37-
38-
// add any other properties here you want to give the service operations
39-
// control over to be mapped to the Http Client
40-
41-
/// The URL scheme to be used for HTTP requests. Supported values are `http` and `https`.
42-
///
43-
/// If none is provided, the default protocol for the operation will be used
44-
public var protocolType: URIScheme?
45-
46-
/// Custom TLS configuration for HTTPS connections.
47-
///
48-
/// Enables specifying client certificates and trust stores for secure communication.
49-
/// Defaults to system's TLS settings if `nil`.
50-
public var tlsConfiguration: (any TLSConfiguration)?
51-
52-
/// HTTP Client Telemetry
53-
public var telemetry: HttpTelemetry?
54-
55-
/// Creates a configuration object for a SDK HTTP client.
56-
///
57-
/// Not all configuration settings may be followed by all clients.
58-
/// - Parameters:
59-
/// - connectTimeout: The maximum time to wait for a connection to be established.
60-
/// - socketTimeout: The maximum time to wait between data packets.
61-
/// - defaultHeaders: HTTP headers to be included with every HTTP request.
62-
/// Note that certain headers may cause your API request to fail. Defaults to no headers.
63-
/// - protocolType: The HTTP scheme (`http` or `https`) to be used for API requests. Defaults to the operation's standard configuration.
64-
/// - tlsConfiguration: Optional custom TLS configuration for HTTPS requests. If `nil`, defaults to a standard configuration.
65-
/// - maxConnections: The maximum number of connections the HTTP client makes per host (for Apple platforms) or per endpoint (for Linux). For non-mac Apple platforms, defaults to 6. For macOS and Linux, defaults to 50.
66-
public init(
67-
connectTimeout: TimeInterval? = nil,
68-
socketTimeout: TimeInterval = 60.0,
69-
protocolType: URIScheme = .https,
70-
defaultHeaders: Headers = Headers(),
71-
tlsConfiguration: (any TLSConfiguration)? = nil,
72-
telemetry: HttpTelemetry? = nil,
73-
maxConnections: Int? = nil
74-
) {
75-
self.socketTimeout = socketTimeout
76-
self.protocolType = protocolType
77-
self.defaultHeaders = defaultHeaders
78-
self.connectTimeout = connectTimeout
79-
self.tlsConfiguration = tlsConfiguration
80-
self.telemetry = telemetry
81-
if let maxConnections {
82-
self.maxConnections = maxConnections
83-
} else {
84-
#if os(macOS) || os(Linux)
85-
self.maxConnections = 50
86-
#else // iOS, ipadOS, watchOS, tvOS.
87-
self.maxConnections = 6 // URLSession default.
88-
#endif
89-
}
90-
}
91-
}
10+
/// Typealias for backward compatibility.
11+
/// The actual implementation is now in SmithyHTTPClientAPI.
12+
public typealias HttpClientConfiguration = SmithyHTTPClientAPI.HTTPClientConfiguration
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import SmithyHTTPClientAPI
9+
10+
/// Typealias for backward compatibility.
11+
/// The actual implementation is now in SmithyHTTPClientAPI.
12+
public typealias TLSConfiguration = SmithyHTTPClientAPI.TLSConfiguration

Sources/ClientRuntime/Networking/Http/URLSession/FoundationStreamBridge.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import class Foundation.DispatchQueue
1313
import class Foundation.InputStream
1414
import class Foundation.NSObject
1515
import class Foundation.OutputStream
16+
import class SmithyHTTPClientAPI.HttpTelemetry
17+
import enum SmithyHTTPClientAPI.HttpMetricsAttributesKeys
1618
import class Foundation.RunLoop
1719
import class Foundation.Stream
1820
import protocol Foundation.StreamDelegate

Sources/ClientRuntime/Networking/Http/URLSession/URLSessionHTTPClient.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import class Foundation.NSRecursiveLock
1818
import var Foundation.NSURLAuthenticationMethodClientCertificate
1919
import var Foundation.NSURLAuthenticationMethodServerTrust
2020
import struct Foundation.TimeInterval
21+
import class SmithyHTTPClientAPI.HttpTelemetry
22+
import enum SmithyHTTPClientAPI.HttpMetricsAttributesKeys
2123
import class Foundation.URLAuthenticationChallenge
2224
import struct Foundation.URLComponents
2325
import class Foundation.URLCredential
@@ -27,6 +29,7 @@ import class Foundation.URLResponse
2729
import class Foundation.URLSession
2830
import class Foundation.URLSessionConfiguration
2931
import protocol Foundation.URLSessionDataDelegate
32+
import SmithyTelemetryAPI
3033
import class Foundation.URLSessionDataTask
3134
import class Foundation.URLSessionTask
3235
import class Foundation.URLSessionTaskMetrics

Sources/ClientRuntime/Plugins/TelemetryPlugin.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8+
import SmithyTelemetryAPI
9+
810
public class TelemetryPlugin: Plugin {
911
private let telemetryProvider: TelemetryProvider
1012

0 commit comments

Comments
 (0)