
    =                        d 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	m
Z
 ddl	mZ dd	lmZ dd
lmZ  G d de
j                        Z	 	 ddZ	 	 d	 ddZ	 	 ddZ	 	 	 	 ddZ	 	 	 ddZy)z1Helper Classes for using gapic clients in gcloud.    )absolute_import)annotations)division)unicode_literals)external_account)
exceptions)requests)creds)storec                      e Zd ZdZy)MissingStoredCredentialsErrorz?Indicates stored credentials do not exist or are not available.N)__name__
__module____qualname____doc__     %lib/googlecloudsdk/core/gapic_util.pyr   r      s    Gr   r   c                    t        j                  |d      t        j                        st	        d      | rt        j
                        _        j                  fd}|_        S )a  Returns a credential object for use by gapic client libraries.

  Currently, we set _quota_project on the credentials, unlike for http requests,
  which add quota project through request wrapping to implement
  go/gcloud-quota-model-v2.

  Additionally, we wrap the refresh method and plug in our own
  google.auth.transport.Request object that uses our transport.

  Args:
    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.

  Returns:
    A google auth credentials.Credentials object.

  Raises:
    MissingStoredCredentialsError: If a google-auth credential cannot be loaded.
  T)allow_account_impersonationuse_google_authzUnable to load credentialsc                    ~ t        t        j                        rj                  ry  t	        j
                               S )N)
isinstancegoogle_auth_external_accountCredentialsvalidr	   GoogleAuthRequest)requestcredentialsoriginal_refreshs    r   WrappedRefreshz+GetGapicCredentials.<locals>.WrappedRefreshJ   s<     $0026A6G6GH66899r   )r   LoadIfEnabledr
   IsGoogleAuthCredentialsr   GetQuotaProject_quota_project_idrefresh)enable_resource_quotar   r!   r   r    s      @@r   GetGapicCredentialsr(   !   sp    6 ##"=+ 
	&	&{	3
'(D
EE$)$9$9+$FK!
 !((: '+	r   Nc                8    ddl m} |j                  | |||      S )a  Initializes a BidiRpc instances.

  Args:
      client: GAPIC Wrapper client to use.
      start_rpc (grpc.StreamStreamMultiCallable): The gRPC method used to start
        the RPC.
      initial_request: The initial request to yield. This is useful if an
        initial request is needed to start the stream.
      metadata: The metadata headers to use for the RPC. It is a list of tuples.
        The first string in the tuple is the header name and the second is the
        header value.

  Returns:
    A bidiRPC instance.
  r   gapic_util_internal)initial_requestmetadata)googlecloudsdk.corer+   BidiRpc)client	start_rpcr,   r-   r+   s        r   MakeBidiRpcr2   X   s(    , 6		$	$i8 
% 
 r   c                    | j                  d      }|r| j                  n| j                  }|r ||      } |  |||            S )a  Instantiates a gapic REST client with gcloud defaults and configuration.

  Args:
    client_class: a gapic client class.
    credentials: google.auth.credentials.Credentials, the credentials to use.
    address_override_func: function, function to call to override the client
      host. It takes a single argument which is the original host.
    mtls_enabled: bool, True if mTLS is enabled for this client. _

  Returns:
    A gapic API client.
  rest)hostr   	transport)get_transport_classDEFAULT_MTLS_ENDPOINTDEFAULT_ENDPOINT)client_classr   address_override_funcmtls_enabledtransport_classaddresss         r   MakeRestClientr@   u   sO      !44V</2>L..LDaDa'#G,G	W+F
H Hr   c           
     H    ddl m}  | |j                  | |||||            S )a  Instantiates a gapic API client with gcloud defaults and configuration.

  grpc cannot be packaged like our other Python dependencies, due to platform
  differences and must be installed by the user. googlecloudsdk.core.gapic
  depends on grpc and must be imported lazily here so that this module can be
  imported safely anywhere.

  Args:
    client_class: a gapic client class.
    credentials: google.auth.credentials.Credentials, the credentials to use.
    address_override_func: function, function to call to override the client
      host. It takes a single argument which is the original host.
    mtls_enabled: bool, True if mTLS is enabled for this client.
    attempt_direct_path: bool, True if we want to attempt direct path gRPC where
      possible
    redact_request_body_reason: str, the reason why the request body must be
      redacted if --log-http is used. If None, the body is not redacted.

  Returns:
    A gapic API client.
  r   r*   r6   )r.   r+   MakeTransport)r;   r   r<   r=   attempt_direct_pathredact_request_body_reasonr+   s          r   
MakeClientrE      s4    < 6	#11





$	
 	r   c           	     F    ddl m}  | |j                  | ||||            S )a"  Instantiates a gapic API client with gcloud defaults and configuration.

  grpc cannot be packaged like our other Python dependencies, due to platform
  differences and must be installed by the user. googlecloudsdk.core.gapic
  depends on grpc and must be imported lazily here so that this module can be
  imported safely anywhere.

  Args:
    client_class: a gapic client class.
    credentials: google.auth.credentials.Credentials, the credentials to use.
    address_override_func: function, function to call to override the client
      host. It takes a single argument which is the original host.
    mtls_enabled: bool, True if mTLS is enabled for this client.
    attempt_direct_path: bool, True if we want to attempt direct path gRPC where
      possible

  Returns:
    A gapic API client.
  r   r*   r6   )r.   r+   MakeAsyncTransport)r;   r   r<   r=   rC   r+   s         r   MakeAsyncClientrH      s1    6 6	#66





 r   )TT)NN)r-   zlist[tuple[str, str]] | None)NF)NFFN)NFF)r   
__future__r   r   r   r   google.authr   r   r.   r   r	   googlecloudsdk.core.credentialsr
   r   Errorr   r(   r2   r@   rE   rH   r   r   r   <module>rM      s    8 & "  ' H * ( 1 1HJ$4$4 H /3484t -1	 +	> *. %H6 #)^ %r   