Skip to content

Commit aa39958

Browse files
committed
Code polishing: helpers to validate input, BulkResult class instead of tuple
1 parent ae41ed8 commit aa39958

11 files changed

Lines changed: 193 additions & 199 deletions

splitapiclient/microclients/attribute_microclient.py

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from splitapiclient.util.exceptions import HTTPResponseError, \
33
UnknownApiClientError
44
from splitapiclient.util.logger import LOGGER
5-
5+
from splitapiclient.util.helpers import as_dict
66

77
class AttributeMicroClient:
88
'''
@@ -56,18 +56,11 @@ def list(self, traffic_type_id):
5656
returned
5757
:rtype: list(TrafficType)
5858
'''
59-
try:
60-
response = self._http_client.make_request(
61-
self._endpoint['all_items'],
62-
trafficTypeId=traffic_type_id
63-
)
64-
return [Attribute(item, self._http_client) for item in response]
65-
except HTTPResponseError as e:
66-
LOGGER.error('Error retrieving items')
67-
raise e
68-
except Exception as e:
69-
LOGGER.debug(e)
70-
raise UnknownApiClientError()
59+
response = self._http_client.make_request(
60+
self._endpoint['all_items'],
61+
trafficTypeId=traffic_type_id
62+
)
63+
return [Attribute(item, self._http_client) for item in response]
7164

7265
def save(self, attribute):
7366
'''
@@ -79,48 +72,35 @@ def save(self, attribute):
7972
:returns: newly created attribute
8073
:rtype: Attribute
8174
'''
82-
# TODO: Validate!
83-
try:
84-
data = attribute.to_dict() if isinstance(attribute, Attribute) else attribute
85-
response = self._http_client.make_request(
86-
self._endpoint['create'],
87-
data,
88-
trafficTypeId=data['trafficTypeId']
89-
)
75+
data = as_dict(attribute)
76+
response = self._http_client.make_request(
77+
self._endpoint['create'],
78+
data,
79+
trafficTypeId=data.get('trafficTypeId')
80+
)
81+
return Attribute(response, self._http_client)
9082

91-
return Attribute(response, self._http_client)
83+
def delete(self, attribute_id, traffic_type_id):
84+
'''
85+
Delete an attribute by specifying its id and it's traffic type id.
9286
93-
except HTTPResponseError as e:
94-
LOGGER.error('Call to Split API failed. Attribute not created.')
95-
raise e
96-
except Exception as e:
97-
LOGGER.exception(e)
98-
raise UnknownApiClientError()
87+
:param attribute_id: attribute's id
88+
:param traffic_type_id: atribute's traffic type id
89+
'''
90+
return self._http_client.make_request(
91+
self._endpoint['delete'],
92+
trafficTypeId=traffic_type_id,
93+
attributeId=attribute_id
94+
)
9995

10096
def delete_by_instance(self, attribute):
10197
'''
10298
Delete an attribute
10399
104100
:param attribute: Attribute instance
105101
'''
106-
return self.delete(attribute.id, attribute.traffic_type_id)
107-
108-
def delete(self, attribute_id, traffic_type_id):
109-
'''
110-
Delete an attribute by specifying its id and it's traffic type id.
111-
112-
:param attribute_id: attribute's id
113-
:param traffic_type_id: atribute's traffic type id
114-
'''
115-
try:
116-
return self._http_client.make_request(
117-
self._endpoint['delete'],
118-
trafficTypeId=traffic_type_id,
119-
attributeId=attribute_id
120-
)
121-
except HTTPResponseError as e:
122-
LOGGER.error('Call to Split API failed. Attribute not deleted.')
123-
raise e
124-
except Exception as e:
125-
LOGGER.debug(e)
126-
raise UnknownApiClientError()
102+
data = as_dict(attribute)
103+
return self.delete(
104+
data.get('id'),
105+
data.get('trafficTypeId')
106+
)

splitapiclient/microclients/environment_microclient.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
UnknownApiClientError
44
from splitapiclient.util.logger import LOGGER
55

6-
76
class EnvironmentMicroClient:
87
'''
98
'''
@@ -34,14 +33,7 @@ def list(self):
3433
:returns: list of Environment objects
3534
:rtype: list(Environment)
3635
'''
37-
try:
38-
response = self._http_client.make_request(
39-
self._endpoint['all_items']
40-
)
41-
return [Environment(item, self._http_client) for item in response]
42-
except HTTPResponseError as e:
43-
LOGGER.error('Error retrieving items')
44-
raise e
45-
except Exception as e:
46-
LOGGER.debug(e)
47-
raise UnknownApiClientError()
36+
response = self._http_client.make_request(
37+
self._endpoint['all_items']
38+
)
39+
return [Environment(item, self._http_client) for item in response]

splitapiclient/microclients/identity_microclient.py

Lines changed: 68 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from splitapiclient.util.exceptions import HTTPResponseError, \
33
UnknownApiClientError
44
from splitapiclient.util.logger import LOGGER
5+
from splitapiclient.util.helpers import as_dict
6+
from splitapiclient.util.bulk_result import BulkOperationResult
57

68

79
class IdentityMicroClient:
@@ -86,22 +88,14 @@ def save(self, identity):
8688
:returns: newly created Identity
8789
:rtype: Identity
8890
'''
89-
try:
90-
data = identity.to_dict() if isinstance(identity, Identity) else identity
91-
response = self._http_client.make_request(
92-
self._endpoint['create'],
93-
data,
94-
key=data.get('key'),
95-
trafficTypeId=data.get('trafficTypeId'),
96-
environmentId=data.get('environmentId'),
97-
)
98-
except HTTPResponseError as e:
99-
LOGGER.error('Call to Split API failed. Identity not created.')
100-
raise e
101-
except Exception as e:
102-
LOGGER.debug(e)
103-
raise UnknownApiClientError()
104-
91+
data = as_dict(identity)
92+
response = self._http_client.make_request(
93+
self._endpoint['create'],
94+
data,
95+
key=data.get('key'),
96+
trafficTypeId=data.get('trafficTypeId'),
97+
environmentId=data.get('environmentId'),
98+
)
10599
return Identity(response, self._http_client)
106100

107101
def save_all(self, identities):
@@ -116,42 +110,29 @@ def save_all(self, identities):
116110
for the failed item togegther with a status code and a message
117111
:rtype: tuple
118112
'''
119-
# TODO: Validate!
120-
try:
121-
# convert Identity objects to dict if necessary
122-
to_save = [
123-
i.to_dict() if isinstance(i, Identity) else i
124-
for i in identities
125-
]
126-
response = self._http_client.make_request(
127-
self._endpoint['create_many'],
128-
to_save,
129-
trafficTypeId=to_save[0]['trafficTypeId'],
130-
environmentId=to_save[0]['environmentId']
131-
)
132-
133-
successful = [
134-
Identity(i, self._http_client)
135-
for i in response['objects']
136-
]
137-
138-
failed = [
139-
{
140-
'object': Identity(i['object'], self._http_client),
141-
'status': i['status'],
142-
'message': i['message'],
143-
}
144-
for i in response['failed']
145-
]
146-
147-
return successful, failed
148-
149-
except HTTPResponseError as e:
150-
LOGGER.error('Call to Split API failed. Identities not created.')
151-
raise e
152-
except Exception as e:
153-
LOGGER.debug(e)
154-
raise UnknownApiClientError()
113+
to_save = [as_dict(i) for i in identities]
114+
response = self._http_client.make_request(
115+
self._endpoint['create_many'],
116+
to_save,
117+
trafficTypeId=to_save[0].get('trafficTypeId'),
118+
environmentId=to_save[0].get('environmentId')
119+
)
120+
121+
successful = [
122+
Identity(i, self._http_client)
123+
for i in response.get('objects', [])
124+
]
125+
126+
failed = [
127+
{
128+
'object': Identity(i['object'], self._http_client),
129+
'status': i['status'],
130+
'message': i['message'],
131+
}
132+
for i in response.get('failed', [])
133+
]
134+
135+
return BulkOperationResult(successful, failed, response.get('metadata'))
155136

156137
def update(self, identity):
157138
'''
@@ -163,22 +144,14 @@ def update(self, identity):
163144
:returns: updated Identity
164145
:rtype: Identity
165146
'''
166-
try:
167-
data = identity.to_dict() if isinstance(identity, Identity) else identity
168-
response = self._http_client.make_request(
169-
self._endpoint['update'],
170-
data,
171-
key=data.get('key'),
172-
trafficTypeId=data.get('trafficTypeId'),
173-
environmentId=data.get('environmentId'),
174-
)
175-
except HTTPResponseError as e:
176-
LOGGER.error('Call to Split API failed. Identity not created.')
177-
raise e
178-
except Exception as e:
179-
LOGGER.debug(e)
180-
raise UnknownApiClientError()
181-
147+
data = as_dict(identity)
148+
response = self._http_client.make_request(
149+
self._endpoint['update'],
150+
data,
151+
key=data.get('key'),
152+
trafficTypeId=data.get('trafficTypeId'),
153+
environmentId=data.get('environmentId'),
154+
)
182155
return Identity(response, self._http_client)
183156

184157
def patch(self, identity):
@@ -191,35 +164,15 @@ def patch(self, identity):
191164
:returns: patched Identity
192165
:rtype: Identity
193166
'''
194-
try:
195-
data = identity.to_dict() if isinstance(identity, Identity) else identity
196-
response = self._http_client.make_request(
197-
self._endpoint['patch'],
198-
data,
199-
key=data.get('key'),
200-
trafficTypeId=data.get('trafficTypeId'),
201-
environmentId=data.get('environmentId'),
202-
)
203-
except HTTPResponseError as e:
204-
LOGGER.error('Call to Split API failed. Identity not created.')
205-
raise e
206-
except Exception as e:
207-
LOGGER.debug(e)
208-
raise UnknownApiClientError()
209-
210-
return Identity(response, self._http_client)
211-
212-
def delete_by_instance(self, identity):
213-
'''
214-
Delete the identity
215-
216-
:param identity: Identity instance
217-
'''
218-
return self.delete(
219-
identity.traffic_type_id,
220-
identity.environment_id,
221-
identity.key
167+
data = as_dict(identity)
168+
response = self._http_client.make_request(
169+
self._endpoint['patch'],
170+
data,
171+
key=data.get('key'),
172+
trafficTypeId=data.get('trafficTypeId'),
173+
environmentId=data.get('environmentId'),
222174
)
175+
return Identity(response, self._http_client)
223176

224177
def delete(self, traffic_type_id, environment_id, key):
225178
'''
@@ -230,16 +183,22 @@ def delete(self, traffic_type_id, environment_id, key):
230183
:param environment_id: Identity's environment id
231184
:param key: Identity's key
232185
'''
233-
try:
234-
self._http_client.make_request(
235-
self._endpoint['delete_attributes'],
236-
key=key,
237-
trafficTypeId=traffic_type_id,
238-
environmentId=environment_id
239-
)
240-
except HTTPResponseError as e:
241-
LOGGER.error('Call to Split API failed. Identity not patched.')
242-
raise e
243-
except Exception as e:
244-
LOGGER.debug(e)
245-
raise UnknownApiClientError()
186+
self._http_client.make_request(
187+
self._endpoint['delete_attributes'],
188+
key=key,
189+
trafficTypeId=traffic_type_id,
190+
environmentId=environment_id
191+
)
192+
193+
def delete_by_instance(self, identity):
194+
'''
195+
Delete the identity
196+
197+
:param identity: Identity instance
198+
'''
199+
data = as_dict(identity)
200+
return self.delete(
201+
data.get('trafficTypeId'),
202+
data.get('environmentId'),
203+
data.get('key')
204+
)

splitapiclient/microclients/traffic_type_microclient.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,7 @@ def list(self):
3434
:returns: List of TrafficType objects
3535
:rtype: list(TrafficType)
3636
'''
37-
try:
38-
response = self._http_client.make_request(
39-
self._endpoint['all_items']
40-
)
41-
return [TrafficType(item, self._http_client) for item in response]
42-
except HTTPResponseError as e:
43-
LOGGER.error('Error retrieving items')
44-
raise e
45-
except Exception as e:
46-
LOGGER.debug(e)
47-
raise UnknownApiClientError()
37+
response = self._http_client.make_request(
38+
self._endpoint['all_items']
39+
)
40+
return [TrafficType(item, self._http_client) for item in response]

0 commit comments

Comments
 (0)