-
Notifications
You must be signed in to change notification settings - Fork 3
/
APIGatewayV2Lambda.swift
70 lines (63 loc) · 2.56 KB
/
APIGatewayV2Lambda.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2021-2021 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import AWSLambdaEvents
import AWSLambdaRuntimeCore
import Hummingbird
import NIOCore
import NIOHTTP1
extension HBLambda where Event == APIGatewayV2Request {
/// Specialization of HBLambda.request where `In` is `APIGateway.Request`
public func request(context: LambdaContext, application: HBApplication, from: Event) throws -> HBRequest {
var request = try HBRequest(context: context, application: application, from: from)
// store api gateway v2 request so it is available in routes
request.extensions.set(\.apiGatewayV2Request, value: from)
return request
}
}
extension HBLambda where Output == APIGatewayV2Response {
/// Specialization of HBLambda.request where `Out` is `APIGateway.Response`
public func output(from response: HBResponse) -> Output {
return response.apiResponse()
}
}
// conform `APIGatewayV2Request` to `APIRequest` so we can use HBRequest.init(context:application:from)
extension APIGatewayV2Request: APIRequest {
var path: String {
return context.http.path
}
var httpMethod: AWSLambdaEvents.HTTPMethod { context.http.method }
var queryString: String { self.rawQueryString }
var httpHeaders: HTTPHeaders {
return HTTPHeaders(self.headers.map { ($0.key, $0.value) })
}
}
// conform `APIGatewayV2Response` to `APIResponse` so we can use HBResponse.apiReponse()
extension APIGatewayV2Response: APIResponse {
init(
statusCode: AWSLambdaEvents.HTTPResponseStatus,
headers: AWSLambdaEvents.HTTPHeaders?,
multiValueHeaders: HTTPMultiValueHeaders?,
body: String?,
isBase64Encoded: Bool?
) {
precondition(multiValueHeaders == nil || multiValueHeaders?.count == 0, "Multi value headers are unavailable in APIGatewayV2")
self.init(statusCode: statusCode, headers: headers, body: body, isBase64Encoded: isBase64Encoded, cookies: nil)
}
}
extension HBRequest {
/// `APIGateway.V2.Request` that generated this `HBRequest`
public var apiGatewayV2Request: APIGatewayV2Request {
self.extensions.get(\.apiGatewayV2Request)
}
}