@@ -327,8 +327,8 @@ def set_limit_first(self, limit):
327
327
Raises:
328
328
ValueError: If the value is not an integer, or set_limit_last() was called previously.
329
329
"""
330
- if not isinstance (limit , int ):
331
- raise ValueError ('Limit must be an integer.' )
330
+ if not isinstance (limit , int ) or limit < 0 :
331
+ raise ValueError ('Limit must be a non-negative integer.' )
332
332
if 'limitToLast' in self ._params :
333
333
raise ValueError ('Cannot set both first and last limits.' )
334
334
self ._params ['limitToFirst' ] = limit
@@ -346,8 +346,8 @@ def set_limit_last(self, limit):
346
346
Raises:
347
347
ValueError: If the value is not an integer, or set_limit_first() was called previously.
348
348
"""
349
- if not isinstance (limit , int ):
350
- raise ValueError ('Limit must be an integer.' )
349
+ if not isinstance (limit , int ) or limit < 0 :
350
+ raise ValueError ('Limit must be a non-negative integer.' )
351
351
if 'limitToFirst' in self ._params :
352
352
raise ValueError ('Cannot set both first and last limits.' )
353
353
self ._params ['limitToLast' ] = limit
@@ -608,6 +608,23 @@ def request_oneway(self, method, urlpath, **kwargs):
608
608
self ._do_request (method , urlpath , ** kwargs )
609
609
610
610
def _do_request (self , method , urlpath , ** kwargs ):
611
+ """Makes an HTTP call using the Python requests library.
612
+
613
+ Refer to http://docs.python-requests.org/en/master/api/ for more information on supported
614
+ options and features.
615
+
616
+ Args:
617
+ method: HTTP method name as a string (e.g. get, post).
618
+ urlpath: URL path of the remote endpoint. This will be appended to the server's base URL.
619
+ kwargs: An additional set of keyword arguments to be passed into requests API
620
+ (e.g. json, params).
621
+
622
+ Returns:
623
+ Response: An HTTP response object.
624
+
625
+ Raises:
626
+ ApiCallError: If an error occurs while making the HTTP call.
627
+ """
611
628
try :
612
629
resp = self ._session .request (method , self ._url + urlpath , auth = self ._auth , ** kwargs )
613
630
resp .raise_for_status ()
@@ -616,11 +633,29 @@ def _do_request(self, method, urlpath, **kwargs):
616
633
raise ApiCallError (self ._extract_error_message (error ), error )
617
634
618
635
def _extract_error_message (self , error ):
619
- if error .response is not None :
620
- data = json .loads (error .response .content )
636
+ """Extracts an error message from an exception.
637
+
638
+ If the server has not sent any response, simply converts the exception into a string.
639
+ If the server has sent a JSON response with an 'error' field, which is the typical
640
+ behavior of the Realtime Database REST API, parses the response to retrieve the error
641
+ message. If the server has sent a non-JSON response, returns the full response
642
+ as the error message.
643
+
644
+ Args:
645
+ error: An exception raised by the requests library.
646
+
647
+ Returns:
648
+ str: A string error message extracted from the exception.
649
+ """
650
+ if error .response is None :
651
+ return str (error )
652
+ try :
653
+ data = error .response .json ()
621
654
if isinstance (data , dict ):
622
655
return '{0}\n Reason: {1}' .format (error , data .get ('error' , 'unknown' ))
623
- return str (error )
656
+ except ValueError :
657
+ pass
658
+ return '{0}\n Reason: {1}' .format (error , error .response .content .decode ())
624
659
625
660
def close (self ):
626
661
self ._session .close ()
0 commit comments