
    }                        d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZm	Z	m
Z
mZmZmZ ddlmZ ddlZddlmZ dd	lmZ ddlZddlZddlZdd
lmZ dZej6                  Zdeej:                     dedee   dee	e gdf      dee	ege f      dee   dee   fdZ!e!e_         G d dejD                        Z# G d dejH                        Z%y)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           	      t   t        | ||||||g|i |\  }	}
t        |	j                        dk(  rt        j                  |
j                  d            }t        |t              rQd|d   v rJ|d   d   }d|v r>d|v r:d|d   v r3|d   d= t        j                  d       t        | ||||||g|i |\  }	}
|	|
fS )	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 ;
K5$V>BFL-$ 	::gnnW-.D$)tG}"<M),k	2k	A#8F9<M#MY 56
,,G C
 
 
-$ 
w    c                        e Zd ZdZ	 	 ddee   dee   f fdZdeeef   deeef   deeef   de	d	e
eeef   eeef   eef   f
 fd
Zdej                  ded	e	f fdZ xZS )BigqueryModelz0Adds optional global parameters to all requests.tracequota_project_idc                 @    t        |   di | || _        || _        y )N )super__init__r1   r2   )selfr1   r2   kwds	__class__s       r,   r6   zBigqueryModel.__init__d   s$     
GtDJ,Dr.   r   path_paramsquery_params
body_valuereturnc                 
   d|vr| j                   r| j                   |d<   | j                  r| j                  |d<   t        j                  j                  rt        j                  j                  |d<   t
        |   ||||      S )aX  Updates outgoing request.


    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.
    r1   cookier   zx-goog-request-reason)r1   r2   bq_flagsREQUEST_REASONvaluer5   request)r7   r   r:   r;   r<   r9   s        r,   rC   zBigqueryModel.requesto   sv    2 l"tzz**gh'+'<'<g#$$$)1)@)@)F)Fg%&7?7KzJJr.   r(   r)   c                 V    t        j                  d|d          t        |   ||      S )aV  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%   r5   response)r7   r(   r)   r9   s      r,   rE   zBigqueryModel.response   s*     LL<d8nM7D'**r.   NN)__name__
__module____qualname____doc__r
   strr6   r   r   objectr   rC   httplib2ResponserE   __classcell__r9   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N+8,, +s +v + +r.   r0   c                        e Zd ZdZdef fdZedededed   fd       Z		 	 dde
ej                     d	e
e   f fd
Z xZS )BigqueryHttpz%Converts errors into Bigquery errors.bigquery_modelc                 l    t        |   |i | t        j                  d|d   |d          || _        y )Nz)URL being requested from BQ client: %s %sr      )r5   r6   r$   r%   _model)r7   rS   r&   r8   r9   s       r,   r6   zBigqueryHttp.__init__   s;     
Gd#d#LL3T(^T!W !DKr.   use_google_authr=   ).rR   c                       fd}|S )zDReturns a function that creates a BigqueryHttp with the given model.c                      rPt        j                         }d|vri |d<   n*d|d   v r#||d   d   vrdj                  ||d   d   g      }||d   d<   }t        |g| i |S )Nr   z
User-Agent )bq_utilsGetUserAgentjoinrR   )r&   r8   
user_agentcaptured_modelrS   rW   s       r,   
_Constructz(BigqueryHttp.Factory.<locals>._Construct   s    	**,
D $y/DO+$y/,"??xxT)_\-J KL*(2Y%%n
  r.   r4   )rS   rW   r`   s   `` r,   FactoryzBigqueryHttp.Factory   s    & r.   r   r   c                    	 |t         }t        | 	  ||      S # t        j                  j
                  $ rO}| j                  j                  |j                  |j                         t        j                  |       Y d }~y d }~wt        j                  t        f$ r}t        j                  |       Y d }~y d }~ww xY w)N)r   r   )#_NUM_RETRIES_FOR_SERVER_SIDE_ERRORSr5   executegoogleapiclienterrors	HttpErrorrV   _log_responser(   r)   bq_client_utilsRaiseErrorFromHttpErrorrM   HttpLib2ErrorIOErrorRaiseErrorFromNonHttpError)r7   r   r   er9   s       r,   rd   zBigqueryHttp.execute   s    6;+w#  
 	
 ##-- 3 	!!!&&!))4//22$$g. 6221556s!    B>ABB>B99B>rF   )rG   rH   rI   rJ   r0   r6   staticmethodboolr   ra   r
   rM   Httpr   rd   rO   rP   s   @r,   rR   rR      sy    -
!#
! # #$ B '+#'6X]]#6 C=6 6r.   rR   )&rJ   
__future__r   r   r   r   r$   typingr   r   r   r	   r
   r   abslr   re   r   http_requestr   rM   r@   r[   clientsr   ri   rc   _retry_requestr   rq   r   rK   floatr-   	JsonModelr0   HttpRequestrR   r4   r.   r,   <module>r{      s    * &  %   = =   0 !    ,&' # ,8+F+F (9
8==
!99 sm9 HeWd]+,	9
 8SE5L)
*9 
#9 SM9x , D+EOO D+RC6<++ C6r.   