|
| 1 | +fromtypingimportDict |
| 2 | +fromtypingimportOptional |
| 3 | + |
| 4 | +fromwerkzeug.datastructuresimportHeaders |
| 5 | +fromwerkzeug.datastructuresimportImmutableMultiDict |
| 6 | + |
| 7 | +fromopenapi_core.contrib.aws.datatypesimportAPIGatewayEvent |
| 8 | +fromopenapi_core.contrib.aws.datatypesimportAPIGatewayEventV2 |
| 9 | +fromopenapi_core.contrib.aws.typesimportAPIGatewayEventPayload |
| 10 | +fromopenapi_core.datatypesimportRequestParameters |
| 11 | + |
| 12 | + |
| 13 | +classAPIGatewayEventOpenAPIRequest: |
| 14 | +""" |
| 15 | + Converts an API Gateway event payload to an OpenAPI request |
| 16 | + """ |
| 17 | + |
| 18 | +def__init__(self,payload:APIGatewayEventPayload): |
| 19 | +self.event=APIGatewayEvent(**payload) |
| 20 | + |
| 21 | +self.parameters=RequestParameters( |
| 22 | +query=ImmutableMultiDict(self.query_params), |
| 23 | +header=Headers(self.event.headers), |
| 24 | +cookie=ImmutableMultiDict(), |
| 25 | + ) |
| 26 | + |
| 27 | +@property |
| 28 | +defquery_params(self)->Dict[str,str]: |
| 29 | +params=self.event.queryStringParameters |
| 30 | +ifparamsisNone: |
| 31 | +return {} |
| 32 | +returnparams |
| 33 | + |
| 34 | +@property |
| 35 | +defproto(self)->str: |
| 36 | +returnself.event.headers.get("X-Forwarded-Proto","https") |
| 37 | + |
| 38 | +@property |
| 39 | +defhost(self)->str: |
| 40 | +returnself.event.headers["Host"] |
| 41 | + |
| 42 | +@property |
| 43 | +defhost_url(self)->str: |
| 44 | +return"://".join([self.proto,self.host]) |
| 45 | + |
| 46 | +@property |
| 47 | +defpath(self)->str: |
| 48 | +returnself.event.resource |
| 49 | + |
| 50 | +@property |
| 51 | +defmethod(self)->str: |
| 52 | +returnself.event.httpMethod.lower() |
| 53 | + |
| 54 | +@property |
| 55 | +defbody(self)->Optional[str]: |
| 56 | +returnself.event.body |
| 57 | + |
| 58 | +@property |
| 59 | +defmimetype(self)->str: |
| 60 | +returnself.event.headers.get("Content-Type","") |
| 61 | + |
| 62 | + |
| 63 | +classAPIGatewayEventV2OpenAPIRequest: |
| 64 | +""" |
| 65 | + Converts an API Gateway event v2 payload to an OpenAPI request |
| 66 | + """ |
| 67 | + |
| 68 | +def__init__(self,payload:APIGatewayEventPayload): |
| 69 | +self.event=APIGatewayEventV2(**payload) |
| 70 | + |
| 71 | +self.parameters=RequestParameters( |
| 72 | +query=ImmutableMultiDict(self.query_params), |
| 73 | +header=Headers(self.event.headers), |
| 74 | +cookie=ImmutableMultiDict(), |
| 75 | + ) |
| 76 | + |
| 77 | +@property |
| 78 | +defquery_params(self)->Dict[str,str]: |
| 79 | +ifself.event.queryStringParametersisNone: |
| 80 | +return {} |
| 81 | +returnself.event.queryStringParameters |
| 82 | + |
| 83 | +@property |
| 84 | +defproto(self)->str: |
| 85 | +returnself.event.headers.get("x-forwarded-proto","https") |
| 86 | + |
| 87 | +@property |
| 88 | +defhost(self)->str: |
| 89 | +returnself.event.headers["host"] |
| 90 | + |
| 91 | +@property |
| 92 | +defhost_url(self)->str: |
| 93 | +return"://".join([self.proto,self.host]) |
| 94 | + |
| 95 | +@property |
| 96 | +defpath(self)->str: |
| 97 | +returnself.event.rawPath |
| 98 | + |
| 99 | +@property |
| 100 | +defmethod(self)->str: |
| 101 | +returnself.event.routeKey.lower() |
| 102 | + |
| 103 | +@property |
| 104 | +defbody(self)->Optional[str]: |
| 105 | +returnself.event.body |
| 106 | + |
| 107 | +@property |
| 108 | +defmimetype(self)->str: |
| 109 | +returnself.event.headers.get("content-type","") |