@@ -25,11 +25,10 @@ class SandboxRestApiClient:
2525
2626 def __init__ (self , host : str , username : str , password : str , domain = 'Global' , port = 82 , is_https = False ,
2727 api_version = "v2" ):
28- """ logs into api and sets headers for future requests """
28+ """ login to api and store headers for future requests """
2929 protocol = "https" if is_https else "http"
3030 self ._base_url = f"{ protocol } ://{ host } :{ port } /api"
3131 self ._versioned_url = f"{ self ._base_url } /{ api_version } "
32- self ._session = requests .Session ()
3332 self ._auth_headers = self ._get_auth_headers (username , password , domain )
3433
3534 def _get_auth_headers (self , user_name : str , password : str , domain : str ):
@@ -65,19 +64,17 @@ def start_sandbox(self, blueprint_id: str, sandbox_name="", duration="PT2H0M",
6564 Create a sandbox from the provided blueprint id
6665 Duration format must be a valid 'ISO 8601'. (e.g 'PT23H' or 'PT4H2M')
6766 """
68- # sandbox name will default to blueprint name if not passed
69- if not sandbox_name :
70- sandbox_name = self .get_blueprint_details (blueprint_id )['name' ]
71-
72- data_dict = {"duration" : duration , "name" : sandbox_name }
73- if permitted_users :
74- data_dict ['permitted_users' ] = permitted_users
75- if bp_params :
76- data_dict ["params" ] = [asdict (x ) for x in bp_params ]
77-
78- response = requests .post (f'{ self ._versioned_url } /blueprints/{ blueprint_id } /start' ,
79- headers = self ._auth_headers ,
80- data = json .dumps (data_dict ))
67+ url = f'{ self ._versioned_url } /blueprints/{ blueprint_id } /start'
68+ sandbox_name = sandbox_name if sandbox_name else self .get_blueprint_details (blueprint_id )['name' ]
69+
70+ data_dict = {
71+ "duration" : duration ,
72+ "name" : sandbox_name ,
73+ "permitted_users" : permitted_users if permitted_users else [],
74+ "params" : [asdict (x ) for x in bp_params ] if bp_params else []
75+ }
76+
77+ response = requests .post (url , headers = self ._auth_headers , data = json .dumps (data_dict ))
8178 if not response .ok :
8279 err_msg = self ._format_err (response , f"Failed to start sandbox from blueprint '{ blueprint_id } '" )
8380 raise SandboxRestException (err_msg )
@@ -86,20 +83,16 @@ def start_sandbox(self, blueprint_id: str, sandbox_name="", duration="PT2H0M",
8683 def start_persistent_sandbox (self , blueprint_id : str , sandbox_name = "" , bp_params : List [InputParam ] = None ,
8784 permitted_users : List [str ] = None ):
8885 """ Create a persistent sandbox from the provided blueprint id """
86+ url = f'{ self ._versioned_url } /blueprints/{ blueprint_id } /start-persistent'
8987
90- # sandbox name will default to blueprint name if not passed
91- if not sandbox_name :
92- sandbox_name = self .get_blueprint_details (blueprint_id )['name' ]
93-
94- data_dict = {"name" : sandbox_name }
95- if permitted_users :
96- data_dict ['permitted_users' ] = permitted_users
97- if bp_params :
98- data_dict ["params" ] = [asdict (x ) for x in bp_params ]
88+ sandbox_name = sandbox_name if sandbox_name else self .get_blueprint_details (blueprint_id )['name' ]
89+ data_dict = {
90+ "name" : sandbox_name ,
91+ "permitted_users" : permitted_users if permitted_users else [],
92+ "params" : [asdict (x ) for x in bp_params ] if bp_params else []
93+ }
9994
100- response = requests .post (f'{ self ._versioned_url } /blueprints/{ blueprint_id } /start-persistent' ,
101- headers = self ._auth_headers ,
102- data = json .dumps (data_dict ))
95+ response = requests .post (url , headers = self ._auth_headers , data = json .dumps (data_dict ))
10396 if not response .ok :
10497 err_msg = self ._format_err (response , f"Failed to start persistent sandbox from blueprint '{ blueprint_id } '" )
10598 raise SandboxRestException (err_msg )
@@ -126,7 +119,8 @@ def run_component_command(self, sandbox_id: str, component_id: str, command_name
126119 data_dict ["params" ] = params
127120 response = requests .post (url , data = json .dumps (data_dict ), headers = self ._auth_headers )
128121 if not response .ok :
129- raise SandboxRestException (self ._format_err (response , f"failed to start component command '{ command_name } '" ))
122+ raise SandboxRestException (
123+ self ._format_err (response , f"failed to start component command '{ command_name } '" ))
130124 return response .json ()
131125
132126 def extend_sandbox (self , sandbox_id : str , duration : str ):
0 commit comments