You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
init(transport: LambdaOpenAPITransport) throws { // <-- add this constructor (don't remove the call to `registerHandlers(on:)`)
@@ -406,9 +406,115 @@ LOCAL_LAMBDA_SERVER_ENABLED=true swift run
406
406
curl -v -X POST --header "Content-Type: application/json" --data @events/GetQuote.json http://127.0.0.1:7000/invoke
407
407
```
408
408
409
-
## Implement your own `OpenAPILambda` to support other event types
409
+
## Implement your own `OpenAPILambda` to support other event types
410
+
411
+
When you expose your AWS Lambda function to other event types, you have to specialize the `OpenAPILambda` protocol and implement the two methods that transform your Lambda function input data to an `OpenAPIRequest` and the other way around, transform an `OpenAPIResponse` to your Lambda function output type.
412
+
413
+
Here is an example with the [Amazon API Gateway (Rest Api)](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-rest-api.htm), (aka the original API Gateway).
414
+
415
+
I start with an OpenAPI stub implementation - unmodified.
Next, I implement a `struct` that conforms to `OpenAPILambda`. This `struct` defines:
440
+
441
+
- the event input and output the Lambda function will work on (from [AWS Lambda Event Types](https://github.com/swift-server/swift-aws-lambda-events) library).
442
+
- the mandatory constructor `init(transport:)`
443
+
- the executable target entrypoint (`@main`)
444
+
445
+
Here is an example using `APIGatewayRequest` and `APIGatewayResponse`:
446
+
447
+
```swift
448
+
@main
449
+
struct QuoteServiceLambda: OpenAPILambda {
450
+
typealias Event = APIGatewayRequest
451
+
typealias Output = APIGatewayResponse
452
+
public init(transport: LambdaOpenAPITransport) throws {
Next step is to implement two methods from `OpenAPILambda` protocol to convert your Lambda function input data (`APIGatewayRequest`) to an `OpenAPIRequest` and the other way around, transform an `OpenAPIResponse` to your Lambda function output type (`APIGatewayResponses`).
460
+
461
+
```swift
462
+
extension OpenAPILambda where Event == APIGatewayRequest {
463
+
/// Transform a Lambda input (`APIGatewayRequest` and `LambdaContext`) to an OpenAPILambdaRequest (`HTTPRequest`, `String?`)
464
+
public func request(context: LambdaContext, from request: Event) throws -> OpenAPILambdaRequest {
465
+
(try request.httpRequest(), request.body)
466
+
}
467
+
}
468
+
469
+
extension OpenAPILambda where Output == APIGatewayResponse {
470
+
/// Transform an OpenAPI response (`HTTPResponse`, `String?`) to a Lambda Output (`APIGatewayResponse`)
471
+
public func output(from response: OpenAPILambdaResponse) -> Output {
472
+
var apiResponse = APIGatewayResponse(from: response.0)
473
+
apiResponse.body = response.1
474
+
return apiResponse
475
+
}
476
+
}
477
+
```
478
+
479
+
To keep the above code short, simple, and readable, we suggest to implement whatever extension on the Lambda event type. Here are the extensions required to support the above code. These are simple data transformation methods from one type to the other.
480
+
481
+
```swift
482
+
extension APIGatewayRequest {
483
+
484
+
/// Return an `HTTPRequest.Method` for this `APIGatewayRequest`
485
+
public func httpRequestMethod() throws -> HTTPRequest.Method {
486
+
guard let method = HTTPRequest.Method(rawValue: self.httpMethod.rawValue) else {
You can apply the same design to support other AWS Lambda event types. However, keep in mind that the `OpenAPILAmbda` implementation is heavily biased towards receiving, routing, and responding to HTTP requests.
0 commit comments