-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathmodels.py
More file actions
169 lines (113 loc) · 3.96 KB
/
Copy pathmodels.py
File metadata and controls
169 lines (113 loc) · 3.96 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8 -*-
class Model(object):
@classmethod
def from_dict(cls, data, **kwargs):
return cls(**data)
def __repr__(self):
return "<%s %s>" % (self.__class__.__name__, self.__dict__)
def __eq__(self, other):
return other and self.__dict__ == other.__dict__
def __ne__(self, other):
return not self.__eq__(other)
class ModelCollection(object):
def __init__(self, size, collection):
super(ModelCollection, self).__init__()
self.size = size
self.collection = collection
self._current = None
self._index = 0
@classmethod
def parse(cls, response, model):
size = response.get('size')
collection = response.get('collection', [])
c = [model.from_dict(d) for d in collection]
return cls(size, c)
def __iter__(self):
return self
def __next__(self):
return self.next()
def next(self):
try:
self._current = self.collection[self._index]
self._index += 1
except IndexError:
raise StopIteration
return self._current
def __repr__(self):
return "<%s [%s]> %s" % (self.__class__.__name__, self.size, self.collection)
def __eq__(self, other):
return other and self.__dict__ == other.__dict__
class ResultCollection(object):
def __init__(self, count, results):
super(ResultCollection, self).__init__()
self.count = count
self.results = results
self._current = None
self._index = 0
@classmethod
def parse(cls, content, model):
collection = []
if isinstance(content, dict):
count = content.get('count')
content = content.get('list', [])
else:
count = len(content)
for data in content:
m = model.from_dict(data)
collection.append(m)
return cls(count, collection)
def __iter__(self):
return self
def __next__(self):
return self.next()
def next(self):
try:
self._current = self.results[self._index]
self._index += 1
except IndexError:
raise StopIteration
return self._current
def __repr__(self):
return "<%s [%s]> %s" % (self.__class__.__name__, self.count, self.results)
def __eq__(self, other):
return other and self.__dict__ == other.__dict__
class SendResult(Model):
def __init__(self, **kwargs):
super(SendResult, self).__init__()
self.id = kwargs.get("id")
self.points = kwargs.get("points")
self.number = kwargs.get("number")
self.date_sent = kwargs.get("date_sent")
self.submitted_number = kwargs.get("submitted_number")
self.status = kwargs.get("status")
self.idx = kwargs.get("idx")
self.error = kwargs.get("error")
self.parts = kwargs.get("parts")
class RemoveMessageResult(Model):
def __init__(self, id=None):
super(RemoveMessageResult, self).__init__()
self.id = id
class InvalidNumber(object):
def __init__(self, number, submitted_number, reason):
super(InvalidNumber, self).__init__()
self.number = number
self.submitted_number = submitted_number
self.reason = reason
@classmethod
def from_dict(cls, data):
return cls(data.get('number'), data.get('submitted_number'), data.get('message'))
def __eq__(self, other):
if other is None:
return False
if isinstance(other, dict):
return self.__dict__ == other
if isinstance(other, self.__class__):
return self.__dict__ == other.__dict__
return False
class HeaderDirectResult(object):
def __init__(self, header):
super(HeaderDirectResult, self).__init__()
self.header = header
def from_dict(self, _, **kwargs):
r = kwargs.get('raw_response')
return r.headers.get(self.header)