|
| 1 | +from typing import Dict |
| 2 | +from typing import Optional |
| 3 | +from typing import Tuple |
| 4 | +from urllib.parse import urljoin |
| 5 | + |
| 6 | +from werkzeug.datastructures import Headers |
| 7 | +from werkzeug.datastructures import ImmutableMultiDict |
| 8 | + |
| 9 | +from openapi_core.contrib.aws.datatypes import APIGatewayEvent |
| 10 | +from openapi_core.contrib.aws.datatypes import APIGatewayEventV2 |
| 11 | +from openapi_core.contrib.aws.types import APIGatewayEventPayload |
| 12 | +from openapi_core.contrib.aws.util import parse_forwarded |
| 13 | +from openapi_core.datatypes import RequestParameters |
| 14 | + |
| 15 | + |
| 16 | +class APIGatewayAWSProxyOpenAPIRequest: |
| 17 | + """ |
| 18 | + API Gateway AWS proxy event payload OpenAPI request. |
| 19 | +
|
| 20 | + Designed to be used with API Gateway REST API specification exports for |
| 21 | + integrations that use event v1 payload. Uses API Gateway event v1 |
| 22 | + requestContext's resourceId. Requires APIGatewayPathFinder to resolve ANY methods. |
| 23 | + """ |
| 24 | + |
| 25 | + def __init__(self, payload: APIGatewayEventPayload): |
| 26 | + self.event = APIGatewayEvent(**payload) |
| 27 | + |
| 28 | + self.parameters = RequestParameters( |
| 29 | + path=self.path_params, |
| 30 | + query=ImmutableMultiDict(self.query_params), |
| 31 | + header=Headers(self.event.headers), |
| 32 | + cookie=ImmutableMultiDict(), |
| 33 | + ) |
| 34 | + |
| 35 | + @property |
| 36 | + def resource_id(self) -> Tuple[str, str]: |
| 37 | + return self.event.requestContext.resourceId.split(" ") |
| 38 | + |
| 39 | + @property |
| 40 | + def path_params(self) -> Dict[str, str]: |
| 41 | + params = self.event.pathParameters |
| 42 | + if params is None: |
| 43 | + return {} |
| 44 | + return params |
| 45 | + |
| 46 | + @property |
| 47 | + def query_params(self) -> Dict[str, str]: |
| 48 | + params = self.event.queryStringParameters |
| 49 | + if params is None: |
| 50 | + return {} |
| 51 | + return params |
| 52 | + |
| 53 | + @property |
| 54 | + def proto(self) -> str: |
| 55 | + return self.event.headers.get("X-Forwarded-Proto", "https") |
| 56 | + |
| 57 | + @property |
| 58 | + def host(self) -> str: |
| 59 | + return self.event.headers["Host"] |
| 60 | + |
| 61 | + @property |
| 62 | + def host_url(self) -> str: |
| 63 | + return "://".join([self.proto, self.host]) |
| 64 | + |
| 65 | + @property |
| 66 | + def path(self) -> str: |
| 67 | + return self.event.path |
| 68 | + |
| 69 | + @property |
| 70 | + def method(self) -> str: |
| 71 | + return self.resource_id[0].lower() |
| 72 | + |
| 73 | + @property |
| 74 | + def body(self) -> Optional[str]: |
| 75 | + return self.event.body |
| 76 | + |
| 77 | + @property |
| 78 | + def content_type(self) -> str: |
| 79 | + return self.event.headers.get("Content-Type", "") |
| 80 | + |
| 81 | + @property |
| 82 | + def path_pattern(self): |
| 83 | + return self.resource_id[1] |
| 84 | + |
| 85 | + |
| 86 | +class APIGatewayAWSProxyV2OpenAPIRequest: |
| 87 | + """ |
| 88 | + API Gateway AWS Proxy event v2 payload OpenAPI request. |
| 89 | +
|
| 90 | + Designed to be used with API Gateway HTTP API specification exports for |
| 91 | + integrations that use event v2 payload. Uses API Gateway event v2 routeKey |
| 92 | + and rawPath data. Requires APIGatewayPathFinder to resolve ANY methods. |
| 93 | +
|
| 94 | + .. note:: |
| 95 | + API Gateway HTTP APIs don't support request validation |
| 96 | + """ |
| 97 | + |
| 98 | + def __init__(self, payload: APIGatewayEventPayload): |
| 99 | + self.event = APIGatewayEventV2(**payload) |
| 100 | + |
| 101 | + self.parameters = RequestParameters( |
| 102 | + path=self.path_params, |
| 103 | + query=ImmutableMultiDict(self.query_params), |
| 104 | + header=Headers(self.event.headers), |
| 105 | + cookie=ImmutableMultiDict(), |
| 106 | + ) |
| 107 | + |
| 108 | + @property |
| 109 | + def path_params(self) -> Dict[str, str]: |
| 110 | + if self.event.pathParameters is None: |
| 111 | + return {} |
| 112 | + return self.event.pathParameters |
| 113 | + |
| 114 | + @property |
| 115 | + def query_params(self) -> Dict[str, str]: |
| 116 | + if self.event.queryStringParameters is None: |
| 117 | + return {} |
| 118 | + return self.event.queryStringParameters |
| 119 | + |
| 120 | + @property |
| 121 | + def proto(self) -> str: |
| 122 | + return self.event.headers.get("x-forwarded-proto", "https") |
| 123 | + |
| 124 | + @property |
| 125 | + def host(self) -> str: |
| 126 | + # use Forwarded header if available |
| 127 | + if "forwarded" in self.event.headers: |
| 128 | + forwarded = parse_forwarded(self.event.headers["forwarded"]) |
| 129 | + if "host" in forwarded: |
| 130 | + return forwarded["host"] |
| 131 | + return self.event.headers["host"] |
| 132 | + |
| 133 | + @property |
| 134 | + def host_url(self) -> str: |
| 135 | + return "://".join([self.proto, self.host]) |
| 136 | + |
| 137 | + @property |
| 138 | + def path(self) -> str: |
| 139 | + return self.event.rawPath |
| 140 | + |
| 141 | + @property |
| 142 | + def method(self) -> str: |
| 143 | + return self.event.routeKey.split(" ")[0].lower() |
| 144 | + |
| 145 | + @property |
| 146 | + def body(self) -> Optional[str]: |
| 147 | + return self.event.body |
| 148 | + |
| 149 | + @property |
| 150 | + def content_type(self) -> str: |
| 151 | + return self.event.headers.get("content-type", "") |
| 152 | + |
| 153 | + |
| 154 | +class APIGatewayHTTPProxyOpenAPIRequest(APIGatewayAWSProxyV2OpenAPIRequest): |
| 155 | + """ |
| 156 | + API Gateway HTTP proxy integration event payload OpenAPI request. |
| 157 | +
|
| 158 | + Uses http integration path and method data. |
| 159 | +
|
| 160 | + NOTE: If you use HTTP integration not in root path then you need to provide the base path |
| 161 | + otherwise it won't find the correct path because it is not send with event. |
| 162 | + """ |
| 163 | + |
| 164 | + def __init__(self, payload: APIGatewayEventPayload, base_path: str = "/"): |
| 165 | + super().__init__(payload) |
| 166 | + self.base_path = base_path |
| 167 | + |
| 168 | + @property |
| 169 | + def path(self) -> str: |
| 170 | + return urljoin(self.base_path, self.event.requestContext.http.path.lstrip('/')) |
| 171 | + |
| 172 | + @property |
| 173 | + def method(self) -> str: |
| 174 | + return self.event.requestContext.http.method.lower() |
0 commit comments