Skip to content

Add apigateway and alb #52

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 5 commits into from
Apr 27, 2020
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
77 changes: 77 additions & 0 deletions Sources/AWSLambdaEvents/ALB.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import class Foundation.JSONEncoder

// https://github.com/aws/aws-lambda-go/blob/master/events/alb.go
public enum ALB {
/// ALBTargetGroupRequest contains data originating from the ALB Lambda target group integration
public struct TargetGroupRequest: Codable {
/// ALBTargetGroupRequestContext contains the information to identify the load balancer invoking the lambda
public struct Context: Codable {
public let elb: ELBContext
}

public let httpMethod: HTTPMethod
public let path: String
public let queryStringParameters: [String: [String]]

/// Depending on your configuration of your target group either `headers` or `multiValueHeaders`
/// are set.
///
/// For more information visit:
/// https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#multi-value-headers
public let headers: HTTPHeaders?

/// Depending on your configuration of your target group either `headers` or `multiValueHeaders`
/// are set.
///
/// For more information visit:
/// https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#multi-value-headers
public let multiValueHeaders: HTTPMultiValueHeaders?
public let requestContext: Context
public let isBase64Encoded: Bool
public let body: String?
}

/// ELBContext contains the information to identify the ARN invoking the lambda
public struct ELBContext: Codable {
public let targetGroupArn: String
}

public struct TargetGroupResponse: Codable {
public let statusCode: HTTPResponseStatus
public let statusDescription: String?
public let headers: HTTPHeaders?
public let multiValueHeaders: HTTPMultiValueHeaders?
public let body: String
public let isBase64Encoded: Bool

public init(
statusCode: HTTPResponseStatus,
statusDescription: String? = nil,
headers: HTTPHeaders? = nil,
multiValueHeaders: HTTPMultiValueHeaders? = nil,
body: String = "",
isBase64Encoded: Bool = false
) {
self.statusCode = statusCode
self.statusDescription = statusDescription
self.headers = headers
self.multiValueHeaders = multiValueHeaders
self.body = body
self.isBase64Encoded = isBase64Encoded
}
}
}
119 changes: 119 additions & 0 deletions Sources/AWSLambdaEvents/APIGateway+V2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

extension APIGateway {
public struct V2 {}
}

extension APIGateway.V2 {
/// APIGateway.V2.Request contains data coming from the new HTTP API Gateway
public struct Request: Codable {
/// Context contains the information to identify the AWS account and resources invoking the Lambda function.
public struct Context: Codable {
public struct HTTP: Codable {
public let method: HTTPMethod
public let path: String
public let `protocol`: String
public let sourceIp: String
public let userAgent: String
}

/// Authorizer contains authorizer information for the request context.
public struct Authorizer: Codable {
/// JWT contains JWT authorizer information for the request context.
public struct JWT: Codable {
public let claims: [String: String]
public let scopes: [String]?
}

let jwt: JWT
}

public let accountId: String
public let apiId: String
public let domainName: String
public let domainPrefix: String
public let stage: String
public let requestId: String

public let http: HTTP
public let authorizer: Authorizer?

/// The request time in format: 23/Apr/2020:11:08:18 +0000
public let time: String
public let timeEpoch: UInt64
}

public let version: String
public let routeKey: String
public let rawPath: String
public let rawQueryString: String

public let cookies: [String]?
public let headers: HTTPHeaders
public let queryStringParameters: [String: String]?
public let pathParameters: [String: String]?

public let context: Context
public let stageVariables: [String: String]?

public let body: String?
public let isBase64Encoded: Bool

enum CodingKeys: String, CodingKey {
case version
case routeKey
case rawPath
case rawQueryString

case cookies
case headers
case queryStringParameters
case pathParameters

case context = "requestContext"
case stageVariables

case body
case isBase64Encoded
}
}
}

extension APIGateway.V2 {
public struct Response: Codable {
public let statusCode: HTTPResponseStatus
public let headers: HTTPHeaders?
public let multiValueHeaders: HTTPMultiValueHeaders?
public let body: String?
public let isBase64Encoded: Bool?
public let cookies: [String]?

public init(
statusCode: HTTPResponseStatus,
headers: HTTPHeaders? = nil,
multiValueHeaders: HTTPMultiValueHeaders? = nil,
body: String? = nil,
isBase64Encoded: Bool? = nil,
cookies: [String]? = nil
) {
self.statusCode = statusCode
self.headers = headers
self.multiValueHeaders = multiValueHeaders
self.body = body
self.isBase64Encoded = isBase64Encoded
self.cookies = cookies
}
}
}
92 changes: 92 additions & 0 deletions Sources/AWSLambdaEvents/APIGateway.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import class Foundation.JSONEncoder

// https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you know about API Gateway's v2 integration with lambda? https://aws.amazon.com/blogs/compute/building-better-apis-http-apis-now-generally-available/

By default, it's request/response shape is different, and requires a configuration to be compatible with the "1.0" shape you've got here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomerd let me see what I can do to get this to work today. Since v2 already is GA, there might be a chance for us never to support v1. 😉

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomerd Okay I added the API Gateway v2 syntax, which in my opinion is much nicer. I've already tried it with my own lambda quite extensively. Works great and the v2 syntax is much easier to use, if you ask me.


public enum APIGateway {
/// APIGatewayRequest contains data coming from the API Gateway
public struct Request: Codable {
public struct Context: Codable {
public struct Identity: Codable {
public let cognitoIdentityPoolId: String?

public let apiKey: String?
public let userArn: String?
public let cognitoAuthenticationType: String?
public let caller: String?
public let userAgent: String?
public let user: String?

public let cognitoAuthenticationProvider: String?
public let sourceIp: String?
public let accountId: String?
}

public let resourceId: String
public let apiId: String
public let resourcePath: String
public let httpMethod: String
public let requestId: String
public let accountId: String
public let stage: String

public let identity: Identity
public let extendedRequestId: String?
public let path: String
}

public let resource: String
public let path: String
public let httpMethod: HTTPMethod

public let queryStringParameters: [String: String]?
public let multiValueQueryStringParameters: [String: [String]]?
public let headers: HTTPHeaders
public let multiValueHeaders: HTTPMultiValueHeaders
public let pathParameters: [String: String]?
public let stageVariables: [String: String]?

public let requestContext: Context
public let body: String?
public let isBase64Encoded: Bool
}
}

// MARK: - Response -

extension APIGateway {
public struct Response: Codable {
public let statusCode: HTTPResponseStatus
public let headers: HTTPHeaders?
public let multiValueHeaders: HTTPMultiValueHeaders?
public let body: String?
public let isBase64Encoded: Bool?

public init(
statusCode: HTTPResponseStatus,
headers: HTTPHeaders? = nil,
multiValueHeaders: HTTPMultiValueHeaders? = nil,
body: String? = nil,
isBase64Encoded: Bool? = nil
) {
self.statusCode = statusCode
self.headers = headers
self.multiValueHeaders = multiValueHeaders
self.body = body
self.isBase64Encoded = isBase64Encoded
}
}
}
Loading