Skip to content

Commit 9340517

Browse files
author
Bilal Al-Shahwany
committed
Added Support for Workspace feature
1 parent ca31a82 commit 9340517

12 files changed

Lines changed: 156 additions & 17 deletions

File tree

splitapiclient/main/apiclient.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ def traffic_types(self):
2020
def environments(sself):
2121
pass
2222

23+
@abc.abstractproperty
24+
def workspaces(sself):
25+
pass
26+
27+
2328
@abc.abstractproperty
2429
def attributes(self):
2530
pass

splitapiclient/main/sync_apiclient.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from splitapiclient.util.exceptions import InsufficientConfigArgumentsException
66
from splitapiclient.microclients import TrafficTypeMicroClient
77
from splitapiclient.microclients import EnvironmentMicroClient
8+
from splitapiclient.microclients import WorkspaceMicroClient
89
from splitapiclient.microclients import IdentityMicroClient
910
from splitapiclient.microclients import AttributeMicroClient
1011

@@ -14,7 +15,8 @@ class SyncApiClient(BaseApiClient):
1415
Synchronous Split API client
1516
'''
1617

17-
BASE_PROD_URL = 'https://api.split.io/internal/api/v1'
18+
BASE_PROD_URL = 'https://api.split.io/internal/api/v2'
19+
BASE_PROD_URL_OLD = 'https://api.split.io/internal/api/v1'
1820

1921
def __init__(self, config):
2022
'''
@@ -29,6 +31,7 @@ def __init__(self, config):
2931
self._base_url = config['base_url']
3032
else:
3133
self._base_url = self.BASE_PROD_URL
34+
self._base_url_old = self.BASE_PROD_URL_OLD
3235

3336
missing = [i for i in ['apikey'] if i not in config]
3437
if missing:
@@ -38,10 +41,13 @@ def __init__(self, config):
3841
)
3942

4043
self._apikey = config['apikey']
41-
http_client = SyncHttpClient(self._base_url, self._apikey)
42-
44+
45+
http_client = SyncHttpClient(self._base_url_old, self._apikey)
4346
self._traffic_type_client = TrafficTypeMicroClient(http_client)
47+
48+
http_client = SyncHttpClient(self._base_url, self._apikey)
4449
self._environment_client = EnvironmentMicroClient(http_client)
50+
self._workspace_client = WorkspaceMicroClient(http_client)
4551
self._attribute_client = AttributeMicroClient(http_client)
4652
self._identity_client = IdentityMicroClient(http_client)
4753

@@ -53,6 +59,10 @@ def traffic_types(self):
5359
def environments(self):
5460
return self._environment_client
5561

62+
@property
63+
def workspaces(self):
64+
return self._workspace_client
65+
5666
@property
5767
def attributes(self):
5868
return self._attribute_client
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from splitapiclient.microclients.traffic_type_microclient import TrafficTypeMicroClient
22
from splitapiclient.microclients.environment_microclient import EnvironmentMicroClient
3+
from splitapiclient.microclients.workspace_microclient import WorkspaceMicroClient
34
from splitapiclient.microclients.attribute_microclient import AttributeMicroClient
45
from splitapiclient.microclients.identity_microclient import IdentityMicroClient

splitapiclient/microclients/attribute_microclient.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class AttributeMicroClient:
1010
_endpoint = {
1111
'all_items': {
1212
'method': 'GET',
13-
'url_template': 'trafficTypes/{trafficTypeId}/schema',
13+
'url_template': 'schema/ws/{workspaceId}/trafficTypes/{trafficTypeId}',
1414
'headers': [{
1515
'name': 'Authorization',
1616
'template': 'Bearer {value}',
@@ -20,8 +20,8 @@ class AttributeMicroClient:
2020
'response': True,
2121
},
2222
'create': {
23-
'method': 'PUT',
24-
'url_template': 'trafficTypes/{trafficTypeId}/schema',
23+
'method': 'POST',
24+
'url_template': 'schema/ws/{workspaceId}/trafficTypes/{trafficTypeId}',
2525
'headers': [{
2626
'name': 'Authorization',
2727
'template': 'Bearer {value}',
@@ -32,7 +32,7 @@ class AttributeMicroClient:
3232
},
3333
'delete': {
3434
'method': 'DELETE',
35-
'url_template': 'trafficTypes/{trafficTypeId}/schema/{attributeId}',
35+
'url_template': 'schema/ws/{workspaceId}/trafficTypes/{trafficTypeId}/schema/{attributeId}',
3636
'headers': [{
3737
'name': 'Authorization',
3838
'template': 'Bearer {value}',
@@ -48,7 +48,7 @@ def __init__(self, http_client):
4848
'''
4949
self._http_client = http_client
5050

51-
def list(self, traffic_type_id):
51+
def list(self, workspace_id, traffic_type_id):
5252
'''
5353
Returns a list of TrafficType objects.
5454
@@ -58,7 +58,8 @@ def list(self, traffic_type_id):
5858
'''
5959
response = self._http_client.make_request(
6060
self._endpoint['all_items'],
61-
trafficTypeId=traffic_type_id
61+
trafficTypeId=traffic_type_id,
62+
workspaceId=workspace_id
6263
)
6364
return [Attribute(item, self._http_client) for item in response]
6465

@@ -73,14 +74,18 @@ def save(self, attribute):
7374
:rtype: Attribute
7475
'''
7576
data = as_dict(attribute)
77+
wsId=data.get('workspaceId')
78+
del data['workspaceId']
79+
del data['isSearchable']
7680
response = self._http_client.make_request(
7781
self._endpoint['create'],
7882
data,
79-
trafficTypeId=data.get('trafficTypeId')
83+
trafficTypeId=data.get('trafficTypeId'),
84+
workspaceId=wsId
8085
)
8186
return Attribute(response, self._http_client)
8287

83-
def delete(self, attribute_id, traffic_type_id):
88+
def delete(self, attribute_id, workspace_id, traffic_type_id):
8489
'''
8590
Delete an attribute by specifying its id and it's traffic type id.
8691
@@ -90,7 +95,8 @@ def delete(self, attribute_id, traffic_type_id):
9095
return self._http_client.make_request(
9196
self._endpoint['delete'],
9297
trafficTypeId=traffic_type_id,
93-
attributeId=attribute_id
98+
attributeId=attribute_id,
99+
workspaceId=workspace_id
94100
)
95101

96102
def delete_by_instance(self, attribute):
@@ -102,5 +108,6 @@ def delete_by_instance(self, attribute):
102108
data = as_dict(attribute)
103109
return self.delete(
104110
data.get('id'),
105-
data.get('trafficTypeId')
111+
data.get('trafficTypeId'),
112+
data.get('workspaceId')
106113
)

splitapiclient/microclients/environment_microclient.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class EnvironmentMicroClient:
99
_endpoint = {
1010
'all_items': {
1111
'method': 'GET',
12-
'url_template': 'environments',
12+
'url_template': 'environments/ws/{workspaceId}',
1313
'headers': [{
1414
'name': 'Authorization',
1515
'template': 'Bearer {value}',
@@ -26,14 +26,15 @@ def __init__(self, http_client):
2626
'''
2727
self._http_client = http_client
2828

29-
def list(self):
29+
def list(self, workspace_id):
3030
'''
3131
Returns a list of Environment objects.
3232
3333
:returns: list of Environment objects
3434
:rtype: list(Environment)
3535
'''
3636
response = self._http_client.make_request(
37-
self._endpoint['all_items']
37+
self._endpoint['all_items'],
38+
workspaceId =workspace_id
3839
)
3940
return [Environment(item, self._http_client) for item in response]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from splitapiclient.resources import Workspace
2+
from splitapiclient.util.exceptions import HTTPResponseError, \
3+
UnknownApiClientError
4+
from splitapiclient.util.logger import LOGGER
5+
6+
class WorkspaceMicroClient:
7+
'''
8+
'''
9+
_endpoint = {
10+
'all_items': {
11+
'method': 'GET',
12+
'url_template': 'workspaces',
13+
'headers': [{
14+
'name': 'Authorization',
15+
'template': 'Bearer {value}',
16+
'required': True,
17+
}],
18+
'query_string': [],
19+
'response': True,
20+
},
21+
}
22+
23+
def __init__(self, http_client):
24+
'''
25+
Constructor
26+
'''
27+
self._http_client = http_client
28+
29+
def list(self):
30+
'''
31+
Returns a list of Workspaces objects.
32+
33+
:returns: list of Workspaces objects
34+
:rtype: list(Workspaces)
35+
'''
36+
response = self._http_client.make_request(
37+
self._endpoint['all_items']
38+
)
39+
return [Workspace(item, self._http_client) for item in response['objects']]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from splitapiclient.resources.traffic_type import TrafficType
22
from splitapiclient.resources.environment import Environment
3+
from splitapiclient.resources.workspace import Workspace
34
from splitapiclient.resources.identity import Identity
45
from splitapiclient.resources.attribute import Attribute

splitapiclient/resources/attribute.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Attribute(BaseResource):
1414
'description': 'string',
1515
'dataType': 'string',
1616
'isSearchable': 'bool',
17+
'workspaceId' : 'string',
1718
}
1819

1920
def __init__(self, data=None, client=None):
@@ -27,6 +28,7 @@ def __init__(self, data=None, client=None):
2728
self._description = data.get('description')
2829
self._data_type = data.get('dataType')
2930
self._is_searchable = data.get('isSearchable')
31+
self._workspace_id = data.get('workspaceId')
3032

3133
@property
3234
def traffic_type_id(self):

splitapiclient/resources/environment.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Environment(BaseResource):
1010
_schema = {
1111
'id': 'string',
1212
'name': 'string',
13+
'workspaceId' : 'string',
1314
}
1415

1516
def __init__(self, data=None, client=None):
@@ -19,7 +20,12 @@ def __init__(self, data=None, client=None):
1920
data = {}
2021
BaseResource.__init__(self, data.get('id'), client)
2122
self._name = data.get('name')
23+
self._workspace_id = data.get('workspaceId')
2224

25+
@property
26+
def workspace_id(self):
27+
return self._workspace_id
28+
2329
@property
2430
def name(self):
2531
return self._name
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from __future__ import absolute_import, division, print_function, \
2+
unicode_literals
3+
from splitapiclient.resources.base_resource import BaseResource
4+
from splitapiclient.util.helpers import require_client, as_dict
5+
6+
7+
class Workspace(BaseResource):
8+
'''
9+
'''
10+
_schema = {
11+
'id': 'string',
12+
'name': 'string',
13+
}
14+
15+
def __init__(self, data=None, client=None):
16+
'''
17+
'''
18+
if not data:
19+
data = {}
20+
BaseResource.__init__(self, data.get('id'), client)
21+
self._name = data.get('name')
22+
23+
@property
24+
def name(self):
25+
return self._name
26+
27+
@name.setter
28+
def name(self, new):
29+
self._name = new
30+
31+
def add_identity(self, data, apiclient=None):
32+
'''
33+
Add a new identity associated with this environment.
34+
35+
:param data: Identity object or dict containing identity properties
36+
:param apiclient: If this instance wasn't returned by the client,
37+
the IdentifyClient instance should be passed in order to perform the
38+
http call
39+
'''
40+
imc = require_client('Identity', self._client, apiclient)
41+
identity = as_dict(data)
42+
identity['environmentId'] = self.id
43+
return imc.save(identity)
44+
45+
def add_identities(self, data, apiclient=None):
46+
'''
47+
Add multiple new identities associated with this environment.
48+
49+
:param data: list ofIdentity objects or dicts containing identity
50+
properties
51+
:param apiclient: If this instance wasn't returned by the client,
52+
the IdentifyClient instance should be passed in order to perform the
53+
http call
54+
55+
:returns: tuple with successful and failed items. Succesful items
56+
are Identity objects. Failed ones will contain the Identity object
57+
for the failed item togegther with a status code and a message
58+
:rtype: tuple
59+
'''
60+
imc = require_client('Identity', self._client, apiclient)
61+
identities = [as_dict(i) for i in data]
62+
for item in identities:
63+
item['environmentId'] = self.id
64+
return imc.save_all(identities)

0 commit comments

Comments
 (0)