@@ -149,11 +149,10 @@ def _response_for(self, path, model_class, ip_address):
149149 'User-Agent' : self ._user_agent ()
150150 },
151151 timeout = self ._timeout )
152- if response .status_code == 200 :
153- body = self ._handle_success (response , uri )
154- return model_class (body , locales = self ._locales )
155- else :
156- self ._handle_error (response , uri )
152+ if response .status_code != 200 :
153+ raise self ._exception_for_error (response , uri )
154+ body = self ._handle_success (response , uri )
155+ return model_class (body , locales = self ._locales )
157156
158157 def _user_agent (self ):
159158 return 'GeoIP2 Python Client v%s (%s)' % (geoip2 .__version__ ,
@@ -168,57 +167,54 @@ def _handle_success(self, response, uri):
168167 'JSON: ' % locals () + ', ' .join (ex .args ), 200 ,
169168 uri )
170169
171- def _handle_error (self , response , uri ):
170+ def _exception_for_error (self , response , uri ):
172171 status = response .status_code
173172
174173 if 400 <= status < 500 :
175- self ._handle_4xx_status (response , status , uri )
174+ return self ._exception_for_4xx_status (response , status , uri )
176175 elif 500 <= status < 600 :
177- self ._handle_5xx_status (status , uri )
178- else :
179- self ._handle_non_200_status (status , uri )
176+ return self ._exception_for_5xx_status (status , uri )
177+ return self ._exception_for_non_200_status (status , uri )
180178
181- def _handle_4xx_status (self , response , status , uri ):
179+ def _exception_for_4xx_status (self , response , status , uri ):
182180 if not response .content :
183- raise HTTPError ('Received a %(status)i error for %(uri)s '
184- 'with no body.' % locals (), status , uri )
181+ return HTTPError ('Received a %(status)i error for %(uri)s '
182+ 'with no body.' % locals (), status , uri )
185183 elif response .headers ['Content-Type' ].find ('json' ) == - 1 :
186- raise HTTPError ('Received a %i for %s with the following '
187- 'body: %s' % (status , uri , response .content ),
188- status , uri )
184+ return HTTPError ('Received a %i for %s with the following '
185+ 'body: %s' % (status , uri , response .content ),
186+ status , uri )
189187 try :
190188 body = response .json ()
191189 except ValueError as ex :
192- raise HTTPError (
190+ return HTTPError (
193191 'Received a %(status)i error for %(uri)s but it did'
194192 ' not include the expected JSON body: ' % locals () +
195193 ', ' .join (ex .args ), status , uri )
196194 else :
197195 if 'code' in body and 'error' in body :
198- self ._handle_web_service_error (
196+ return self ._exception_for_web_service_error (
199197 body .get ('error' ), body .get ('code' ), status , uri )
200- else :
201- raise HTTPError (
202- 'Response contains JSON but it does not specify '
203- 'code or error keys' , status , uri )
198+ return HTTPError ('Response contains JSON but it does not specify '
199+ 'code or error keys' , status , uri )
204200
205- def _handle_web_service_error (self , message , code , status , uri ):
201+ def _exception_for_web_service_error (self , message , code , status , uri ):
206202 if code in ('IP_ADDRESS_NOT_FOUND' , 'IP_ADDRESS_RESERVED' ):
207- raise AddressNotFoundError (message )
203+ return AddressNotFoundError (message )
208204 elif code in ('AUTHORIZATION_INVALID' , 'LICENSE_KEY_REQUIRED' ,
209205 'USER_ID_REQUIRED' , 'USER_ID_UNKNOWN' ):
210- raise AuthenticationError (message )
206+ return AuthenticationError (message )
211207 elif code in ('INSUFFICIENT_FUNDS' , 'OUT_OF_QUERIES' ):
212- raise OutOfQueriesError (message )
208+ return OutOfQueriesError (message )
213209 elif code == 'PERMISSION_REQUIRED' :
214- raise PermissionRequiredError (message )
210+ return PermissionRequiredError (message )
215211
216- raise InvalidRequestError (message , code , status , uri )
212+ return InvalidRequestError (message , code , status , uri )
217213
218- def _handle_5xx_status (self , status , uri ):
219- raise HTTPError ('Received a server error (%(status)i) for '
220- '%(uri)s' % locals (), status , uri )
214+ def _exception_for_5xx_status (self , status , uri ):
215+ return HTTPError ('Received a server error (%(status)i) for '
216+ '%(uri)s' % locals (), status , uri )
221217
222- def _handle_non_200_status (self , status , uri ):
223- raise HTTPError ('Received a very surprising HTTP status '
224- '(%(status)i) for %(uri)s' % locals (), status , uri )
218+ def _exception_for_non_200_status (self , status , uri ):
219+ return HTTPError ('Received a very surprising HTTP status '
220+ '(%(status)i) for %(uri)s' % locals (), status , uri )
0 commit comments