Skip to content

Make v2 API functions and types public #358

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 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions Sources/AWSLambdaRuntime/Lambda+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ import class Foundation.JSONEncoder

extension JSONDecoder: AWSLambdaRuntimeCore.LambdaEventDecoder {}

@usableFromInline
package struct LambdaJSONOutputEncoder<Output: Encodable>: LambdaOutputEncoder {
public struct LambdaJSONOutputEncoder<Output: Encodable>: LambdaOutputEncoder {
@usableFromInline let jsonEncoder: JSONEncoder

@inlinable
package init(_ jsonEncoder: JSONEncoder) {
public init(_ jsonEncoder: JSONEncoder) {
self.jsonEncoder = jsonEncoder
}

@inlinable
package func encode(_ value: Output, into buffer: inout ByteBuffer) throws {
public func encode(_ value: Output, into buffer: inout ByteBuffer) throws {
try self.jsonEncoder.encode(value, into: &buffer)
}
}
Expand All @@ -47,7 +46,7 @@ extension LambdaCodableAdapter {
/// - encoder: The encoder object that will be used to encode the generic ``Output`` obtained from the `handler`'s `outputWriter` into a ``ByteBuffer``.
/// - decoder: The decoder object that will be used to decode the received ``ByteBuffer`` event into the generic ``Event`` type served to the `handler`.
/// - handler: The handler object.
package init(
public init(
encoder: JSONEncoder,
decoder: JSONDecoder,
handler: Handler
Expand All @@ -71,7 +70,7 @@ extension LambdaRuntime {
/// - Parameter body: The handler in the form of a closure.
/// - Parameter encoder: The encoder object that will be used to encode the generic ``Output`` into a ``ByteBuffer``. ``JSONEncoder()`` used as default.
/// - Parameter decoder: The decoder object that will be used to decode the incoming ``ByteBuffer`` event into the generic ``Event`` type. ``JSONDecoder()`` used as default.
package convenience init<Event: Decodable, Output>(
public convenience init<Event: Decodable, Output>(
body: @escaping (Event, LambdaContext) async throws -> Output,
encoder: JSONEncoder = JSONEncoder(),
decoder: JSONDecoder = JSONDecoder()
Expand All @@ -97,7 +96,7 @@ extension LambdaRuntime {
/// Initialize an instance with a ``LambdaHandler`` defined in the form of a closure **with a `Void` return type**.
/// - Parameter body: The handler in the form of a closure.
/// - Parameter decoder: The decoder object that will be used to decode the incoming ``ByteBuffer`` event into the generic ``Event`` type. ``JSONDecoder()`` used as default.
package convenience init<Event: Decodable>(
public convenience init<Event: Decodable>(
body: @escaping (Event, LambdaContext) async throws -> Void,
decoder: JSONDecoder = JSONDecoder()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import NIOCore

/// The protocol a decoder must conform to so that it can be used with ``LambdaCodableAdapter`` to decode incoming
/// ``ByteBuffer`` events.
package protocol LambdaEventDecoder {
public protocol LambdaEventDecoder {
/// Decode the ``ByteBuffer`` representing the received event into the generic ``Event`` type
/// the handler will receive.
/// - Parameters:
Expand All @@ -28,7 +28,7 @@ package protocol LambdaEventDecoder {

/// The protocol an encoder must conform to so that it can be used with ``LambdaCodableAdapter`` to encode the generic
/// ``Output`` object into a ``ByteBuffer``.
package protocol LambdaOutputEncoder {
public protocol LambdaOutputEncoder {
associatedtype Output

/// Encode the generic type `Output` the handler has returned into a ``ByteBuffer``.
Expand All @@ -38,15 +38,17 @@ package protocol LambdaOutputEncoder {
func encode(_ value: Output, into buffer: inout ByteBuffer) throws
}

package struct VoidEncoder: LambdaOutputEncoder {
package typealias Output = Void
public struct VoidEncoder: LambdaOutputEncoder {
public typealias Output = Void

public init() {}

@inlinable
package func encode(_ value: Void, into buffer: inout NIOCore.ByteBuffer) throws {}
public func encode(_ value: Void, into buffer: inout NIOCore.ByteBuffer) throws {}
}

/// Adapts a ``LambdaHandler`` conforming handler to conform to ``LambdaWithBackgroundProcessingHandler``.
package struct LambdaHandlerAdapter<
public struct LambdaHandlerAdapter<
Event: Decodable,
Output,
Handler: LambdaHandler
Expand All @@ -56,7 +58,7 @@ package struct LambdaHandlerAdapter<
/// Initializes an instance given a concrete handler.
/// - Parameter handler: The ``LambdaHandler`` conforming handler that is to be adapted to ``LambdaWithBackgroundProcessingHandler``.
@inlinable
package init(handler: Handler) {
public init(handler: Handler) {
self.handler = handler
}

Expand All @@ -67,7 +69,7 @@ package struct LambdaHandlerAdapter<
/// - outputWriter: The writer to write the computed response to.
/// - context: The ``LambdaContext`` containing the invocation's metadata.
@inlinable
package func handle(
public func handle(
_ event: Event,
outputWriter: some LambdaResponseWriter<Output>,
context: LambdaContext
Expand All @@ -78,7 +80,7 @@ package struct LambdaHandlerAdapter<
}

/// Adapts a ``LambdaWithBackgroundProcessingHandler`` conforming handler to conform to ``StreamingLambdaHandler``.
package struct LambdaCodableAdapter<
public struct LambdaCodableAdapter<
Handler: LambdaWithBackgroundProcessingHandler,
Event: Decodable,
Output,
Expand All @@ -88,7 +90,6 @@ package struct LambdaCodableAdapter<
@usableFromInline let handler: Handler
@usableFromInline let encoder: Encoder
@usableFromInline let decoder: Decoder
//
@usableFromInline var byteBuffer: ByteBuffer = .init()

/// Initializes an instance given an encoder, decoder, and a handler with a non-`Void` output.
Expand All @@ -97,7 +98,7 @@ package struct LambdaCodableAdapter<
/// - decoder: The decoder object that will be used to decode the received ``ByteBuffer`` event into the generic ``Event`` type served to the `handler`.
/// - handler: The handler object.
@inlinable
package init(encoder: Encoder, decoder: Decoder, handler: Handler) where Output: Encodable {
public init(encoder: Encoder, decoder: Decoder, handler: Handler) where Output: Encodable {
self.encoder = encoder
self.decoder = decoder
self.handler = handler
Expand All @@ -108,7 +109,7 @@ package struct LambdaCodableAdapter<
/// - decoder: The decoder object that will be used to decode the received ``ByteBuffer`` event into the generic ``Event`` type served to the `handler`.
/// - handler: The handler object.
@inlinable
package init(decoder: Decoder, handler: Handler) where Output == Void, Encoder == VoidEncoder {
public init(decoder: Decoder, handler: Handler) where Output == Void, Encoder == VoidEncoder {
self.encoder = VoidEncoder()
self.decoder = decoder
self.handler = handler
Expand All @@ -120,7 +121,7 @@ package struct LambdaCodableAdapter<
/// - outputWriter: The writer to write the computed response to.
/// - context: The ``LambdaContext`` containing the invocation's metadata.
@inlinable
package mutating func handle<Writer: LambdaResponseStreamWriter>(
public mutating func handle<Writer: LambdaResponseStreamWriter>(
_ request: ByteBuffer,
responseWriter: Writer,
context: LambdaContext
Expand All @@ -136,7 +137,7 @@ package struct LambdaCodableAdapter<
}

/// A ``LambdaResponseStreamWriter`` wrapper that conforms to ``LambdaResponseWriter``.
package struct LambdaCodableResponseWriter<Output, Encoder: LambdaOutputEncoder, Base: LambdaResponseStreamWriter>:
public struct LambdaCodableResponseWriter<Output, Encoder: LambdaOutputEncoder, Base: LambdaResponseStreamWriter>:
LambdaResponseWriter
where Output == Encoder.Output {
@usableFromInline let underlyingStreamWriter: Base
Expand All @@ -147,13 +148,13 @@ where Output == Encoder.Output {
/// - encoder: The encoder object that will be used to encode the generic ``Output`` into a ``ByteBuffer``, which will then be passed to `streamWriter`.
/// - streamWriter: The underlying ``LambdaResponseStreamWriter`` that will be wrapped.
@inlinable
package init(encoder: Encoder, streamWriter: Base) {
public init(encoder: Encoder, streamWriter: Base) {
self.encoder = encoder
self.underlyingStreamWriter = streamWriter
}

@inlinable
package func write(_ output: Output) async throws {
public func write(_ output: Output) async throws {
var outputBuffer = ByteBuffer()
try self.encoder.encode(output, into: &outputBuffer)
try await self.underlyingStreamWriter.writeAndFinish(outputBuffer)
Expand Down
4 changes: 2 additions & 2 deletions Sources/AWSLambdaRuntimeCore/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import ucrt
#error("Unsupported platform")
#endif

enum Lambda {
public enum Lambda {
package static func runLoop<RuntimeClient: LambdaRuntimeClientProtocol, Handler>(
runtimeClient: RuntimeClient,
handler: Handler,
Expand Down Expand Up @@ -60,7 +60,7 @@ enum Lambda {
}

/// The default EventLoop the Lambda is scheduled on.
package static var defaultEventLoop: any EventLoop = NIOSingletons.posixEventLoopGroup.next()
public static var defaultEventLoop: any EventLoop = NIOSingletons.posixEventLoopGroup.next()
}

// MARK: - Public API
Expand Down
20 changes: 10 additions & 10 deletions Sources/AWSLambdaRuntimeCore/LambdaContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import NIOCore

/// Lambda runtime context.
/// The Lambda runtime generates and passes the `LambdaContext` to the Lambda handler as an argument.
package struct LambdaContext: CustomDebugStringConvertible, Sendable {
public struct LambdaContext: CustomDebugStringConvertible, Sendable {
final class _Storage: Sendable {
let requestID: String
let traceID: String
Expand Down Expand Up @@ -52,39 +52,39 @@ package struct LambdaContext: CustomDebugStringConvertible, Sendable {
private var storage: _Storage

/// The request ID, which identifies the request that triggered the function invocation.
package var requestID: String {
public var requestID: String {
self.storage.requestID
}

/// The AWS X-Ray tracing header.
package var traceID: String {
public var traceID: String {
self.storage.traceID
}

/// The ARN of the Lambda function, version, or alias that's specified in the invocation.
package var invokedFunctionARN: String {
public var invokedFunctionARN: String {
self.storage.invokedFunctionARN
}

/// The timestamp that the function times out.
package var deadline: DispatchWallTime {
public var deadline: DispatchWallTime {
self.storage.deadline
}

/// For invocations from the AWS Mobile SDK, data about the Amazon Cognito identity provider.
package var cognitoIdentity: String? {
public var cognitoIdentity: String? {
self.storage.cognitoIdentity
}

/// For invocations from the AWS Mobile SDK, data about the client application and device.
package var clientContext: String? {
public var clientContext: String? {
self.storage.clientContext
}

/// `Logger` to log with.
///
/// - note: The `LogLevel` can be configured using the `LOG_LEVEL` environment variable.
package var logger: Logger {
public var logger: Logger {
self.storage.logger
}

Expand All @@ -108,15 +108,15 @@ package struct LambdaContext: CustomDebugStringConvertible, Sendable {
)
}

package func getRemainingTime() -> TimeAmount {
public func getRemainingTime() -> Duration {
let deadline = self.deadline.millisSinceEpoch
let now = DispatchWallTime.now().millisSinceEpoch

let remaining = deadline - now
return .milliseconds(remaining)
}

package var debugDescription: String {
public var debugDescription: String {
"\(Self.self)(requestID: \(self.requestID), traceID: \(self.traceID), invokedFunctionARN: \(self.invokedFunctionARN), cognitoIdentity: \(self.cognitoIdentity ?? "nil"), clientContext: \(self.clientContext ?? "nil"), deadline: \(self.deadline))"
}

Expand Down
30 changes: 15 additions & 15 deletions Sources/AWSLambdaRuntimeCore/LambdaHandlers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import NIOCore
/// Background work can also be executed after returning the response. After closing the response stream by calling
/// ``LambdaResponseStreamWriter/finish()`` or ``LambdaResponseStreamWriter/writeAndFinish(_:)``,
/// the ``handle(_:responseWriter:context:)`` function is free to execute any background work.
package protocol StreamingLambdaHandler {
public protocol StreamingLambdaHandler {
/// The handler function -- implement the business logic of the Lambda function here.
/// - Parameters:
/// - event: The invocation's input data.
Expand All @@ -45,7 +45,7 @@ package protocol StreamingLambdaHandler {

/// A writer object to write the Lambda response stream into. The HTTP response is started lazily.
/// before the first call to ``write(_:)`` or ``writeAndFinish(_:)``.
package protocol LambdaResponseStreamWriter {
public protocol LambdaResponseStreamWriter {
/// Write a response part into the stream. Bytes written are streamed continually.
/// - Parameter buffer: The buffer to write.
func write(_ buffer: ByteBuffer) async throws
Expand All @@ -64,7 +64,7 @@ package protocol LambdaResponseStreamWriter {
///
/// - note: This handler protocol does not support response streaming because the output has to be encoded prior to it being sent, e.g. it is not possible to encode a partial/incomplete JSON string.
/// This protocol also does not support the execution of background work after the response has been returned -- the ``LambdaWithBackgroundProcessingHandler`` protocol caters for such use-cases.
package protocol LambdaHandler {
public protocol LambdaHandler {
/// Generic input type.
/// The body of the request sent to Lambda will be decoded into this type for the handler to consume.
associatedtype Event: Decodable
Expand All @@ -85,7 +85,7 @@ package protocol LambdaHandler {
/// This is achieved by not having a return type in the `handle` function. The output is instead written into a
/// ``LambdaResponseWriter``that is passed in as an argument, meaning that the ``handle(_:)`` function is then free to implement
/// any background work after the result has been sent to the AWS Lambda control plane.
package protocol LambdaWithBackgroundProcessingHandler {
public protocol LambdaWithBackgroundProcessingHandler {
/// Generic input type.
/// The body of the request sent to Lambda will be decoded into this type for the handler to consume.
associatedtype Event: Decodable
Expand All @@ -109,7 +109,7 @@ package protocol LambdaWithBackgroundProcessingHandler {
/// Used with ``LambdaWithBackgroundProcessingHandler``.
/// A mechanism to "return" an output from ``LambdaWithBackgroundProcessingHandler/handle(_:outputWriter:context:)`` without the function needing to
/// have a return type and exit at that point. This allows for background work to be executed _after_ a response has been sent to the AWS Lambda response endpoint.
package protocol LambdaResponseWriter<Output> {
public protocol LambdaResponseWriter<Output> {
associatedtype Output
/// Sends the generic ``Output`` object (representing the computed result of the handler)
/// to the AWS Lambda response endpoint.
Expand All @@ -120,12 +120,12 @@ package protocol LambdaResponseWriter<Output> {

/// A ``StreamingLambdaHandler`` conforming handler object that can be constructed with a closure.
/// Allows for a handler to be defined in a clean manner, leveraging Swift's trailing closure syntax.
package struct StreamingClosureHandler: StreamingLambdaHandler {
public struct StreamingClosureHandler: StreamingLambdaHandler {
let body: @Sendable (ByteBuffer, LambdaResponseStreamWriter, LambdaContext) async throws -> Void

/// Initialize an instance from a handler function in the form of a closure.
/// - Parameter body: The handler function written as a closure.
package init(
public init(
body: @Sendable @escaping (ByteBuffer, LambdaResponseStreamWriter, LambdaContext) async throws -> Void
) {
self.body = body
Expand All @@ -137,7 +137,7 @@ package struct StreamingClosureHandler: StreamingLambdaHandler {
/// - responseWriter: A ``LambdaResponseStreamWriter`` to write the invocation's response to.
/// If no response or error is written to `responseWriter` an error will be reported to the invoker.
/// - context: The ``LambdaContext`` containing the invocation's metadata.
package func handle(
public func handle(
_ request: ByteBuffer,
responseWriter: some LambdaResponseStreamWriter,
context: LambdaContext
Expand All @@ -148,34 +148,34 @@ package struct StreamingClosureHandler: StreamingLambdaHandler {

/// A ``LambdaHandler`` conforming handler object that can be constructed with a closure.
/// Allows for a handler to be defined in a clean manner, leveraging Swift's trailing closure syntax.
package struct ClosureHandler<Event: Decodable, Output>: LambdaHandler {
public struct ClosureHandler<Event: Decodable, Output>: LambdaHandler {
let body: (Event, LambdaContext) async throws -> Output

/// Initialize with a closure handler over generic `Input` and `Output` types.
/// - Parameter body: The handler function written as a closure.
package init(body: @escaping (Event, LambdaContext) async throws -> Output) where Output: Encodable {
public init(body: @escaping (Event, LambdaContext) async throws -> Output) where Output: Encodable {
self.body = body
}

/// Initialize with a closure handler over a generic `Input` type, and a `Void` `Output`.
/// - Parameter body: The handler function written as a closure.
package init(body: @escaping (Event, LambdaContext) async throws -> Void) where Output == Void {
public init(body: @escaping (Event, LambdaContext) async throws -> Void) where Output == Void {
self.body = body
}

/// Calls the provided `self.body` closure with the generic ``Event`` object representing the incoming event, and the ``LambdaContext``
/// - Parameters:
/// - event: The generic ``Event`` object representing the invocation's input data.
/// - context: The ``LambdaContext`` containing the invocation's metadata.
package func handle(_ event: Event, context: LambdaContext) async throws -> Output {
public func handle(_ event: Event, context: LambdaContext) async throws -> Output {
try await self.body(event, context)
}
}

extension LambdaRuntime {
/// Initialize an instance with a ``StreamingLambdaHandler`` in the form of a closure.
/// - Parameter body: The handler in the form of a closure.
package convenience init(
public convenience init(
body: @Sendable @escaping (ByteBuffer, LambdaResponseStreamWriter, LambdaContext) async throws -> Void
) where Handler == StreamingClosureHandler {
self.init(handler: StreamingClosureHandler(body: body))
Expand All @@ -185,7 +185,7 @@ extension LambdaRuntime {
/// - Parameter body: The handler in the form of a closure.
/// - Parameter encoder: The encoder object that will be used to encode the generic ``Output`` into a ``ByteBuffer``.
/// - Parameter decoder: The decoder object that will be used to decode the incoming ``ByteBuffer`` event into the generic ``Event`` type.
package convenience init<
public convenience init<
Event: Decodable,
Output: Encodable,
Encoder: LambdaOutputEncoder,
Expand Down Expand Up @@ -217,7 +217,7 @@ extension LambdaRuntime {
/// - Parameter body: The handler in the form of a closure.
/// - Parameter encoder: The encoder object that will be used to encode the generic ``Output`` into a ``ByteBuffer``.
/// - Parameter decoder: The decoder object that will be used to decode the incoming ``ByteBuffer`` event into the generic ``Event`` type.
package convenience init<Event: Decodable, Decoder: LambdaEventDecoder>(
public convenience init<Event: Decodable, Decoder: LambdaEventDecoder>(
decoder: Decoder,
body: @escaping (Event, LambdaContext) async throws -> Void
)
Expand Down
Loading
Loading