
                            S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSK	r	SSK
Jr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKr      SS jr " S S5      r " S S\R,                  \R.                  \R0                  5      rg)z@A module to get a credentialed http object for making API calls.    )absolute_import)division)unicode_literalsN)external_account)base)http)creds)store)	transportc                    [         R                  " XUS9nUc  [        R                  " 5       n[	        5       n[
        R                  " XE5      nUR                  UUUUUS9nUR                  UUUUS9n[        US5      (       aL  UR                  n	[        R                  " U	5      (       a  [        U	5      n
OU	n
[        UR                  SU
5        U$ )a  Get an httplib2.Http client for working with the Google API.

Args:
  timeout: double, The timeout in seconds to pass to httplib2.  This is the
      socket level timeout.  If timeout is None, timeout is infinite.  If
      default argument 'unset' is given, a sensible default is selected.
  response_encoding: str, the encoding to use to decode the response.
  ca_certs: str, absolute filename of a ca_certs file that overrides the
      default
  enable_resource_quota: bool, By default, we are going to tell APIs to use
      the quota of the project being operated on. For some APIs we want to use
      gcloud's quota, so you can explicitly disable that behavior by passing
      False here.
  allow_account_impersonation: bool, True to allow use of impersonated service
    account credentials for calls made with this client. If False, the active
    user credentials will always be used.
  use_google_auth: bool, True if the calling command indicates to use
    google-auth library for authentication. If False, authentication will
    fallback to using the oauth2client library. If None, set the value based
    on the configuration.

Returns:
  1. A regular httplib2.Http object if no credentials are available;
  2. Or a httplib2.Http client object authorized by oauth2client
     credentials if use_google_auth==False;
  3. Or a google_auth_httplib2.AuthorizedHttp client object authorized by
     google-auth credentials.

Raises:
  core.credentials.exceptions.Error: If an error loading the credentials
    occurs.
)timeoutresponse_encodingca_certscredentials_googlecloudsdk_credentialsr   )r   Httpr   UseGoogleAuthRequestWrapperr
   LoadIfEnabled	WrapQuotaWrapCredentialshasattrr   
core_credsIsGoogleAuthCredentials_GoogleAuthApitoolsCredentialssetattrrequest)r   r   r   enable_resource_quotaallow_account_impersonationuse_google_authhttp_clientrequest_wrapperr   r	   apitools_credss              +lib/googlecloudsdk/core/credentials/http.pyr   r   "   s    L 		'#+-+ ((*O"$/##!+  ))! * +  //!	 0 + [78833E))%005e<nn K?	    c                   $    \ rS rSrSrS rS rSrg)r   l   zClass of wrapping credentials.c                     Xl         g Nr   )selfr   s     r%   __init__'_GoogleAuthApitoolsCredentials.__init__o   s    "r&   c                     A[        U R                  [        R                  5      (       a  U R                  R                  (       a  g U R                  R                  [        R                  " 5       5        g r*   )
isinstancer   google_auth_external_accountCredentialsvalidrefreshr   GoogleAuthRequest)r+   r"   s     r%   r3   &_GoogleAuthApitoolsCredentials.refreshr   sS    $002 26:6F6F6L6LT3356r&   r   N)__name__
__module____qualname____firstlineno____doc__r,   r3   __static_attributes__ r&   r%   r   r   l   s    &#7r&   r   c                   *    \ rS rSrSrS r SS jrSrg)r   {   z-Class for wrapping httplib.Httplib2 requests.c                     [         R                  " U5      (       a  [        R                  " X!5      nU$ UR	                  U5      nU$ )z=Returns an http_client authorized with the given credentials.)r   r   google_auth_httplib2AuthorizedHttp	authorize)r+   r"   r	   s      r%   AuthorizeClientRequestWrapper.AuthorizeClient   sA    ))%00(77Kk  OOK0kr&   Nc                    ^^	 U R                  UUUUS9nU(       d  U$ UR                  mU R                  X5      m	UU	4S jn[        R                  " 5       (       a  Xql        U$ T	Ul        U$ )z3Returns an http_client with quota project handling.r   c                    > T" U 0 UD6u  p#UR                   S:w  a  X#4$ [        R                  " U5      n [        R                  " U5      S   S   nU Hl  nUR                  S5      S:X  d  M  UR                  S5      [        R                  :X  d  M?  UR                  S5      [        R                  :X  d  Md  T" U 0 UD6s  $    X#4$ ! [
        [        R                  4 a    X#4s $ f = f)a  Retries the request after removing the quota project header.

Try the request with the X-Goog-User-Project header. If the account does
not have the permission to expense the quota of the user project in the
header, remove the header and retry.

Args:
  *args: *args to send to httplib2.Http.request method.
  **kwargs: **kwargs to send to httplib2.Http.request method.

Returns:
  Response from httplib2.Http.request.
i  errordetailsz@typez(type.googleapis.com/google.rpc.ErrorInforeasondomain)statussixensure_textjsonloadsKeyErrorJSONDecodeErrorgetr   USER_PROJECT_ERROR_REASONUSER_PROJECT_ERROR_DOMAIN)	argskwargsresponsecontentcontent_texterr_details
err_detailorig_requestwrapped_requests	          r%   RequestWithRetry2RequestWrapper.WrapQuota.<locals>.RequestWithRetry   s     *4:6:h	C	  __W-l!jj.w7	B $*NN7#9:NN8$	(K(KKNN8$	(K(KKt.v.
. $  ,,- !  !s   C C'&C')QuotaProjectr   QuotaWrappedRequestr   #UserProjectQuotaWithFallbackEnabled)
r+   r"   r   r    r!   r   quota_projectr^   r\   r]   s
           @@r%   r   RequestWrapper.WrapQuota   s     %%#	 & M &&L..$O> //11,  ,kr&   r<   r*   )r6   r7   r8   r9   r:   rC   r   r;   r<   r&   r%   r   r   {   s    5 8r&   r   )unsetNNTTN)r:   
__future__r   r   r   rN   google.authr   r0   r@   googlecloudsdk.callioper   googlecloudsdk.corer   googlecloudsdk.core.credentialsr	   r   r
   r   rL   r   r   CredentialWrappingMixinQuotaHandlerMixinr   r<   r&   r%   <module>rm      sy     G &  '  H  ( $ ? 1 5 
 #%)GT7 7DY6600$2E2EDr&   