
    !                        S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKJrJ	r	J
r
JrJrJr  SSKJr  SSKrSSKJr  SS	KJr  SSKrSSKrSSKrSS
KJr  Sr\R6                  rS\\R:                     S\S\\   S\\	\ /S4      S\\	\/\ 4      S\\   S\\   4S jr!\!\l         " S S\RD                  5      r# " S S\RH                  5      r%g)z#Bigquery Client library for Python.    )absolute_import)division)print_functionN)AnyCallableDictListOptionalTuple)flags)http)model)utils   r   num_retriesreq_typesleeprandurimethodc           	         [        XX#XEU/UQ70 UD6u  p[        U	R                  5      S:X  a  [        R                  " U
R                  S5      5      n[        U[        5      (       aY  SUS   ;   aP  US   S   nSU;   aB  SU;   a<  SUS   ;   a3  US   S	 [        R                  " S5        [        U UUUUUU/UQ70 UD6u  pX4$ )	a  Conditionally retries an HTTP request.


If the original request fails with a specific permission error, retry it once
without the x-goog-user-project header.

Args:
  http: Http object to be used to execute request.
  num_retries: Maximum number of retries.
  req_type: Type of the request (used for logging retries).
  sleep: Function to sleep for random time between retries.
  rand: Function to sleep for random time between retries.
  uri: URI to be requested.
  method: HTTP method to be used.
  *args: Additional arguments passed to http.request.
  **kwargs: Additional arguments passed to http.request.

Returns:
  resp, content - Response from the http request (may be HTTP 5xx).
i  zutf-8messageerrorz'roles/serviceusage.serviceUsageConsumerheadersx-goog-user-projectz7Retrying request without the x-goog-user-project header)
(_ORIGINAL_GOOGLEAPI_CLIENT_RETRY_REQUESTintstatusjsonloadsdecode
isinstancedictlogginginfo)r   r   r   r   r   r   r   argskwargsrespcontentdataerr_messages                $platform/bq/clients/bigquery_http.py_RetryRequestr-   !   s    B ;
$V>BFL-$ 	::gnnW-.D$)tG}"<M),k	2k	A#8F9<M#MY 56
,,G C
 
 
-$ 
    c                      ^  \ rS rSrSr  SS\\   S\\   4U 4S jjjrS\\\4   S\\\4   S\\\	4   S	\
S
\\\\4   \\\4   \\4   4
U 4S jjrS\R                  S\S
\
4U 4S jjrSrU =r$ )BigqueryModela   z0Adds optional global parameters to all requests.tracequota_project_idc                 >   > [         TU ]  " S0 UD6  Xl        X l        g )N )super__init__r2   r3   )selfr2   r3   kwds	__class__s       r,   r7   BigqueryModel.__init__d   s      
GtJ,r.   r   path_paramsquery_params
body_valuereturnc                   > SU;  a   U R                   (       a  U R                   US'   SU;  a  SUS'   SR                  [        R                  " 5       US   /5      nUR	                  5       US'   U R
                  (       a  U R
                  US'   [        R                  R                  (       a  [        R                  R                  US'   [        TU ])  XX45      $ )a  Updates outgoing request.

Headers updated here will be applied to only requests of API methods having
JSON-type responses. For API methods with non-JSON-type responses, headers
need to be set in BigqueryHttp.Factory._Construct.


Args:
  headers: dict, request headers
  path_params: dict, parameters that appear in the request path
  query_params: dict, parameters that appear in the query
  body_value: object, the request body as a Python object, which must be
    serializable.

Returns:
  A tuple of (headers, path_params, query, body)

  headers: dict, request headers
  path_params: dict, parameters that appear in the request path
  query: string, query part of the request URI
  body: string, the body serialized in the desired wire format.
r2   cookie
user-agent  r   x-goog-request-reason)r2   joinbq_utilsGetUserAgentstripr3   bq_flagsREQUEST_REASONvaluer6   request)r8   r   r<   r=   r>   
user_agentr:   s         r,   rM   BigqueryModel.requesto   s    : l"tzz**gh7" gl8002GL4IJKJ&,,.GL'+'<'<g#$$$)1)@)@)F)Fg%&7?7JJr.   r(   r)   c                 V   > [         R                  " SUS   5        [        TU ]  X5      $ )a6  Convert the response wire format into a Python object.


Args:
  resp: httplib2.Response, the HTTP response headers and status
  content: string, the body of the HTTP response

Returns:
  The body de-serialized as a Python object.

Raises:
  googleapiclient.errors.HttpError if a non 2xx response is received.
z)Response from server with status code: %sr   )r$   r%   r6   response)r8   r(   r)   r:   s      r,   rQ   BigqueryModel.response   s(     LL<d8nM7D**r.   )r3   r2   NN)__name__
__module____qualname____firstlineno____doc__r
   strr7   r   r   objectr   rM   httplib2ResponserQ   __static_attributes____classcell__r:   s   @r,   r0   r0   a   s    8 "(,-c]- !- -+KCH~+K S>+K cN	+K
 +K T#s(^T#s(^S#56+K`+8,, +s +v + +r.   r0   c                      ^  \ rS rSrSrS\4U 4S jjr\S\S\S   4S j5       r	  SS\
\R                     S	\
\   4U 4S
 jjjrSrU =r$ )BigqueryHttp   z%Converts errors into Bigquery errors.bigquery_modelc                 n   > [         TU ]  " U0 UD6  [        R                  " SUS   US   5        Xl        g )Nz)URL being requested from BQ client: %s %sr      )r6   r7   r$   r%   _model)r8   rc   r&   r9   r:   s       r,   r7   BigqueryHttp.__init__   s9     
Gd#d#LL3T(^T!W !Kr.   r?   ).ra   c                    ^  U 4S jnU$ )zDReturns a function that creates a BigqueryHttp with the given model.c                    > SU;  a  0 US'   US   R                  SS5      n[        R                  " 5       n[        R	                  U5      [        R	                  U5      ;  a(  SR                  X2/5      nUR                  5       US   S'   SUS   ;  aD  TR                  (       a3  [        R                  " STR                  5        TR                  US   S'   SUS   ;  an  [        R                  R                  (       aO  [        R                  " S[        R                  R                  5        [        R                  R                  US   S'   Tn[        U/U Q70 UD6$ )	Nr   rB   rC   rD   r   z)Setting x-goog-user-project header to: %srE   z+Setting x-goog-request-reason header to: %s)getrG   rH   rY   lowerrF   rI   r3   r$   r%   rJ   rK   rL   ra   )r&   r9   rN   bq_user_agentcaptured_modelrc   s        r,   
_Construct(BigqueryHttp.Factory.<locals>._Construct   sD    
$	Y 	?&&|R8j++-m	=	!:)>	>XX}9:
(2(8(8(:Y%  tI
6--7++	
 2@1P1PY-. "i
8%%++9##))	
 4<3J3J3P3PY/0%n
  r.   r5   )rc   rn   s   ` r,   FactoryBigqueryHttp.Factory   s    (T r.   r   r   c                   >  Uc  [         n[        TU ]	  UUS9$ ! [        R                  R
                   aP  nU R                  R                  UR                  UR                  5        [        R                  " U5         S nAg S nAf[        R                  [        4 a   n[        R                  " U5         S nAg S nAff = f)N)r   r   )#_NUM_RETRIES_FOR_SERVER_SIDE_ERRORSr6   executegoogleapiclienterrors	HttpErrorrf   _log_responser(   r)   bq_client_utilsRaiseErrorFromHttpErrorr[   HttpLib2ErrorIOErrorRaiseErrorFromNonHttpError)r8   r   r   er:   s       r,   rt   BigqueryHttp.execute   s    6;+w#  
 	
 ##-- 3 	!!!&&!))4//22$$g. 6221556s!    C ABC  B;;C )rf   rS   )rT   rU   rV   rW   rX   r0   r7   staticmethodr   rp   r
   r[   Httpr   rt   r]   r^   r_   s   @r,   ra   ra      so    -
!#
! /#/#$/ /n '+#'6X]]#6 C=6 6r.   ra   )&rX   
__future__r   r   r   r   r$   typingr   r   r   r	   r
   r   abslr   ru   r   http_requestr   r[   rJ   rG   clientsr   ry   rs   _retry_requestr   r   r   rY   floatr-   	JsonModelr0   HttpRequestra   r5   r.   r,   <module>r      s    * &  %   = =   0 !    ,&' # ,8+F+F (9
8==
!99 sm9 HeWd]+,	9
 8SE5L)
*9 
#9 SM9x , M+EOO M+dY6<++ Y6r.   