first commit
This commit is contained in:
29
app/serviceInput/base.py
Normal file
29
app/serviceInput/base.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from typing import Any, Dict, Optional, Generic, TypeVar
|
||||
from pydantic import BaseModel
|
||||
from datetime import datetime
|
||||
|
||||
InputType = TypeVar("InputType", bound=BaseModel)
|
||||
|
||||
class ServiceInput(Generic[InputType]):
|
||||
""" Base unificada de entradas """
|
||||
|
||||
def __init__(self, raw_data: Any, source: str = "api"):
|
||||
self.raw_data = raw_data
|
||||
self.source = source
|
||||
self.timestamp = datetime.utcnow()
|
||||
self._validated = None
|
||||
|
||||
def validate(self, schema: type[InputType]) -> InputType:
|
||||
"""Validacion de datos en contra de un schema"""
|
||||
self._validated = schema(**self.raw_data)
|
||||
return self._validated
|
||||
|
||||
def get_validated(self) -> Optional[InputType]:
|
||||
return self._validated
|
||||
|
||||
def to_dict(self) -> Dict:
|
||||
return {
|
||||
"source": self.source,
|
||||
"timestamp": self.timestamp.isoformat(),
|
||||
"data": self.raw_data,
|
||||
}
|
||||
Reference in New Issue
Block a user