new api service and logic implemented
This commit is contained in:
68
Services/Redis/Models/response.py
Normal file
68
Services/Redis/Models/response.py
Normal file
@@ -0,0 +1,68 @@
|
||||
from typing import Union, Dict, List, Optional, Any
|
||||
from Services.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 isinstance(data, RedisRow):
|
||||
self.data_type = "row"
|
||||
elif data is None:
|
||||
self.data_type = None
|
||||
self.error = error
|
||||
|
||||
def as_dict(self) -> Dict:
|
||||
data = self.all
|
||||
main_dict = {
|
||||
"status": self.status,
|
||||
"message": self.message,
|
||||
"count": self.count,
|
||||
"dataType": getattr(self, "data_type", None),
|
||||
}
|
||||
if isinstance(data, RedisRow):
|
||||
dict_return = {data.keys: data.row}
|
||||
dict_return.update(dict(main_dict))
|
||||
return dict_return
|
||||
elif isinstance(data, list):
|
||||
dict_return = {row.keys: row.data for row in data}
|
||||
dict_return.update(dict(main_dict))
|
||||
return dict_return
|
||||
|
||||
@property
|
||||
def all(self) -> Union[Optional[List[RedisRow]]]:
|
||||
return self.data or []
|
||||
|
||||
@property
|
||||
def count(self) -> int:
|
||||
print()
|
||||
row = self.all
|
||||
if isinstance(row, list):
|
||||
return len(row)
|
||||
elif isinstance(row, RedisRow):
|
||||
return 1
|
||||
|
||||
@property
|
||||
def first(self) -> Union[RedisRow, None]:
|
||||
print("self.data", self.data)
|
||||
if self.data:
|
||||
if isinstance(self.data, list):
|
||||
return self.data[0]
|
||||
elif isinstance(self.data, RedisRow):
|
||||
return self.row
|
||||
self.status = False
|
||||
return
|
||||
Reference in New Issue
Block a user