-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
208 lines (155 loc) · 6.23 KB
/
base.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from datetime import datetime
import json
import requests
import inflection
import dateutil.parser
import recordtype
from kalibro_client.errors import KalibroClientSaveError, KalibroClientDeleteError, \
KalibroClientNotFoundError, KalibroClientRequestError
class RequestMethods(object):
def save_prefix(self):
return ""
def update_prefix(self):
return ""
def delete_prefix(self):
return ""
@classmethod
def entity_name(cls):
raise NotImplementedError
@classmethod
def endpoint(cls):
return inflection.pluralize(cls.entity_name())
@classmethod
def service_address(cls):
raise NotImplementedError
@classmethod
def request(cls, action, params=None, method='post', prefix=None):
url = cls.service_address()
if prefix:
url += "/" + prefix
url += "/{}/{}".format(cls.endpoint(), action)
if params is not None and 'id' in params:
url = url.replace(':id', str(params.pop('id')))
response = requests.request(method, url, data=json.dumps(params),
headers={'Content-Type': 'application/json'})
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
raise KalibroClientRequestError(response)
return response.json()
class Base(object):
@classmethod
def _is_valid_field(cls, name):
return name in cls._fields
@classmethod
def response_to_objects_array(cls, response):
array = response[inflection.pluralize(cls.entity_name())]
return cls.array_to_objects_array(array)
@classmethod
def array_to_objects_array(cls, array):
return [cls(**attributes) for attributes in array]
class BaseCRUD(Base, RequestMethods):
@classmethod
def find(cls, id):
try:
response = cls.request(':id', params={'id': id}, method='get')
except KalibroClientRequestError as error:
body = error.response.json()
raise KalibroClientNotFoundError(body['errors'])
return cls(**response[cls.entity_name()])
@classmethod
def all(cls):
return cls.response_to_objects_array(cls.request('', method='get'))
@classmethod
def exists(cls, id):
return cls.request(':id/exists', params={'id': id}, method='get')['exists']
def _update_request(self):
return self.request(action=str(self.id),
params={self.entity_name(): self._asdict(), 'id': str(self.id)},
method='put',
prefix=self.update_prefix())
def save(self):
if not self.id:
response = self.request(action='',
params={self.entity_name(): self._asdict()},
prefix=self.save_prefix())
else:
response = self._update_request()
if 'errors' not in response:
response_body = response[self.entity_name()]
self.id = response_body['id']
self.created_at = response_body['created_at']
self.updated_at = response_body['updated_at']
else:
raise KalibroClientSaveError(response['errors'])
def update(self, **kwargs):
if not self.id:
raise KalibroClientSaveError("Cannot update a record that is not saved.")
for attr, value in kwargs.items():
if self._is_valid_field(attr):
setattr(self, attr, value)
try:
response = self._update_request()
except KalibroClientRequestError as error:
body = error.response.json()
raise KalibroClientSaveError(body['errors'])
response_body = response[self.entity_name()]
self.updated_at = response_body['updated_at']
def delete(self):
if not isinstance(self.id, (int, long)):
raise KalibroClientDeleteError('Can not delete object without id')
response = self.request(action=':id', params={'id': self.id}, method='delete', prefix=self.delete_prefix())
if 'errors' in response:
raise KalibroClientDeleteError(response['errors'])
def entity_name_decorator(top_cls):
"""
Assign an entity name based on the class immediately inhering from Base.
This is needed because we don't want
entity names to come from any class that simply inherits our classes,
just the ones in our module.
For example, if you create a class Project2 that exists outside of
kalibro_client and inherits from Project, it's entity name should still
be Project.
"""
class_name = inflection.underscore(top_cls.__name__).lower()
def entity_name(cls):
return class_name
top_cls.entity_name = classmethod(entity_name)
return top_cls
def attributes_class_constructor(name, fields, identity=True, *args, **kwargs):
if not identity:
return recordtype.recordtype(name, fields, *args, **kwargs)
# Create the new class, inheriting from the record type and from this
# class
class IdentityClass(recordtype.recordtype(name, fields, *args, **kwargs)):
def __init__(self, id=None, created_at=None, updated_at=None,
*init_args, **init_kwargs):
super(IdentityClass, self).__init__(*init_args, **init_kwargs)
self.id = id
self.created_at = created_at
self.updated_at = updated_at
@property
def id(self):
return self._id
@id.setter
def id(self, value):
if value is not None:
value = int(value)
self._id = value
@property
def created_at(self):
return self._created_at
@created_at.setter
def created_at(self, value):
if value is not None and not isinstance(value, datetime):
value = dateutil.parser.parse(value)
self._created_at = value
@property
def updated_at(self):
return self._updated_at
@updated_at.setter
def updated_at(self, value):
if value is not None and not isinstance(value, datetime):
value = dateutil.parser.parse(value)
self._updated_at = value
return IdentityClass