__init__.py 1.36 KB
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
1
2
3
4
# coding: utf-8
class Result(dict):
    def __init__(self):
        super(Result, self).__init__()
Wei Shoulin's avatar
error    
Wei Shoulin committed
5
6
7
        self["code"] = 0
        self["message"] = ""
        self["data"] = None
Wei Shoulin's avatar
Wei Shoulin committed
8
    
Wei Shoulin's avatar
Wei Shoulin committed
9
    @property
Wei Shoulin's avatar
Wei Shoulin committed
10
    def success(self):
Wei Shoulin's avatar
Wei Shoulin committed
11
        return self["code"] == 0
Wei Shoulin's avatar
Wei Shoulin committed
12

Wei Shoulin's avatar
Wei Shoulin committed
13
    @property
Wei Shoulin's avatar
error    
Wei Shoulin committed
14
15
    def data(self):
        return self["data"]
Wei Shoulin's avatar
Wei Shoulin committed
16

Wei Shoulin's avatar
Wei Shoulin committed
17
    @property
Wei Shoulin's avatar
error    
Wei Shoulin committed
18
19
20
    def message(self):
        return self["message"]
        
Wei Shoulin's avatar
Wei Shoulin committed
21
22
23
    @staticmethod
    def error(code = -1, message = ""):
        r = Result()
Wei Shoulin's avatar
error    
Wei Shoulin committed
24
25
        r["code"] = code
        r["message"] = message
Wei Shoulin's avatar
Wei Shoulin committed
26
27
28
        return r

    @staticmethod
Wei Shoulin's avatar
Wei Shoulin committed
29
    def ok_data(data=None):
Wei Shoulin's avatar
Wei Shoulin committed
30
        r = Result()
Wei Shoulin's avatar
error    
Wei Shoulin committed
31
        r["data"] = data
Wei Shoulin's avatar
Wei Shoulin committed
32
33
34
35
36
        return r
        
    @staticmethod
    def ok_msg(message):
        r = Result()
Wei Shoulin's avatar
error    
Wei Shoulin committed
37
38
39
40
41
        r["message"] = message
        return r

    def append(self, k: str, v):
        self[k] = v
Wei Shoulin's avatar
Wei Shoulin committed
42
43
44
        return self

class Request(object):
Wei Shoulin's avatar
Wei Shoulin committed
45
    def __init__(self, **kvargs):
Wei Shoulin's avatar
Wei Shoulin committed
46
        super(Request, self).__init__()
Wei Shoulin's avatar
Wei Shoulin committed
47
48
        for k, v in kvargs.items():
            self.append(k, v)
Wei Shoulin's avatar
Wei Shoulin committed
49
50
51
52
53
54
55
56
57
58

    @staticmethod
    def from_dict(d: dict):
        r = Request()
        for k, v in d.items():
            r.append(k, v)
        return r

    def append(self, k: str, v):
        self.__setattr__(k, v)
Wei Shoulin's avatar
Wei Shoulin committed
59
60
61
62
63
64
65
66
67
        return self

    def __getattr__(self, attr):
        try:
            return object.__getattribute__(self, attr)
        except:
            return ''