Skip to content

Commit e4f9e47

Browse files
authored
Merge pull request #1 from splitio/qos-fixes
These changes are all deployed and working in the testing apps. Merging to dev
2 parents 5b8df73 + 1905a82 commit e4f9e47

31 files changed

Lines changed: 1832 additions & 1120 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,6 @@ ENV/
9090

9191
#vim backup files
9292
*.swp
93+
94+
#IDE
95+
.idea

identify/http_clients/sync_client.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from identify.http_clients import base_client
77
from identify.util.logger import LOGGER
88
from identify.util.exceptions import HTTPResponseError, HTTPNotFoundError, \
9-
HTTPIncorrectParametersError, HTTPUnauthorizedError
9+
HTTPIncorrectParametersError, HTTPUnauthorizedError, \
10+
IdentifyBackendUnreachableError
1011

1112

1213
class SyncHttpClient(base_client.BaseHttpClient):
@@ -37,7 +38,11 @@ def setup_method(self, method, body=None):
3738

3839
def _handle_invalid_response(self, response):
3940
'''
40-
TODO
41+
Handle responses that are not okay and throw an appropriate exception.
42+
If the code doesn't match the known ones, a generic HTTPResponseError
43+
is thrown
44+
45+
:param response: requests' module response object
4146
'''
4247
status_codes_exceptions = {
4348
404: HTTPNotFoundError,
@@ -51,6 +56,18 @@ def _handle_invalid_response(self, response):
5156
else:
5257
raise HTTPResponseError
5358

59+
def _handle_connection_error(self, e):
60+
'''
61+
Handle error when attempting to connect to identify backend.
62+
Logs exception thrown by requests module, and raises an
63+
IdentifyBackendUnreachableError error, so that it can be caught
64+
by using the top level IdentifyException
65+
'''
66+
LOGGER.debug(e)
67+
raise IdentifyBackendUnreachableError(
68+
'Unable to reach Identify backend'
69+
)
70+
5471
def make_request(self, endpoint, body=None, **kwargs):
5572
'''
5673
This method delegates bulding of headers, url and querystring (!)
@@ -81,7 +98,7 @@ def make_request(self, endpoint, body=None, **kwargs):
8198
try:
8299
response = method(url, headers=headers)
83100
except Exception as e:
84-
return self._connection_error(e)
101+
return self._handle_connection_error(e)
85102
finally:
86103
LOGGER.debug('RESPONSE: ' + response.text)
87104

identify/main/__init__.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
from identify.main.identify_sync_client import SyncIdentifyClient
2-
from identify.util import logger
32

43

54
def get_client(config):
65
'''
76
Entry point for the Identify API client
87
'''
98
_async = config.get('async', False)
10-
if _async: raise Exception('Async client not yet implemented')
11-
12-
if 'log_level' in config:
13-
logger.set_level(config['log_level'])
9+
if _async:
10+
raise Exception('Async client not yet implemented')
1411

1512
return SyncIdentifyClient(config)

identify/main/identify_client.py

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,18 @@ class BaseIdentifyClient:
1212
def __init__(self, config):
1313
pass
1414

15-
@abc.abstractmethod
16-
def get_traffic_types(self):
17-
pass
18-
19-
@abc.abstractmethod
20-
def get_environments(self):
21-
pass
22-
23-
@abc.abstractmethod
24-
def get_attributes_for_traffic_type(self, traffic_type_id):
25-
pass
26-
27-
@abc.abstractmethod
28-
def create_attribute_for_traffic_type(self, traffic_type_id, attr_data):
29-
pass
30-
31-
@abc.abstractmethod
32-
def delete_attribute_from_schema(self, traffic_type_id, attribute_id):
15+
@abc.abstractproperty
16+
def traffic_type(self):
3317
pass
3418

35-
@abc.abstractmethod
36-
def add_identities(self, traffic_type_id, environment_id, entities):
19+
@abc.abstractproperty
20+
def environment(self):
3721
pass
3822

39-
@abc.abstractmethod
40-
def add_identity(self, traffic_type_id, environment_id, key, values):
23+
@abc.abstractproperty
24+
def attribute(self):
4125
pass
4226

43-
@abc.abstractmethod
44-
def update_identity(self, traffic_type_id, environment_id, key, values):
45-
pass
46-
47-
@abc.abstractmethod
48-
def patch_identity(self, traffic_type_id, environment_id, key, values):
49-
pass
50-
51-
@abc.abstractmethod
52-
def delete_attributes_from_key(self, traffic_type_id, environment_id, key):
27+
@abc.abstractproperty
28+
def identity(self):
5329
pass

identify/main/identify_sync_client.py

Lines changed: 21 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
unicode_literals
33
from identify.main.identify_client import BaseIdentifyClient
44
from identify.http_clients.sync_client import SyncHttpClient
5-
from identify.resources.traffic_type import TrafficType
6-
from identify.resources.environment import Environment
7-
from identify.resources.attribute import Attribute
8-
from identify.resources.identity import Identity
95
from identify.util.exceptions import InsufficientConfigArgumentsException
6+
from identify.microclients import TrafficTypeMicroClient
7+
from identify.microclients import EnvironmentMicroClient
8+
from identify.microclients import IdentityMicroClient
9+
from identify.microclients import AttributeMicroClient
1010

1111

1212
class SyncIdentifyClient(BaseIdentifyClient):
@@ -33,167 +33,25 @@ def __init__(self, config):
3333
% ','.join(missing)
3434
)
3535

36-
self._client = SyncHttpClient(self._base_url, self._apikey)
36+
http_client = SyncHttpClient(self._base_url, self._apikey)
3737

38-
def get_traffic_types(self):
39-
'''
40-
Returns a list of TrafficType objects.
41-
'''
42-
return TrafficType.retrieve_all(self._client)
38+
self._traffic_type_client = TrafficTypeMicroClient(http_client)
39+
self._environment_client = EnvironmentMicroClient(http_client)
40+
self._attribute_client = AttributeMicroClient(http_client)
41+
self._identity_client = IdentityMicroClient(http_client)
4342

44-
def get_environments(self):
45-
'''
46-
Returns a list of environments.
47-
'''
48-
return Environment.retrieve_all(self._client)
43+
@property
44+
def traffic_type(self):
45+
return self._traffic_type_client
4946

50-
def get_attributes_for_traffic_type(self, traffic_type_id):
51-
'''
52-
Returns a list of attributes for a particular traffic type.
47+
@property
48+
def environment(self):
49+
return self._environment_client
5350

54-
:param traffic_type_id: Id of the traffic type whose attributes are to
55-
be retrieved.
56-
'''
57-
return Attribute.retrieve_all(
58-
self._client,
59-
trafficTypeId=traffic_type_id
60-
)
61-
62-
def create_attribute_for_traffic_type(self, traffic_type_id, attr_data):
63-
'''
64-
Creates an attribute for a specific traffic type.
51+
@property
52+
def attribute(self):
53+
return self._attribute_client
6554

66-
:param traffic_type_id: Id of the traffic type for which an attribute
67-
will be created.
68-
:param attr_data: Dictionary with the the data to create the new
69-
attribute. Should include the followind fields:
70-
- 'id': Id of the new attribute
71-
- 'displayName': Display Name of the new attribute
72-
- 'description': Description of the new attribute
73-
- 'dataType': Data type of the new attribute
74-
'''
75-
return Attribute.create(
76-
self._client,
77-
attr_data.get('id'),
78-
traffic_type_id,
79-
attr_data.get('displayName'),
80-
attr_data.get('description'),
81-
attr_data.get('dataType')
82-
)
83-
84-
def delete_attribute_from_schema(self, traffic_type_id, attribute_id):
85-
'''
86-
Delete an attribute from a particular traffic type.
87-
88-
:param traffic_type_id: Trafic id of the schema whose attribute
89-
will be removed.
90-
:param attribute_id: Id of the attribute to be removed.
91-
'''
92-
return Attribute.delete(self._client, attribute_id, traffic_type_id)
93-
94-
def add_identities(self, traffic_type_id, environment_id, identities):
95-
'''
96-
Create Identities for a specific traffic type and environment.
97-
98-
:param traffic_type_id: Traffic type id
99-
:param environment_id: Environment where the identities will be created.
100-
:param identities: Identities to be added. Should be in a dict with
101-
the following format:
102-
{
103-
'key1': {
104-
attribute_id_1a: value_1a,
105-
attribute_id_1b: value_1b,
106-
},
107-
'key2': {
108-
attribute_id_2a: value_2a,
109-
attribute_id_2b: value_2b,
110-
}
111-
}
112-
'''
113-
return Identity.create_many(
114-
self._client,
115-
traffic_type_id,
116-
environment_id,
117-
identities
118-
)
119-
120-
def add_identity(self, traffic_type_id, environment_id, key, values):
121-
'''
122-
Create a new Identity.
123-
124-
:param traffic_type_id: Traffic Type Id
125-
:param environment_id: Environment where the identity will be created.
126-
:key: Identity key
127-
:values: Attribute values for the identity. Should be a dict with
128-
the following format:
129-
{
130-
attribute_id1: value1,
131-
attribute_id2: value2,
132-
}
133-
'''
134-
return Identity.create(
135-
self._client,
136-
key,
137-
traffic_type_id,
138-
environment_id,
139-
values
140-
)
141-
142-
def update_identity(self, traffic_type_id, environment_id, key, values):
143-
'''
144-
Update an Identity.
145-
146-
:param traffic_type_id: Traffic Type Id
147-
:param environment_id: Environment where the identity will be updated.
148-
:key: Identity key
149-
:values: Attribute values for the identity. Should be a dict with
150-
the following format:
151-
{
152-
attribute_id1: value1,
153-
attribute_id2: value2,
154-
}
155-
'''
156-
return Identity.update(
157-
self._client,
158-
key,
159-
traffic_type_id,
160-
environment_id,
161-
values
162-
)
163-
164-
def patch_identity(self, traffic_type_id, environment_id, key, values):
165-
'''
166-
Patch an Identity.
167-
168-
:param traffic_type_id: Traffic Type Id
169-
:param environment_id: Environment where the identity will be patched.
170-
:key: Identity key
171-
:values: Attribute values for the identity. Should be a dict with
172-
the following format:
173-
{
174-
attribute_id1: value1,
175-
attribute_id2: value2,
176-
}
177-
'''
178-
return Identity.patch(
179-
self._client,
180-
key,
181-
traffic_type_id,
182-
environment_id,
183-
values
184-
)
185-
186-
def delete_attributes_from_key(self, traffic_type_id, environment_id, key):
187-
'''
188-
Delete all attributes for a specific key.
189-
190-
:param traffic_type_id: Traffic Type Id,
191-
:param environment_id: Enviroment Id,
192-
:key: Key whose attributes will be deleted.
193-
'''
194-
return Identity.delete_all_attributes(
195-
self._client,
196-
traffic_type_id,
197-
environment_id,
198-
key
199-
)
55+
@property
56+
def identity(self):
57+
return self._identity_client

identify/microclients/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from identify.microclients.traffic_type_microclient import TrafficTypeMicroClient
2+
from identify.microclients.environment_microclient import EnvironmentMicroClient
3+
from identify.microclients.attribute_microclient import AttributeMicroClient
4+
from identify.microclients.identity_microclient import IdentityMicroClient

0 commit comments

Comments
 (0)