forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
63 lines (46 loc) · 1.86 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import logging
from abc import ABC, abstractmethod
from typing import Any, Dict, Optional, TypeVar, Union
from ..types import Model
logger = logging.getLogger(__name__)
class BaseEnvelope(ABC):
"""ABC implementation for creating a supported Envelope"""
@staticmethod
def _parse(data: Optional[Union[Dict[str, Any], Any]], model: Model) -> Union[Model, None]:
"""Parses envelope data against model provided
Parameters
----------
data : Dict
Data to be parsed and validated
model : Model
Data model to parse and validate data against
Returns
-------
Any
Parsed data
"""
if data is None:
logger.debug("Skipping parsing as event is None")
return data
logger.debug("parsing event against model")
if isinstance(data, str):
logger.debug("parsing event as string")
return model.parse_raw(data)
return model.parse_obj(data)
@abstractmethod
def parse(self, data: Optional[Union[Dict[str, Any], Any]], model: Model):
"""Implementation to parse data against envelope model, then against the data model
NOTE: Call `_parse` method to fully parse data with model provided.
Example
-------
**EventBridge envelope implementation example**
def parse(...):
# 1. parses data against envelope model
parsed_envelope = EventBridgeModel(**data)
# 2. parses portion of data within the envelope against model
return self._parse(data=parsed_envelope.detail, model=data_model)
"""
return NotImplemented # pragma: no cover
# Generic to support type annotations throughout parser
# Note: Can't be defined under types.py due to circular dependency
Envelope = TypeVar("Envelope", bound=BaseEnvelope)