forked from swift-server/swift-openapi-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPIGatewayV2+HTTPRequest.swift
63 lines (56 loc) · 1.99 KB
/
APIGatewayV2+HTTPRequest.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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift OpenAPI Lambda open source project
//
// Copyright (c) 2023 Amazon.com, Inc. or its affiliates
// and the Swift OpenAPI Lambda project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift OpenAPI Lambda project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import AWSLambdaEvents
import HTTPTypes
import OpenAPIRuntime
extension APIGatewayV2Request {
// OpenAPIGenerator expects the path to include the query string
var pathWithQueryString: String {
rawPath + (rawQueryString.isEmpty ? "" : "?\(rawQueryString)")
}
/// Return an `HTTPRequest` for this `APIGatewayV2Request`
public func httpRequest() throws -> HTTPRequest {
HTTPRequest(
method: self.context.http.method,
scheme: "https",
authority: "",
path: pathWithQueryString,
headerFields: self.headers.httpFields()
)
}
}
extension APIGatewayV2Response {
/// Create a `APIGatewayV2Response` from an `HTTPResponse`
public init(from response: HTTPResponse) {
self = APIGatewayV2Response(
statusCode: response.status,
headers: .init(from: response.headerFields),
isBase64Encoded: false,
cookies: nil
)
}
}
public extension HTTPHeaders {
/// Create an `HTTPFields` (from `HTTPTypes` library) from this APIGateway `HTTPHeader`
func httpFields() -> HTTPFields {
HTTPFields(self.map { key, value in HTTPField(name: .init(key)!, value: value) })
}
/// Create HTTPHeaders from HTTPFields
init(from fields: HTTPFields) {
var headers: HTTPHeaders = [:]
fields.forEach { headers[$0.name.rawName] = $0.value }
self = headers
}
}