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