-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AWS API Gateway with Amazon Lambda integrations support
- Loading branch information
Showing
17 changed files
with
768 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"""OpenAPI core contrib aws module""" | ||
from openapi_core.contrib.aws.finders import APIGatewayPathFinder | ||
from openapi_core.contrib.aws.requests import APIGatewayEventOpenAPIRequest | ||
from openapi_core.contrib.aws.responses import ( | ||
APIGatewayEventResponseOpenAPIResponse, | ||
) | ||
|
||
__all__ = [ | ||
"APIGatewayEventOpenAPIRequest", | ||
"APIGatewayEventResponseOpenAPIResponse", | ||
"APIGatewayPathFinder", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from typing import List | ||
from typing import Optional | ||
|
||
from pydantic.dataclasses import dataclass | ||
|
||
|
||
class LambdaConfig: | ||
extra = "allow" | ||
|
||
|
||
@dataclass(config=LambdaConfig, kw_only=True) | ||
class BaseAPIGatewayEvent: | ||
path: str | ||
httpMethod: str | ||
headers: dict | ||
queryStringParameters: Optional[dict] = None | ||
isBase64Encoded: Optional[bool] = None | ||
body: Optional[str] = None | ||
pathParameters: Optional[dict] = None | ||
stageVariables: Optional[dict] = None | ||
|
||
|
||
@dataclass(config=LambdaConfig, kw_only=True) | ||
class APIGatewayEvent(BaseAPIGatewayEvent): | ||
"""AWS API Gateway event""" | ||
|
||
resource: str | ||
multiValueHeaders: dict | ||
version: Optional[str] = "1.0" | ||
multiValueQueryStringParameters: Optional[dict] = None | ||
|
||
|
||
@dataclass(config=LambdaConfig, kw_only=True) | ||
class APIGatewayEventV2(BaseAPIGatewayEvent): | ||
"""AWS API Gateway event v2""" | ||
|
||
version: str | ||
routeKey: str | ||
rawPath: dict | ||
rawQueryString: str | ||
cookies: Optional[List[str]] = None | ||
|
||
|
||
@dataclass(config=LambdaConfig, kw_only=True) | ||
class APIGatewayEventResponse: | ||
"""AWS API Gateway event response""" | ||
|
||
isBase64Encoded: bool | ||
statusCode: str | ||
headers: dict | ||
multiValueHeaders: dict | ||
body: str | ||
|
||
|
||
@dataclass(config=LambdaConfig, kw_only=True) | ||
class APIGatewayEventV2Response: | ||
"""AWS API Gateway event v2 response""" | ||
|
||
isBase64Encoded: bool = False | ||
statusCode: str = 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from openapi_core.templating.paths.finders import APICallPathFinder | ||
from openapi_core.templating.paths.iterators import AnyMethodOperationsIterator | ||
|
||
|
||
class APIGatewayPathFinder(APICallPathFinder): | ||
operations_iterator = AnyMethodOperationsIterator( | ||
any_method="x-amazon-apigateway-any-method", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from typing import Optional | ||
|
||
from werkzeug.datastructures import Headers | ||
from werkzeug.datastructures import ImmutableMultiDict | ||
|
||
from openapi_core.contrib.aws.datatypes import APIGatewayEvent | ||
from openapi_core.contrib.aws.typing import APIGatewayEventDict | ||
from openapi_core.datatypes import RequestParameters | ||
|
||
|
||
class APIGatewayEventOpenAPIRequest: | ||
""" | ||
Converts an API Gateway event to an OpenAPI request | ||
""" | ||
|
||
def __init__(self, event: APIGatewayEventDict): | ||
self.event = APIGatewayEvent(**event) | ||
|
||
self.parameters = RequestParameters( | ||
query=ImmutableMultiDict(self.event.queryStringParameters), | ||
header=Headers(self.event.headers), | ||
cookie=ImmutableMultiDict(), | ||
) | ||
|
||
@property | ||
def host_url(self) -> str: | ||
proto = self.event.headers["X-Forwarded-Proto"] | ||
host = self.event.headers["Host"] | ||
return "://".join([proto, host]) | ||
|
||
@property | ||
def path(self) -> str: | ||
return self.event.resource | ||
|
||
@property | ||
def method(self) -> str: | ||
return self.event.httpMethod.lower() | ||
|
||
@property | ||
def body(self) -> Optional[str]: | ||
return self.event.body | ||
|
||
@property | ||
def mimetype(self) -> str: | ||
return self.event.headers.get("Content-Type", "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from werkzeug.datastructures import Headers | ||
|
||
from openapi_core.contrib.aws.datatypes import APIGatewayEventResponse | ||
from openapi_core.contrib.aws.typing import APIGatewayEventResponseDict | ||
|
||
|
||
class APIGatewayEventResponseOpenAPIResponse: | ||
""" | ||
Converts an API Gateway event response to an OpenAPI request | ||
""" | ||
|
||
def __init__(self, response: APIGatewayEventResponseDict): | ||
self.response = APIGatewayEventResponse(**response) | ||
|
||
@property | ||
def data(self) -> str: | ||
return self.response.body | ||
|
||
@property | ||
def status_code(self) -> int: | ||
return self.response.statusCode | ||
|
||
@property | ||
def headers(self) -> Headers: | ||
return self.response.headers | ||
|
||
@property | ||
def mimetype(self) -> str: | ||
content_type = self.response.headers.get("Content-Type", "") | ||
assert isinstance(content_type, str) | ||
return content_type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from typing import Any | ||
from typing import Mapping | ||
|
||
APIGatewayEventDict = Mapping[str, Any] | ||
APIGatewayEventResponseDict = Mapping[str, Any] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from openapi_core.templating.paths.finders import APICallPathFinder | ||
from openapi_core.templating.paths.finders import WebhookPathFinder | ||
|
||
__all__ = [ | ||
"APICallPathFinder", | ||
"WebhookPathFinder", | ||
] |
Oops, something went wrong.