Skip to content

Commit d403bd3

Browse files
committed
updated to support large segments
1 parent 6f189e1 commit d403bd3

18 files changed

+1146
-7
lines changed

CHANGES.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@
3737
- Added ability to retrieve API keys when creating an environment
3838
3.1.13 (Jan 31, 2024)
3939
- Updated keyId of an API KEY to be the actual ID and not the key itself
40-
3.2.0 (Jan 31, 2025)
41-
- Updated to support flag sets and the impressionsDisabled boolean value
40+
3.2.0 (Feb 2, 2025)
41+
- Updated to support flag sets, large segments and the impressionsDisabled boolean value

splitapiclient/main/sync_apiclient.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
from splitapiclient.microclients import APIKeyMicroClient
1919
from splitapiclient.microclients import RestrictionMicroClient
2020
from splitapiclient.microclients import FlagSetMicroClient
21+
from splitapiclient.microclients import LargeSegmentMicroClient
22+
from splitapiclient.microclients import LargeSegmentDefinitionMicroClient
23+
2124

2225
class SyncApiClient(BaseApiClient):
2326
'''
@@ -41,7 +44,11 @@ def __init__(self, config):
4144
else:
4245
self._base_url = self.BASE_PROD_URL
4346
self._base_url_old = self.BASE_PROD_URL_OLD
44-
self._base_url_v3 = self.BASE_PROD_URL_V3
47+
48+
if 'base_url_v3' in config:
49+
self._base_url_v3 = config['base_url_v3']
50+
else:
51+
self._base_url_v3 = self.BASE_PROD_URL_V3
4552

4653
missing = [i for i in ['apikey'] if i not in config]
4754
if missing:
@@ -60,6 +67,8 @@ def __init__(self, config):
6067
self._split_definition_client = SplitDefinitionMicroClient(http_client)
6168
self._segment_client = SegmentMicroClient(http_client)
6269
self._segment_definition_client = SegmentDefinitionMicroClient(http_client)
70+
self._large_segment_client = LargeSegmentMicroClient(http_client)
71+
self._large_segment_definition_client = LargeSegmentDefinitionMicroClient(http_client)
6372
self._workspace_client = WorkspaceMicroClient(http_client)
6473
self._traffic_type_client = TrafficTypeMicroClient(http_client)
6574
self._attribute_client = AttributeMicroClient(http_client)
@@ -94,6 +103,14 @@ def segments(self):
94103
@property
95104
def segment_definitions(self):
96105
return self._segment_definition_client
106+
107+
@property
108+
def large_segments(self):
109+
return self._large_segment_client
110+
111+
@property
112+
def large_segment_definitions(self):
113+
return self._large_segment_definition_client
97114

98115
@property
99116
def workspaces(self):

splitapiclient/microclients/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@
1212
from splitapiclient.microclients.group_microclient import GroupMicroClient
1313
from splitapiclient.microclients.apikey_microclient import APIKeyMicroClient
1414
from splitapiclient.microclients.restriction_microclient import RestrictionMicroClient
15-
from splitapiclient.microclients.flag_set_microclient import FlagSetMicroClient
15+
from splitapiclient.microclients.flag_set_microclient import FlagSetMicroClient
16+
from splitapiclient.microclients.large_segment_microclient import LargeSegmentMicroClient
17+
from splitapiclient.microclients.large_segment_definition_microclient import LargeSegmentDefinitionMicroClient
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
from splitapiclient.resources import LargeSegmentDefinition
2+
from splitapiclient.util.exceptions import HTTPResponseError, \
3+
UnknownApiClientError
4+
from splitapiclient.util.logger import LOGGER
5+
from splitapiclient.util.helpers import as_dict
6+
import urllib
7+
import requests
8+
9+
class LargeSegmentDefinitionMicroClient:
10+
'''
11+
'''
12+
_endpoint = {
13+
'all_items': {
14+
'method': 'GET',
15+
'url_template': 'large-segments/ws/{workspaceId}/environments/{environmentId}',
16+
'headers': [{
17+
'name': 'Authorization',
18+
'template': 'Bearer {value}',
19+
'required': True,
20+
}],
21+
'query_string': [],
22+
'response': True,
23+
},
24+
'make_cr': {
25+
'method': 'POST',
26+
'url_template': 'changeRequests/ws/{workspaceId}/environments/{environmentId}',
27+
'headers': [{
28+
'name': 'Authorization',
29+
'template': 'Bearer {value}',
30+
'required': True,
31+
}],
32+
'query_string': [],
33+
'response': True,
34+
},
35+
36+
}
37+
38+
def __init__(self, http_client):
39+
'''
40+
Constructor
41+
'''
42+
self._http_client = http_client
43+
44+
def list(self, environment_id, workspace_id):
45+
'''
46+
Returns a list of Segment in environemnt objects.
47+
48+
:returns: list of Segment in environemnt objects
49+
:rtype: list(SegmentDefinition)
50+
'''
51+
final_list = []
52+
response = self._http_client.make_request(
53+
self._endpoint['all_items'],
54+
workspaceId = workspace_id,
55+
environmentId = environment_id,
56+
)
57+
for item in response:
58+
final_list.append(as_dict(item))
59+
60+
61+
segment_definition_list = []
62+
for item in final_list:
63+
item['environment'] = {'id':environment_id, 'name':''}
64+
item['workspaceId'] = workspace_id
65+
segment_definition_list.append(LargeSegmentDefinition(item, self._http_client))
66+
return segment_definition_list
67+
68+
def find(self, segment_name, environment_id, workspace_id):
69+
'''
70+
Find Segment in environment list objects.
71+
72+
:returns: SegmentDefinition object
73+
:rtype: SegmentDefinition
74+
'''
75+
for item in self.list(environment_id, workspace_id):
76+
if item.name == segment_name:
77+
return item
78+
LOGGER.error("Large Segment Definition Name does not exist")
79+
return None
80+
81+
82+
83+
def remove_all_members(self, workspace_id, environment_id, name, title, comment, approvers):
84+
'''
85+
remove all segment members
86+
87+
:param data: json data
88+
:param workspace_id: workspace id
89+
:param environment_id: environment id
90+
91+
92+
:returns: True
93+
:rtype: boolean
94+
'''
95+
96+
data = {
97+
'largeSegment': {
98+
'name': name
99+
},
100+
'operationType': 'ARCHIVE',
101+
'title': title,
102+
'comment': comment,
103+
'approvers': approvers
104+
}
105+
response = self._http_client.make_request(
106+
self._endpoint['make_cr'],
107+
body=as_dict(data),
108+
environmentId = environment_id,
109+
workspaceId = workspace_id
110+
)
111+
return True
112+
113+
114+
def submit_upload(self, workspace_id, environment_id, name, title,comment,approvers):
115+
'''
116+
remove keys from csv file into segment
117+
118+
:param segment: segment name, environment id, json data
119+
120+
:returns: True
121+
:rtype: boolean
122+
'''
123+
data ={
124+
'largeSegment': {
125+
'name': name
126+
},
127+
'operationType': 'UPLOAD',
128+
'title': title,
129+
'comment': comment,
130+
'approvers': approvers,
131+
}
132+
response = self._http_client.make_request(
133+
self._endpoint['make_cr'],
134+
body=as_dict(data),
135+
environmentId = environment_id,
136+
workspaceId = workspace_id
137+
)
138+
return response
139+
140+
141+
def upload_file(self, result, file_path):
142+
'''
143+
Upload a file to the specified URL.
144+
145+
:param result: dictionary containing 'url', 'method', and 'transactionMetadata'
146+
:param file_path: path to the file to be uploaded
147+
148+
:returns: response from the server
149+
:rtype: requests.Response
150+
'''
151+
url = result['transactionMetadata']['url']
152+
headers = {
153+
'Host': result['transactionMetadata']['headers']['Host'][0]
154+
}
155+
with open(file_path, 'rb') as file:
156+
response = requests.put(url, headers=headers, data=file)
157+
return response

0 commit comments

Comments
 (0)