|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2017-2021 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 | +import NIOHTTP1 |
| 17 | + |
| 18 | +enum ControlPlaneRequest: Hashable { |
| 19 | + case next |
| 20 | + case invocationResponse(String, ByteBuffer?) |
| 21 | + case invocationError(String, ErrorResponse) |
| 22 | + case initializationError(ErrorResponse) |
| 23 | +} |
| 24 | + |
| 25 | +enum ControlPlaneResponse: Hashable { |
| 26 | + case next(Invocation, ByteBuffer) |
| 27 | + case accepted |
| 28 | + case error(ErrorResponse) |
| 29 | +} |
| 30 | + |
| 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? |
| 38 | + |
| 39 | + init(headers: HTTPHeaders) throws { |
| 40 | + guard let requestID = headers.first(name: AmazonHeaders.requestID), !requestID.isEmpty else { |
| 41 | + throw Lambda.RuntimeError.invocationMissingHeader(AmazonHeaders.requestID) |
| 42 | + } |
| 43 | + |
| 44 | + guard let deadline = headers.first(name: AmazonHeaders.deadline), |
| 45 | + let unixTimeInMilliseconds = Int64(deadline) |
| 46 | + else { |
| 47 | + throw Lambda.RuntimeError.invocationMissingHeader(AmazonHeaders.deadline) |
| 48 | + } |
| 49 | + |
| 50 | + guard let invokedFunctionARN = headers.first(name: AmazonHeaders.invokedFunctionARN) else { |
| 51 | + throw Lambda.RuntimeError.invocationMissingHeader(AmazonHeaders.invokedFunctionARN) |
| 52 | + } |
| 53 | + |
| 54 | + guard let traceID = headers.first(name: AmazonHeaders.traceID) else { |
| 55 | + throw Lambda.RuntimeError.invocationMissingHeader(AmazonHeaders.traceID) |
| 56 | + } |
| 57 | + |
| 58 | + self.requestID = requestID |
| 59 | + self.deadlineInMillisSinceEpoch = unixTimeInMilliseconds |
| 60 | + self.invokedFunctionARN = invokedFunctionARN |
| 61 | + self.traceID = traceID |
| 62 | + self.clientContext = headers["Lambda-Runtime-Client-Context"].first |
| 63 | + self.cognitoIdentity = headers["Lambda-Runtime-Cognito-Identity"].first |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +struct ErrorResponse: Hashable, Codable { |
| 68 | + var errorType: String |
| 69 | + var errorMessage: String |
| 70 | +} |
| 71 | + |
| 72 | +extension ErrorResponse { |
| 73 | + internal func toJSONBytes() -> [UInt8] { |
| 74 | + var bytes = [UInt8]() |
| 75 | + bytes.append(UInt8(ascii: "{")) |
| 76 | + bytes.append(contentsOf: #""errorType":"#.utf8) |
| 77 | + self.errorType.encodeAsJSONString(into: &bytes) |
| 78 | + bytes.append(contentsOf: #","errorMessage":"#.utf8) |
| 79 | + self.errorMessage.encodeAsJSONString(into: &bytes) |
| 80 | + bytes.append(UInt8(ascii: "}")) |
| 81 | + return bytes |
| 82 | + } |
| 83 | +} |
0 commit comments