22from keyword import iskeyword
33
44import requests
5+ from requests .exceptions import RequestException
56
67from cnct .client .constants import CONNECT_ENDPOINT_URL , CONNECT_SPECS_URL
7- from cnct .client .exceptions import APIError , HttpError , NotFoundError
8+ from cnct .client .exceptions import ClientError , NotFoundError
89from cnct .client .models import Collection , NS
910from cnct .client .utils import get_headers
1011from cnct .help import DefaultFormatter
@@ -143,28 +144,28 @@ def collection(self, name):
143144 raise NotFoundError (f'The collection { name } does not exist.' )
144145
145146 def get (self , url , ** kwargs ):
146- return self .execute ('get' , url , 200 , ** kwargs )
147+ return self .execute ('get' , url , ** kwargs )
147148
148149 def create (self , url , payload = None , ** kwargs ):
149150 kwargs = kwargs or {}
150151
151152 if payload :
152153 kwargs ['json' ] = payload
153154
154- return self .execute ('post' , url , 201 , ** kwargs )
155+ return self .execute ('post' , url , ** kwargs )
155156
156157 def update (self , url , payload = None , ** kwargs ):
157158 kwargs = kwargs or {}
158159
159160 if payload :
160161 kwargs ['json' ] = payload
161162
162- return self .execute ('put' , url , 200 , ** kwargs )
163+ return self .execute ('put' , url , ** kwargs )
163164
164165 def delete (self , url , ** kwargs ):
165- return self .execute ('delete' , url , 204 , ** kwargs )
166+ return self .execute ('delete' , url , ** kwargs )
166167
167- def execute (self , method , url , expected_status , ** kwargs ):
168+ def execute (self , method , url , ** kwargs ):
168169 kwargs = kwargs or {}
169170 if 'headers' in kwargs :
170171 kwargs ['headers' ].update (get_headers (self .api_key ))
@@ -174,48 +175,36 @@ def execute(self, method, url, expected_status, **kwargs):
174175 if self .default_headers :
175176 kwargs ['headers' ].update (self .default_headers )
176177
177- self .response = requests .request (
178- method ,
179- url ,
180- ** kwargs ,
181- )
182-
183- if self .response .status_code != expected_status :
184- try :
185- error = self .response .json ()
186- if 'error_code' in error and 'errors' in error :
187- raise APIError (
188- self .response .status_code ,
189- error ['error_code' ],
190- error ['errors' ],
191- )
192- except JSONDecodeError :
193- pass
194-
195- self ._raise_exception ()
178+ self .response = None
196179
197- if self .response .status_code == 204 :
198- return
180+ try :
181+ self ._execute_http_call (method , url , kwargs )
182+ if self .response .status_code == 204 :
183+ return None
184+ if self .response .headers ['Content-Type' ] == 'application/json' :
185+ return self .response .json ()
186+ else :
187+ return self .response .content
199188
200- return self .response .json ()
189+ except RequestException as re :
190+ api_error = self ._get_api_error_details () or {}
191+ status_code = self .response .status_code if self .response is not None else None
192+ raise ClientError (status_code = status_code , ** api_error ) from re
201193
202194 def help (self ):
203195 self ._help_formatter .print_help (self .specs )
204196 return self
205197
206- def _raise_exception (self ):
207- message = ''
208- if isinstance (self .response .reason , bytes ):
209- try :
210- reason = self .response .reason .decode ('utf-8' )
211- except UnicodeDecodeError :
212- reason = self .response .reason .decode ('iso-8859-1' )
213- else :
214- reason = self .response .reason
198+ def _execute_http_call (self , method , url , kwargs ):
199+ self .response = requests .request (method , url , ** kwargs )
200+ if self .response .status_code >= 400 :
201+ self .response .raise_for_status ()
215202
216- if 400 <= self .response .status_code < 500 :
217- message = f'{ self .response .status_code } Client Error: { reason } '
218- elif 500 <= self .response .status_code < 600 :
219- message = f'{ self .response .status_code } Server Error: { reason } '
220-
221- raise HttpError (message , response = self .response )
203+ def _get_api_error_details (self ):
204+ if self .response is not None :
205+ try :
206+ error = self .response .json ()
207+ if 'error_code' in error and 'errors' in error :
208+ return error
209+ except JSONDecodeError :
210+ pass
0 commit comments