45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
from typing import Union, Dict, List, Optional, Any
|
|
from ApiServiceRedis.Redis.Models.base import RedisRow
|
|
|
|
|
|
class RedisResponse:
|
|
"""Base class for Redis response handling."""
|
|
|
|
def __init__(
|
|
self,
|
|
status: bool,
|
|
message: str,
|
|
data: Any = None,
|
|
error: Optional[str] = None,
|
|
):
|
|
self.status = status
|
|
self.message = message
|
|
self.data = data
|
|
|
|
if isinstance(data, dict):
|
|
self.data_type = "dict"
|
|
elif isinstance(data, list):
|
|
self.data_type = "list"
|
|
elif data is None:
|
|
self.data_type = None
|
|
self.error = error
|
|
|
|
def as_dict(self) -> Dict:
|
|
return {
|
|
"status": self.status,
|
|
"message": self.message,
|
|
"data": self.data,
|
|
"dataType": self.data_type,
|
|
"error": self.error,
|
|
}
|
|
|
|
@property
|
|
def all(self) -> Union[Optional[List[RedisRow]]]:
|
|
return self.data
|
|
|
|
@property
|
|
def first(self) -> Union[RedisRow, None]:
|
|
if self.data:
|
|
return self.data[0]
|
|
return None
|