@@ -210,6 +210,7 @@ def __init__(
210210 request_timeout = constants .DEFAULT_REQUEST_TIMEOUT , # type: Union[float, Tuple[float, float]]
211211 handle_retry = exceptions .RetryWithExponentialBackoff (),
212212 service_account_file = None ,
213+ json_lib = None ,
213214 ):
214215 # type: (...) -> None
215216 self ._client_session = ClientSession (
@@ -258,6 +259,7 @@ def __init__(
258259 self ._handle_retry = handle_retry
259260 self .max_attempts = max_attempts
260261 self ._http_scheme = http_scheme
262+ self .json_lib = json_lib
261263
262264 @property
263265 def transaction_id (self ):
@@ -355,6 +357,8 @@ def post(self, sql):
355357 while http_response is not None and http_response .is_redirect :
356358 location = http_response .headers ["Location" ]
357359 url = self ._redirect_handler .handle (location )
360+ log_level = logger .getEffectiveLevel ()
361+ if log_level <= logging .logging .INFO :
358362 logger .info (
359363 "redirect {} from {} to {}" .format (
360364 http_response .status_code , location , url
@@ -406,9 +410,14 @@ def process(self, http_response):
406410 if not http_response .ok :
407411 self .raise_response_error (http_response )
408412
413+ log_level = logger .getEffectiveLevel ()
409414 http_response .encoding = "utf-8"
410- response = http_response .json ()
411- logger .debug ("HTTP {}: {}" .format (http_response .status_code , response ))
415+ if self .json_lib :
416+ response = self .json_lib .loads (http_response .content )
417+ else :
418+ response = http_response .json ()
419+ if log_level <= logging .logging .DEBUG :
420+ logger .debug ("HTTP {}: {}" .format (http_response .status_code , response ))
412421 if "error" in response :
413422 raise self ._process_error (response ["error" ], response .get ("id" ))
414423
@@ -474,11 +483,13 @@ def __iter__(self):
474483 self ._rows = None
475484
476485 # Subsequent fetches from GET requests until next_uri is empty.
486+ log_level = logger .getEffectiveLevel ()
477487 while not self ._query .is_finished ():
478488 rows = self ._query .fetch ()
479489 for row in rows :
480490 self ._rownumber += 1
481- logger .debug ("row {}" .format (row ))
491+ if log_level <= logging .logging .DEBUG :
492+ logger .debug ("row {}" .format (row ))
482493 yield row
483494
484495
@@ -554,7 +565,9 @@ def fetch(self):
554565 if status .columns :
555566 self ._columns = status .columns
556567 self ._stats .update (status .stats )
557- logger .debug (status )
568+ log_level = logger .getEffectiveLevel ()
569+ if log_level <= logging .logging .DEBUG :
570+ logger .debug (status )
558571 if status .next_uri is None :
559572 self ._finished = True
560573 return status .rows
@@ -565,13 +578,17 @@ def cancel(self):
565578 if self .query_id is None or self .is_finished ():
566579 return
567580
581+ log_level = logger .getEffectiveLevel ()
568582 self ._cancelled = True
569583 url = self ._request .get_url ("/v1/query/{}" .format (self .query_id ))
570- logger .debug ("cancelling query: %s" , self .query_id )
584+ if log_level <= logging .logging .DEBUG :
585+ logger .debug ("cancelling query: %s" , self .query_id )
571586 response = self ._request .delete (url )
572- logger .info (response )
587+ if log_level <= logging .logging .INFO :
588+ logger .info (response )
573589 if response .status_code == requests .codes .no_content :
574- logger .debug ("query cancelled: %s" , self .query_id )
590+ if log_level <= logging .logging .DEBUG :
591+ logger .debug ("query cancelled: %s" , self .query_id )
575592 return
576593 self ._request .raise_response_error (response )
577594
0 commit comments