File size: 2,466 Bytes
079c32c |
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
from abc import ABCMeta
from typing import Mapping, Any
from requests.exceptions import HTTPError
from ..base import get_values_from_response
class _IResponseInformation(metaclass=ABCMeta):
"""
Overview:
Response information basic structure interface
"""
@property
def success(self) -> bool:
"""
Overview:
Get response success or not
Returns:
- success (:obj:`bool`): Response success or not
"""
raise NotImplementedError
@property
def code(self) -> int:
"""
Overview:
Get response error code (`0` means success)
Returns:
- code (:obj:`int`): Response error code
"""
raise NotImplementedError
@property
def message(self) -> str:
"""
Overview:
Get response message
Returns:
- message (:obj:`str`): Response message
"""
raise NotImplementedError
@property
def data(self) -> Mapping[str, Any]:
"""
Overview:
Get response data
Returns:
- data (:obj:`Mapping[str, Any]`): Response data
"""
raise NotImplementedError
# exception class for processing response
class ResponseException(Exception, _IResponseInformation, metaclass=ABCMeta):
"""
Overview:
Response exception, which can be directly raised in methods to create fail http response.
"""
def __init__(self, error: HTTPError):
"""
Overview:
Constructor of `ResponseException`
Arguments:
- error (:obj:`HTTPError`): Original http exception object
"""
self.__error = error
self.__status_code, self.__success, self.__code, self.__message, self.__data = \
get_values_from_response(error.response)
Exception.__init__(self, self.__message)
@property
def status_code(self) -> int:
"""
Overview:
Get http status code of response
Returns:
- status_code (:obj:`int`): Http status code
"""
return self.__status_code
@property
def success(self) -> bool:
return self.__success
@property
def code(self) -> int:
return self.__code
@property
def message(self) -> str:
return self.__message
@property
def data(self) -> Mapping[str, Any]:
return self.__data
|