Skip to content

Commit 484b10a

Browse files
committed
BIG REFACTOR
1 parent e2523da commit 484b10a

23 files changed

Lines changed: 1530 additions & 1104 deletions

identify/http_clients/sync_client.py

Lines changed: 11 additions & 2 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):
@@ -51,6 +52,14 @@ def _handle_invalid_response(self, response):
5152
else:
5253
raise HTTPResponseError
5354

55+
def _handle_connection_error(self, e):
56+
'''
57+
'''
58+
LOGGER.debug(e)
59+
raise IdentifyBackendUnreachableError(
60+
'Unable to reach Identify backend'
61+
)
62+
5463
def make_request(self, endpoint, body=None, **kwargs):
5564
'''
5665
This method delegates bulding of headers, url and querystring (!)
@@ -81,7 +90,7 @@ def make_request(self, endpoint, body=None, **kwargs):
8190
try:
8291
response = method(url, headers=headers)
8392
except Exception as e:
84-
return self._connection_error(e)
93+
return self._handle_connection_error(e)
8594
finally:
8695
LOGGER.debug('RESPONSE: ' + response.text)
8796

identify/main/identify_sync_client.py

Lines changed: 4 additions & 220 deletions
Original file line numberDiff line numberDiff line change
@@ -2,227 +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
10-
11-
12-
class TrafficTypeMicroClient:
13-
'''
14-
'''
15-
16-
def __init__(self, http_client):
17-
'''
18-
'''
19-
self._http_client = http_client
20-
21-
def get_all(self):
22-
'''
23-
Returns a list of TrafficType objects.
24-
'''
25-
return TrafficType.retrieve_all(self._http_client)
26-
27-
28-
class EnvironmentMicroClient:
29-
'''
30-
'''
31-
32-
def __init__(self, http_client):
33-
'''
34-
'''
35-
self._http_client = http_client
36-
37-
def get_all(self):
38-
'''
39-
Returns a list of environments.
40-
'''
41-
return Environment.retrieve_all(self._http_client)
42-
43-
44-
class AttributeMicroClient:
45-
'''
46-
'''
47-
48-
def __init__(self, http_client):
49-
'''
50-
'''
51-
self._http_client = http_client
52-
53-
def get_all(self, traffic_type_id):
54-
'''
55-
Returns a list of attributes for a particular traffic type.
56-
57-
:param traffic_type_id: Id of the traffic type whose attributes are to
58-
be retrieved.
59-
'''
60-
return Attribute.retrieve_all(
61-
self._http_client,
62-
trafficTypeId=traffic_type_id
63-
)
64-
65-
def create(self, traffic_type_id, attr_id, display_name, description,
66-
data_type, is_searchable):
67-
'''
68-
Creates an attribute for a specific traffic type.
69-
70-
:param traffic_type_id: Id of the traffic type for which an attribute
71-
will be created.
72-
:param attr_id: string. New attribute's Id
73-
:param display_name: string. New attribute's display name
74-
:param description: string. New attribute's description
75-
:param data_type: string. New attribute's data type
76-
:param is_searchable: string.
77-
'''
78-
return Attribute.create(
79-
self._http_client,
80-
attr_id,
81-
traffic_type_id,
82-
display_name,
83-
description,
84-
data_type,
85-
is_searchable
86-
)
87-
88-
def delete(self, traffic_type_id, attribute_id):
89-
'''
90-
Delete an attribute from a particular traffic type.
91-
92-
:param traffic_type_id: Trafic id of the schema whose attribute
93-
will be removed.
94-
:param attribute_id: Id of the attribute to be removed.
95-
'''
96-
return Attribute.delete(
97-
self._http_client,
98-
attribute_id,
99-
traffic_type_id
100-
)
101-
102-
103-
class IdentityMicroClient:
104-
'''
105-
'''
106-
107-
def __init__(self, http_client):
108-
'''
109-
'''
110-
self._http_client = http_client
111-
112-
def add_many(self, traffic_type_id, environment_id, identities,
113-
organization_id=None):
114-
'''
115-
Create Identities for a specific traffic type and environment.
116-
117-
:param traffic_type_id: Traffic type id
118-
:param environment_id: Environment where the identities will be created.
119-
:param identities: Identities to be added. Should be in a dict with
120-
the following format:
121-
{
122-
'key1': {
123-
attribute_id_1a: value_1a,
124-
attribute_id_1b: value_1b,
125-
},
126-
'key2': {
127-
attribute_id_2a: value_2a,
128-
attribute_id_2b: value_2b,
129-
}
130-
}
131-
'''
132-
return Identity.create_many(
133-
self._http_client,
134-
traffic_type_id,
135-
environment_id,
136-
identities,
137-
organization_id
138-
)
139-
140-
def add(self, traffic_type_id, environment_id, key, values,
141-
organization_id=None):
142-
'''
143-
Create a new Identity.
144-
145-
:param traffic_type_id: Traffic Type Id
146-
:param environment_id: Environment where the identity will be created.
147-
:key: Identity key
148-
:values: Attribute values for the identity. Should be a dict with
149-
the following format:
150-
{
151-
attribute_id1: value1,
152-
attribute_id2: value2,
153-
}
154-
'''
155-
return Identity.create(
156-
self._http_client,
157-
key,
158-
traffic_type_id,
159-
environment_id,
160-
values,
161-
organization_id
162-
)
163-
164-
def update(self, traffic_type_id, environment_id, key, values,
165-
organization_id=None):
166-
'''
167-
Update an Identity.
168-
169-
:param traffic_type_id: Traffic Type Id
170-
:param environment_id: Environment where the identity will be updated.
171-
:key: Identity key
172-
:values: Attribute values for the identity. Should be a dict with
173-
the following format:
174-
{
175-
attribute_id1: value1,
176-
attribute_id2: value2,
177-
}
178-
'''
179-
return Identity.update(
180-
self._http_client,
181-
key,
182-
traffic_type_id,
183-
environment_id,
184-
values,
185-
organization_id
186-
)
187-
188-
def patch(self, traffic_type_id, environment_id, key, values,
189-
organization_id=None):
190-
'''
191-
Patch an Identity.
192-
193-
:param traffic_type_id: Traffic Type Id
194-
:param environment_id: Environment where the identity will be patched.
195-
:key: Identity key
196-
:values: Attribute values for the identity. Should be a dict with
197-
the following format:
198-
{
199-
attribute_id1: value1,
200-
attribute_id2: value2,
201-
}
202-
'''
203-
return Identity.patch(
204-
self._http_client,
205-
key,
206-
traffic_type_id,
207-
environment_id,
208-
values,
209-
organization_id
210-
)
211-
212-
def delete_attributes(self, traffic_type_id, environment_id, key):
213-
'''
214-
Delete all attributes for a specific key.
215-
216-
:param traffic_type_id: Traffic Type Id,
217-
:param environment_id: Enviroment Id,
218-
:key: Key whose attributes will be deleted.
219-
'''
220-
return Identity.delete_all_attributes(
221-
self._http_client,
222-
traffic_type_id,
223-
environment_id,
224-
key
225-
)
6+
from identify.microclients import TrafficTypeMicroClient
7+
from identify.microclients import EnvironmentMicroClient
8+
from identify.microclients import IdentityMicroClient
9+
from identify.microclients import AttributeMicroClient
22610

22711

22812
class SyncIdentifyClient(BaseIdentifyClient):

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
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
from identify.resources.attribute import Attribute
2+
from identify.util.exceptions import HTTPResponseError, \
3+
UnknownIdentifyClientError
4+
from identify.util.logger import LOGGER
5+
6+
7+
class AttributeMicroClient:
8+
'''
9+
'''
10+
_endpoint = {
11+
'all_items': {
12+
'method': 'GET',
13+
'url_template': 'trafficTypes/{trafficTypeId}/schema',
14+
'headers': [{
15+
'name': 'Authorization',
16+
'template': 'Bearer {value}',
17+
'required': True,
18+
}],
19+
'query_string': [],
20+
'response': True,
21+
},
22+
'create': {
23+
'method': 'PUT',
24+
'url_template': 'trafficTypes/{trafficTypeId}/schema',
25+
'headers': [{
26+
'name': 'Authorization',
27+
'template': 'Bearer {value}',
28+
'required': True,
29+
}],
30+
'query_string': [],
31+
'response': True,
32+
},
33+
'delete': {
34+
'method': 'DELETE',
35+
'url_template': 'trafficTypes/{trafficTypeId}/schema/{attributeId}',
36+
'headers': [{
37+
'name': 'Authorization',
38+
'template': 'Bearer {value}',
39+
'required': True,
40+
}],
41+
'query_string': [],
42+
'response': False,
43+
},
44+
}
45+
46+
def __init__(self, http_client):
47+
'''
48+
'''
49+
self._http_client = http_client
50+
51+
def list(self, traffic_type_id):
52+
'''
53+
Returns a list of TrafficType objects.
54+
'''
55+
try:
56+
response = self._http_client.make_request(
57+
self._endpoint['all_items'],
58+
trafficTypeId=traffic_type_id
59+
)
60+
return [Attribute(item) for item in response]
61+
except HTTPResponseError as e:
62+
LOGGER.error('Error retrieving items')
63+
raise e
64+
except Exception as e:
65+
LOGGER.debug(e)
66+
raise UnknownIdentifyClientError()
67+
68+
def create(self, attribute):
69+
'''
70+
'''
71+
# TODO: Validate!
72+
try:
73+
data = attribute.to_dict() if isinstance(attribute, Attribute) else attribute
74+
response = self._http_client.make_request(
75+
self._endpoint['create'],
76+
data,
77+
trafficTypeId=data['trafficTypeId']
78+
)
79+
80+
return Attribute(response, self._http_client)
81+
82+
except HTTPResponseError as e:
83+
LOGGER.error('Call to Identify API failed. Attribute not created.')
84+
raise e
85+
except Exception as e:
86+
LOGGER.debug(e)
87+
raise UnknownIdentifyClientError()
88+
89+
def delete(self, attribute):
90+
'''
91+
'''
92+
return self.delete_by_id(attribute.id, attribute.traffic_type_id)
93+
94+
def delete_by_id(self, attribute_id, traffic_type_id):
95+
'''
96+
'''
97+
try:
98+
return self._http_client.make_request(
99+
self._endpoint['delete'],
100+
trafficTypeId=traffic_type_id,
101+
attributeId=attribute_id
102+
)
103+
except HTTPResponseError as e:
104+
LOGGER.error('Call to Identify API failed. Attribute not deleted.')
105+
raise e
106+
except Exception as e:
107+
LOGGER.debug(e)
108+
raise UnknownIdentifyClientError()

0 commit comments

Comments
 (0)