
                         
   d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddl	Z	ddl
mZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlZ	 	 	 	 	 	 ddZ G d d      Z G d dej,                  ej.                  ej0                        Zy)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                    t        j                  | ||      }|t        j                         }t	               }t        j                  ||      }|j                  |||||      }|j                  ||||      }t        |d      rF|j                  }	t        j                  |	      rt        |	      }
n|	}
t        |j                  d|
       |S )a2  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 		'=N#+-+ ((*O"$/##!?+  ))! * +  //!	 0 + [7833E))%05e<nn K?	    c                       e Zd ZdZd Zd Zy)r   zClass of wrapping credentials.c                     || _         y Nr   )selfr   s     r%   __init__z'_GoogleAuthApitoolsCredentials.__init__o   s
    "Dr&   c                     ~t        | j                  t        j                        r| j                  j                  ry | j                  j                  t        j                                y r)   )
isinstancer   google_auth_external_accountCredentialsvalidrefreshr   GoogleAuthRequest)r*   r"   s     r%   r1   z&_GoogleAuthApitoolsCredentials.refreshr   sN    $0026:6F6F6L6LT3356r&   N)__name__
__module____qualname____doc__r+   r1    r&   r%   r   r   l   s    &#7r&   r   c                        e Zd ZdZd Z	 ddZy)r   z-Class for wrapping httplib.Httplib2 requests.c                     t        j                  |      rt        j                  ||      }|S |j	                  |      }|S )z=Returns an http_client authorized with the given credentials.)r   r   google_auth_httplib2AuthorizedHttp	authorize)r*   r"   r	   s      r%   AuthorizeClientzRequestWrapper.AuthorizeClient   s@    ))%0(77{Kk  OOK0kr&   Nc                    	 | j                  ||||      }|s|S |j                  | j                  ||      		fd}t        j                         r	||_        |S 	|_        |S )z3Returns an http_client with quota project handling.r   c                      | i |\  }}|j                   dk7  r||fS t        j                  |      }	 t        j                  |      d   d   }|D ]f  }|j                  d      dk(  s|j                  d      t        j                  k(  s;|j                  d      t        j                  k(  s^ | i |c S  ||fS # t
        t        j                  f$ r ||fcY S w xY w)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%   RequestWithRetryz2RequestWrapper.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.
. $ w ,,- !  !s   C   C C )QuotaProjectr   QuotaWrappedRequestr   #UserProjectQuotaWithFallbackEnabled)
r*   r"   r   r    r!   r   quota_projectrW   rU   rV   s
           @@r%   r   zRequestWrapper.WrapQuota   s     %%#	 & M &&L..]$O> //1,k  ,kr&   r)   )r3   r4   r5   r6   r=   r   r7   r&   r%   r   r   {   s    5 8r&   r   )unsetNNTTN)r6   
__future__r   r   r   rG   google.authr   r.   r:   googlecloudsdk.callioper   googlecloudsdk.corer   googlecloudsdk.core.credentialsr	   r   r
   r   rE   r   r   CredentialWrappingMixinQuotaHandlerMixinr   r7   r&   r%   <module>rd      sy     G &  '  H  ( $ ? 1 5 
 #%)GT7 7DY6600$2E2EDr&   