Skip to content

Commit a70a396

Browse files
committed
Add LambdaRuntimeClientProtocol
1 parent f329874 commit a70a396

File tree

5 files changed

+50
-17
lines changed

5 files changed

+50
-17
lines changed

Sources/AWSLambdaRuntimeCore/ControlPlaneRequest.swift

+9-9
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ enum ControlPlaneRequest: Hashable {
2323
}
2424

2525
enum ControlPlaneResponse: Hashable {
26-
case next(Invocation, ByteBuffer)
26+
case next(InvocationMetadata, ByteBuffer)
2727
case accepted
2828
case error(ErrorResponse)
2929
}
3030

31-
struct Invocation: Hashable {
32-
let requestID: String
33-
let deadlineInMillisSinceEpoch: Int64
34-
let invokedFunctionARN: String
35-
let traceID: String
36-
let clientContext: String?
37-
let cognitoIdentity: String?
31+
package struct InvocationMetadata: Hashable {
32+
package let requestID: String
33+
package let deadlineInMillisSinceEpoch: Int64
34+
package let invokedFunctionARN: String
35+
package let traceID: String
36+
package let clientContext: String?
37+
package let cognitoIdentity: String?
3838

39-
init(headers: HTTPHeaders) throws {
39+
package init(headers: HTTPHeaders) throws {
4040
guard let requestID = headers.first(name: AmazonHeaders.requestID), !requestID.isEmpty else {
4141
throw LambdaRuntimeError.invocationMissingHeader(AmazonHeaders.requestID)
4242
}

Sources/AWSLambdaRuntimeCore/LambdaRuntimeClient.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ struct LambdaRuntimeClient {
3232
}
3333

3434
/// Requests invocation from the control plane.
35-
func getNextInvocation(logger: Logger) -> EventLoopFuture<(Invocation, ByteBuffer)> {
35+
func getNextInvocation(logger: Logger) -> EventLoopFuture<(InvocationMetadata, ByteBuffer)> {
3636
let url = Consts.invocationURLPrefix + Consts.getNextInvocationURLSuffix
3737
logger.debug("requesting work from lambda runtime engine using \(url)")
3838
return self.httpClient.get(url: url, headers: LambdaRuntimeClient.defaultHeaders).flatMapThrowing { response in
3939
guard response.status == .ok else {
4040
throw LambdaRuntimeError.badStatusCode(response.status)
4141
}
42-
let invocation = try Invocation(headers: response.headers)
42+
let invocation = try InvocationMetadata(headers: response.headers)
4343
guard let event = response.body else {
4444
throw LambdaRuntimeError.noBody
4545
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the SwiftAWSLambdaRuntime 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 SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import NIOCore
16+
17+
package protocol LambdaResponseStreamWriter {
18+
mutating func write(_ buffer: ByteBuffer) async throws
19+
func finish() async throws
20+
func writeAndFinish(_ buffer: ByteBuffer) async throws
21+
func reportError(_ error: any Error) async throws
22+
}
23+
24+
package protocol LambdaRuntimeClientProtocol {
25+
associatedtype Writer: LambdaResponseStreamWriter
26+
27+
func nextInvocation() async throws -> (Invocation, Writer)
28+
}
29+
30+
package struct Invocation {
31+
package let metadata: InvocationMetadata
32+
package let event: ByteBuffer
33+
}

Tests/AWSLambdaRuntimeCoreTests/ControlPlaneRequestTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class InvocationTest: XCTestCase {
2525
(AmazonHeaders.invokedFunctionARN, "arn:aws:lambda:us-east-1:123456789012:function:custom-runtime"),
2626
])
2727

28-
var invocation: Invocation?
28+
var invocation: InvocationMetadata?
2929

30-
XCTAssertNoThrow(invocation = try Invocation(headers: headers))
30+
XCTAssertNoThrow(invocation = try InvocationMetadata(headers: headers))
3131
XCTAssertNotNil(invocation)
3232

3333
guard !invocation!.traceID.isEmpty else {

Tests/AWSLambdaRuntimeCoreTests/LambdaRuntimeClientTest.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ class LambdaRuntimeClientTest: XCTestCase {
279279
(AmazonHeaders.invokedFunctionARN, "arn:aws:lambda:us-east-1:123456789012:function:custom-runtime"),
280280
(AmazonHeaders.traceID, "Root=\(AmazonHeaders.generateXRayTraceID());Sampled=1"),
281281
])
282-
var inv: Invocation?
283-
XCTAssertNoThrow(inv = try Invocation(headers: header))
282+
var inv: InvocationMetadata?
283+
XCTAssertNoThrow(inv = try InvocationMetadata(headers: header))
284284
guard let invocation = inv else { return }
285285

286286
let result = client.reportResults(
@@ -332,8 +332,8 @@ class LambdaRuntimeClientTest: XCTestCase {
332332
(AmazonHeaders.invokedFunctionARN, "arn:aws:lambda:us-east-1:123456789012:function:custom-runtime"),
333333
(AmazonHeaders.traceID, "Root=\(AmazonHeaders.generateXRayTraceID());Sampled=1"),
334334
])
335-
var inv: Invocation?
336-
XCTAssertNoThrow(inv = try Invocation(headers: header))
335+
var inv: InvocationMetadata?
336+
XCTAssertNoThrow(inv = try InvocationMetadata(headers: header))
337337
guard let invocation = inv else { return }
338338

339339
let result = client.reportResults(logger: logger, invocation: invocation, result: Result.success(nil))

0 commit comments

Comments
 (0)