-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_core.py
53 lines (44 loc) · 2 KB
/
response_core.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
"""module for unified response from API"""
from typing import Any
from pydantic import BaseModel
class BasePydanticResponse(BaseModel):
"""Base pydantic class for unified response"""
success: bool
api: str
data: Any # if fact this attr is pydantic object or None
error: Any # if fact this attr is pydantic object or None
class BaseResponse:
"""Base interface class. Specifies response format"""
def __init__(self, status_code: int, success: bool, api: str,
data: [BaseModel, None], error: [BaseModel, None]):
self.status_code = status_code # HTTP status code
self.response = BasePydanticResponse(
success=success,
api=api,
data=data,
error=error
)
class SuccessResponse(BaseResponse):
"""Class for success response from API"""
def __init__(self, data: BaseModel, api: str, status_code=200, success=True, error=None):
"""
Args:
data: any custom pydantic object with data of successful response
api: identified API (any string value)
status_code: HTTP status code
success: bool flag (in success = True)
error: any custom pydantic object with data of error response (in success = None)
"""
super().__init__(status_code=status_code, success=success, data=data, error=error, api=api)
class ErrorResponse(BaseResponse):
"""Class for error response from API"""
def __init__(self, error: BaseModel, api: str, status_code: int, success=False, data=None):
"""
Args:
data: any custom pydantic object with data of successful response (in error = None)
api: identified API (any string value)
status_code: HTTP status code
success: bool flag (in error = False)
error: any custom pydantic object with data of error response
"""
super().__init__(status_code=status_code, success=success, data=data, error=error, api=api)