| Total Complexity | 6 |
| Total Lines | 39 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | from typing import Optional, Type |
||
| 2 | |||
| 3 | from flask import current_app, g |
||
| 4 | from pydantic import BaseModel |
||
| 5 | |||
| 6 | |||
| 7 | class _ContextLocalData: |
||
| 8 | def __init__(self, key_name, default): |
||
| 9 | self.key_name = key_name |
||
| 10 | self.default = default |
||
| 11 | |||
| 12 | def get(self, _g): |
||
| 13 | return getattr(_g, self.key_name, self.default) |
||
| 14 | |||
| 15 | def set(self, _g, value): |
||
| 16 | setattr(_g, self.key_name, value) |
||
| 17 | |||
| 18 | |||
| 19 | class _ContextProperty: |
||
| 20 | class ContextLocalData: |
||
| 21 | request_payload = _ContextLocalData("request_payload", None) |
||
| 22 | |||
| 23 | @property |
||
| 24 | def secret_key(self) -> str: |
||
| 25 | return current_app.secret_key |
||
| 26 | |||
| 27 | # - request payload - |
||
| 28 | |||
| 29 | @property |
||
| 30 | def request_payload(self) -> Optional[BaseModel]: |
||
| 31 | return self.ContextLocalData.request_payload.get(g) |
||
| 32 | |||
| 33 | @request_payload.setter |
||
| 34 | def request_payload(self, value: Type[BaseModel]): |
||
| 35 | self.ContextLocalData.request_payload.set(g, value) |
||
| 36 | |||
| 37 | |||
| 38 | context_property = _ContextProperty() |
||
| 39 |