1- from requests import Response , request
1+ from requests import Response , request , exceptions
22
33from cyclient import config , __version__
4+ from cli .exceptions .custom_exceptions import NetworkError , HttpUnauthorizedError
45
56
67class CycodeClientBase :
78
89 MANDATORY_HEADERS : dict = {
9- " User-Agent" : f'cycode-cli_{ __version__ } ' ,
10+ ' User-Agent' : f'cycode-cli_{ __version__ } ' ,
1011 }
1112
12- def __init__ (self , api_url ):
13+ def __init__ (self , api_url : str ):
1314 self .timeout = config .timeout
1415 self .api_url = api_url
1516
@@ -20,8 +21,7 @@ def post(
2021 headers : dict = None ,
2122 ** kwargs
2223 ) -> Response :
23- return self ._execute (
24- method = "post" , endpoint = url_path , json = body , headers = headers , ** kwargs )
24+ return self ._execute (method = 'post' , endpoint = url_path , json = body , headers = headers , ** kwargs )
2525
2626 def put (
2727 self ,
@@ -30,16 +30,15 @@ def put(
3030 headers : dict = None ,
3131 ** kwargs
3232 ) -> Response :
33- return self ._execute (
34- method = "put" , endpoint = url_path , json = body , headers = headers , ** kwargs )
33+ return self ._execute (method = 'put' , endpoint = url_path , json = body , headers = headers , ** kwargs )
3534
3635 def get (
3736 self ,
3837 url_path : str ,
3938 headers : dict = None ,
4039 ** kwargs
4140 ) -> Response :
42- return self ._execute (method = " get" , endpoint = url_path , headers = headers , ** kwargs )
41+ return self ._execute (method = ' get' , endpoint = url_path , headers = headers , ** kwargs )
4342
4443 def _execute (
4544 self ,
@@ -48,20 +47,39 @@ def _execute(
4847 headers : dict = None ,
4948 ** kwargs
5049 ) -> Response :
51-
5250 url = self .build_full_url (self .api_url , endpoint )
5351
54- response = request (
55- method = method , url = url , timeout = self .timeout , headers = self .get_request_headers (headers ), ** kwargs
56- )
57- response .raise_for_status ()
58- return response
52+ try :
53+ response = request (
54+ method = method , url = url , timeout = self .timeout , headers = self .get_request_headers (headers ), ** kwargs
55+ )
56+
57+ response .raise_for_status ()
58+ return response
59+ except Exception as e :
60+ self ._handle_exception (e )
5961
60- def get_request_headers (self , additional_headers : dict = None ):
62+ def get_request_headers (self , additional_headers : dict = None ) -> dict :
6163 if additional_headers is None :
62- return self .MANDATORY_HEADERS
64+ return self .MANDATORY_HEADERS . copy ()
6365 return {** self .MANDATORY_HEADERS , ** additional_headers }
6466
65- def build_full_url (self , url , endpoint ):
66- return f"{ url } /{ endpoint } "
67+ def build_full_url (self , url : str , endpoint : str ) -> str :
68+ return f'{ url } /{ endpoint } '
69+
70+ def _handle_exception (self , e : Exception ):
71+ if isinstance (e , exceptions .Timeout ):
72+ raise NetworkError (504 , 'Timeout Error' , e .response )
73+ elif isinstance (e , exceptions .HTTPError ):
74+ self ._handle_http_exception (e )
75+ elif isinstance (e , exceptions .ConnectionError ):
76+ raise NetworkError (502 , 'Connection Error' , e .response )
77+ else :
78+ raise e
79+
80+ @staticmethod
81+ def _handle_http_exception (e : exceptions .HTTPError ):
82+ if e .response .status_code == 401 :
83+ raise HttpUnauthorizedError (e .response .text , e .response )
6784
85+ raise NetworkError (e .response .status_code , e .response .text , e .response )
0 commit comments