-
Notifications
You must be signed in to change notification settings - Fork 35
add initial stream bridge #964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientError.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
/// Errors that are particular to the NIO-based Smithy HTTP client. | ||
public enum NIOHTTPClientError: Error { | ||
|
||
case streamingError(Error) | ||
} |
143 changes: 143 additions & 0 deletions
143
Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientStreamBridge.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import AsyncHTTPClient | ||
import Foundation | ||
import NIO | ||
import Smithy | ||
import SmithyStreams | ||
|
||
/// Handles streaming between Smithy streams and AsyncHTTPClient | ||
final class NIOHTTPClientStreamBridge { | ||
|
||
/// Convert Smithy ByteStream to AsyncHTTPClient request body | ||
static func convertRequestBody( | ||
from body: ByteStream, | ||
allocator: ByteBufferAllocator | ||
) async throws -> AsyncHTTPClient.HTTPClientRequest.Body { | ||
switch body { | ||
case .noStream: | ||
// No body to send | ||
return .bytes(allocator.buffer(capacity: 0)) | ||
|
||
case .data(let data): | ||
// Convert Data to ByteBuffer | ||
if let data = data { | ||
var buffer = allocator.buffer(capacity: data.count) | ||
buffer.writeBytes(data) | ||
return .bytes(buffer) | ||
} else { | ||
return .bytes(allocator.buffer(capacity: 0)) | ||
} | ||
|
||
case .stream(let stream): | ||
// Handle streaming request body | ||
return try await convertStreamToRequestBody(stream: stream, allocator: allocator) | ||
} | ||
} | ||
|
||
/// Convert AsyncHTTPClient response body to Smithy ByteStream | ||
static func convertResponseBody( | ||
from response: AsyncHTTPClient.HTTPClientResponse | ||
) -> ByteStream { | ||
let bufferedStream = BufferedStream() | ||
|
||
// Start a background task to stream data from AsyncHTTPClient to BufferedStream | ||
Task { | ||
do { | ||
var iterator = response.body.makeAsyncIterator() | ||
while let buffer = try await iterator.next() { | ||
// Convert ByteBuffer to Data and write to buffered stream | ||
if let bytes = buffer.getBytes(at: buffer.readerIndex, length: buffer.readableBytes) { | ||
let data = Data(bytes) | ||
try bufferedStream.write(contentsOf: data) | ||
} | ||
} | ||
bufferedStream.close() | ||
} catch { | ||
bufferedStream.closeWithError(error) | ||
} | ||
} | ||
|
||
return .stream(bufferedStream) | ||
} | ||
|
||
/// Convert a Smithy Stream to AsyncHTTPClient request body | ||
private static func convertStreamToRequestBody( | ||
stream: Smithy.Stream, | ||
allocator: ByteBufferAllocator | ||
) async throws -> AsyncHTTPClient.HTTPClientRequest.Body { | ||
if let streamLength = stream.length { | ||
let asyncSequence = StreamToAsyncSequence(stream: stream, allocator: allocator) | ||
return .stream(asyncSequence, length: .known(Int64(streamLength))) | ||
} else { | ||
do { | ||
let data = try await stream.readToEndAsync() | ||
if let data = data { | ||
var buffer = allocator.buffer(capacity: data.count) | ||
buffer.writeBytes(data) | ||
return .bytes(buffer) | ||
} else { | ||
return .bytes(allocator.buffer(capacity: 0)) | ||
} | ||
} catch { | ||
throw NIOHTTPClientError.streamingError(error) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// AsyncSequence adapter that converts a Smithy Stream to ByteBuffer sequence for AsyncHTTPClient | ||
internal struct StreamToAsyncSequence: AsyncSequence, Sendable { | ||
typealias Element = ByteBuffer | ||
|
||
private let stream: Smithy.Stream | ||
private let allocator: ByteBufferAllocator | ||
|
||
init(stream: Smithy.Stream, allocator: ByteBufferAllocator) { | ||
self.stream = stream | ||
self.allocator = allocator | ||
} | ||
|
||
func makeAsyncIterator() -> AsyncIterator { | ||
AsyncIterator(stream: stream, allocator: allocator) | ||
} | ||
|
||
struct AsyncIterator: AsyncIteratorProtocol { | ||
private let stream: Smithy.Stream | ||
private let allocator: ByteBufferAllocator | ||
jbelkins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private var isFinished = false | ||
|
||
init(stream: Smithy.Stream, allocator: ByteBufferAllocator) { | ||
self.stream = stream | ||
self.allocator = allocator | ||
} | ||
|
||
mutating func next() async throws -> ByteBuffer? { | ||
guard !isFinished else { return nil } | ||
|
||
do { | ||
// Read a chunk from the stream (using default chunk size) | ||
let data = try await stream.readAsync(upToCount: CHUNK_SIZE_BYTES) | ||
jbelkins marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
if let data = data, !data.isEmpty { | ||
var buffer = allocator.buffer(capacity: data.count) | ||
buffer.writeBytes(data) | ||
return buffer | ||
} else { | ||
isFinished = true | ||
stream.close() | ||
return nil | ||
} | ||
} catch { | ||
isFinished = true | ||
stream.close() | ||
throw NIOHTTPClientError.streamingError(error) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.