66from typing import Any , Dict , List , Optional
77from urllib .parse import urlparse
88
9- import requests as req
10- from requests import HTTPError
9+ import requests
1110
1211from graphdatascience .session .algorithm_category import AlgorithmCategory
1312from graphdatascience .session .aura_api_responses import (
2221from graphdatascience .session .session_sizes import SessionMemoryValue
2322from graphdatascience .version import __version__
2423
24+ class AuraApiError (Exception ):
25+ def __init__ (self , message : str , status_code : int ):
26+ super ().__init__ (self , message )
27+ self .status_code = status_code
2528
2629class AuraApi :
2730 class AuraAuthToken :
@@ -64,36 +67,36 @@ def extract_id(uri: str) -> str:
6467 return host .split ("." )[0 ].split ("-" )[0 ]
6568
6669 def create_session (self , name : str , dbid : str , pwd : str , memory : SessionMemoryValue ) -> SessionDetails :
67- response = req .post (
70+ response = requests .post (
6871 f"{ self ._base_uri } /v1beta5/data-science/sessions" ,
6972 headers = self ._build_header (),
7073 json = {"name" : name , "instance_id" : dbid , "password" : pwd , "memory" : memory .value },
7174 )
7275
73- response . raise_for_status ( )
76+ self . _check_code ( response )
7477
7578 return SessionDetails .fromJson (response .json ())
7679
7780 def list_session (self , session_id : str , dbid : str ) -> Optional [SessionDetails ]:
78- response = req .get (
81+ response = requests .get (
7982 f"{ self ._base_uri } /v1beta5/data-science/sessions/{ session_id } ?instanceId={ dbid } " ,
8083 headers = self ._build_header (),
8184 )
8285
8386 if response .status_code == 404 :
8487 return None
8588
86- response . raise_for_status ( )
89+ self . _check_code ( response )
8790
8891 return SessionDetails .fromJson (response .json ())
8992
9093 def list_sessions (self , dbid : str ) -> List [SessionDetails ]:
91- response = req .get (
94+ response = requests .get (
9295 f"{ self ._base_uri } /v1beta5/data-science/sessions?instanceId={ dbid } " ,
9396 headers = self ._build_header (),
9497 )
9598
96- response . raise_for_status ( )
99+ self . _check_code ( response )
97100
98101 return [SessionDetails .fromJson (s ) for s in response .json ()]
99102
@@ -127,7 +130,7 @@ def wait_for_session_running(
127130 )
128131
129132 def delete_session (self , session_id : str , dbid : str ) -> bool :
130- response = req .delete (
133+ response = requests .delete (
131134 f"{ self ._base_uri } /v1beta5/data-science/sessions/{ session_id } " ,
132135 headers = self ._build_header (),
133136 json = {"instance_id" : dbid },
@@ -138,7 +141,7 @@ def delete_session(self, session_id: str, dbid: str) -> bool:
138141 elif response .status_code == 202 :
139142 return True
140143
141- response . raise_for_status ( )
144+ self . _check_code ( response )
142145
143146 return False
144147
@@ -157,56 +160,52 @@ def create_instance(
157160 "cloud_provider" : cloud_provider ,
158161 }
159162
160- response = req .post (
163+ response = requests .post (
161164 f"{ self ._base_uri } /v1/instances" ,
162165 json = data ,
163166 headers = self ._build_header (),
164167 )
165168
166- try :
167- response .raise_for_status ()
168- except HTTPError as e :
169- print (response .json ())
170- raise e
169+ self ._check_code (response )
171170
172171 return InstanceCreateDetails .from_json (response .json ()["data" ])
173172
174173 def delete_instance (self , instance_id : str ) -> Optional [InstanceSpecificDetails ]:
175- response = req .delete (
174+ response = requests .delete (
176175 f"{ self ._base_uri } /v1/instances/{ instance_id } " ,
177176 headers = self ._build_header (),
178177 )
179178
180179 if response .status_code == 404 :
181180 return None
182181
183- response . raise_for_status ( )
182+ self . _check_code ( response )
184183
185184 return InstanceSpecificDetails .fromJson (response .json ()["data" ])
186185
187186 def list_instances (self ) -> List [InstanceDetails ]:
188- response = req .get (
187+ response = requests .get (
189188 f"{ self ._base_uri } /v1/instances" ,
190189 headers = self ._build_header (),
191190 params = {"tenantId" : self ._tenant_id },
192191 )
193192
194- response . raise_for_status ( )
193+ self . _check_code ( response )
195194
196195 raw_data = response .json ()["data" ]
197196
198197 return [InstanceDetails .fromJson (i ) for i in raw_data ]
199198
200199 def list_instance (self , instance_id : str ) -> Optional [InstanceSpecificDetails ]:
201- response = req .get (
200+ response = requests .get (
202201 f"{ self ._base_uri } /v1/instances/{ instance_id } " ,
203202 headers = self ._build_header (),
204203 )
205204
206205 if response .status_code == 404 :
207206 return None
208207
209- response . raise_for_status ( )
208+ self . _check_code ( response )
210209
211210 raw_data = response .json ()["data" ]
212211
@@ -246,17 +245,17 @@ def estimate_size(
246245 "instance_type" : "dsenterprise" ,
247246 }
248247
249- response = req .post (f"{ self ._base_uri } /v1/instances/sizing" , headers = self ._build_header (), json = data )
250- response . raise_for_status ( )
248+ response = requests .post (f"{ self ._base_uri } /v1/instances/sizing" , headers = self ._build_header (), json = data )
249+ self . _check_code ( response )
251250
252251 return EstimationDetails .from_json (response .json ()["data" ])
253252
254253 def _get_tenant_id (self ) -> str :
255- response = req .get (
254+ response = requests .get (
256255 f"{ self ._base_uri } /v1/tenants" ,
257256 headers = self ._build_header (),
258257 )
259- response . raise_for_status ( )
258+ self . _check_code ( response )
260259
261260 raw_data = response .json ()["data" ]
262261
@@ -270,11 +269,11 @@ def _get_tenant_id(self) -> str:
270269
271270 def tenant_details (self ) -> TenantDetails :
272271 if not self ._tenant_details :
273- response = req .get (
272+ response = requests .get (
274273 f"{ self ._base_uri } /v1/tenants/{ self ._tenant_id } " ,
275274 headers = self ._build_header (),
276275 )
277- response . raise_for_status ( )
276+ self . _check_code ( response )
278277 self ._tenant_details = TenantDetails .from_json (response .json ()["data" ])
279278 return self ._tenant_details
280279
@@ -293,13 +292,20 @@ def _update_token(self) -> AuraAuthToken:
293292
294293 self ._logger .debug ("Updating oauth token" )
295294
296- response = req .post (
295+ response = requests .post (
297296 f"{ self ._base_uri } /oauth/token" , data = data , auth = (self ._credentials [0 ], self ._credentials [1 ])
298297 )
299298
300- response . raise_for_status ( )
299+ self . _check_code ( response )
301300
302301 return AuraApi .AuraAuthToken (response .json ())
303302
303+ def _check_code (self , resp : requests .Response ) -> None :
304+ if resp .status_code >= 400 :
305+ raise AuraApiError (
306+ f"Request for { resp .url } failed with status code { resp .status_code } - { resp .reason } : { resp .text } " ,
307+ status_code = resp .status_code ,
308+ )
309+
304310 def _instance_type (self ) -> str :
305311 return "enterprise-ds" if not self ._dev_env else "professional-ds"
0 commit comments