__init__.py 2.72 KB
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
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
import requests
import json
from enum import Enum

class Result(dict):
    '''
    类Result用于封装返回结果,包含success、message、data三个属性:
        success (bool): 是否成功
        message (str): 消息
        data (any): 数据
    '''
    __setattr__ = dict.__setitem__
    __getattr__ = dict.__getitem__
    def __init__(self):
        super(Result, self).__init__()
        self["code"] = 200
        self["message"] = ""
        self["data"] = None
    
    @property
    def success(self) -> bool:
        return self["code"] == 200

    @property
    def data(self) -> any:
        return self["data"]

    @property
    def message(self) -> str:
        return str(self["message"])
        
    @staticmethod
    def error(code: int = 500, message: str = "") -> "Result":
        r = Result()
        r["code"] = code
        r["message"] = message
        return r

    @staticmethod
    def ok_data(data = None) -> "Result":
        r = Result()
        r["code"] = 200
        r["data"] = data
        return r
        
    @staticmethod
    def ok_msg(message: str) -> "Result":
        r = Result()
        r["message"] = message
        return r

    def append(self, k: str, v) -> "Result":
        self[k] = v
        return self
    
    @classmethod
    def from_response(cls, resp: requests.Response) -> "Result":
        self = cls()
        self["code"] = resp.status_code
        if self.success:
            content_dict = json.loads(resp.content.decode('utf-8'))
Wei Shoulin's avatar
plan    
Wei Shoulin committed
62
            if isinstance(content_dict, dict) and 'total_count' in content_dict and 'records' in content_dict:
Wei Shoulin's avatar
Wei Shoulin committed
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
                self["data"] = content_dict['records']
                self["total_count"] = content_dict['total_count']
            else:
                self["data"] = content_dict
            self['data'] = DictObject.from_json(self['data'])
            self['message'] = resp.url
        else:
            try:
                content_dict = json.loads(resp.content.decode('utf-8'))
                self["message"] = content_dict.get("detail", "")
            except Exception as _:
                pass
        return self

class DictObject(dict):
    def __init__(self, **kwargs):
        super(DictObject, self).__init__(**kwargs)
    def __getattr__(self, name):
        if name in self.keys():
            return self[name]
        return None
    @staticmethod
    def from_json(data):
        if not data:
            return data
        if isinstance(data, list):
            return [DictObject.from_json(i) for i in data]
        elif isinstance(data, dict):
            return DictObject(**data)
        else:
            return data
    
class ModuleID(Enum):
    MSC = "MSC"
    IFS = "IFS"
    MCI = "MCI"
    HSTDM = "HSTDM"
    CPIC = "CPIC"