
    _4                        S 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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Jr  SSKr " S S\R$                  5      r " S S\5      rS rS rS rS"S jrSrSrSrS r S#S jrS#S jrS r   S$S jr\R@                  RB                  4S jr"S\R@                  RB                  SSS4S jr#S"S jr$S  r%S! r&g)%z/Library for obtaining API clients and messages.    )absolute_import)division)unicode_literals)
exceptions)api_enablement)apis_internal)	apis_util)
properties)apis_mapNc                       \ rS rSrSrSrg)Error#   z$A base class for apis helper errors. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       'lib/googlecloudsdk/api_lib/util/apis.pyr   r   #   s    ,r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )GapicRestUnsupportedError(   z=An error for the unsupported REST transport on GAPIC Clients.c                 ,   > [         [        U ]  S5        g )Nz5REST transport is not yet supported for GAPIC Clients)superr   __init__)self	__class__s    r   r   "GapicRestUnsupportedError.__init__+   s    	!NOr   r   )r   r   r   r   r   r   r   __classcell__)r   s   @r   r   r   (   s    EP Pr   r   c                     [         R                  " U R                  5       H0  u  p[         R                  " U5       H  u  p4[        XU5        M     M2     g N)six	iteritemsMAP_AddToApisMap)unreleased_apis_mapapi_nameapi_versionsapi_versionapi_defs        r   AddUnreleasedAPIsr-   1   s@     #.A.E.E Fh #l ;H73 !< !Gr   c                     [         R                  " U 5      u  p[        R                  R	                  U 0 5      nU(       + Ul        X$U'   U[        R                  U '   g)a  Adds the APIDef specified by the given arguments to the APIs map.

This method should only be used for runtime patching of the APIs map.
Additions to the map should ensure that there is only one and only one default
version for each API.

Args:
  api_name: str, The API name (or the command surface name, if different).
  api_version: str, The version of the API.
  api_def: APIDef for the API version.
N)r   _GetApiNameAndAliasr   r&   getdefault_version)r)   r+   r,   _r*   s        r   r'   r'   8   sO     11(;+( !!(B/, ,,'%{'(,,xr   c                 .    [         R                  " U 5      $ )zReturn available versions for given api.

Args:
  api_name: str, The API name (or the command surface name, if different).

Raises:
  UnknownAPIError: If api_name does not exist in the APIs map.

Returns:
  list, of version names.
)r   _GetVersions)r)   s    r   GetVersionsr5   P   s     
	#	#H	--r   c                    [         R                  " U 5      u  pU [        R                  ;  a  [        R
                  " U 5      e[        R                  R                  R                  5       nSnU(       a!  UR                  SR                  X!5      S5      nU(       d  UR                  X!5      nU=(       d    [         R                  " U 5      $ )a=  Resolves the version for an API based on the APIs map and API overrides.

Args:
  api_name: str, The API name (or the command surface name, if different).
  api_version: str, The API version.

Raises:
  apis_internal.UnknownAPIError: If api_name does not exist in the APIs map.

Returns:
  str, The resolved version.
Nz{}/{})r   r/   r   r&   r	   UnknownAPIErrorr
   VALUESapi_client_overrides	AllValuesr0   format_GetDefaultVersion)r)   r+   api_name_aliasversion_overridesapi_version_overrides        r   ResolveVersionr@   `   s     +>>xH(X\\!

#
#H
-- ''<<FFH ,00~3T;	,00M
 5

*
*8
46r   i  i  <   c                     [         R                  " U 5      nUR                  R                  [        :w  a  g[
        R                  " UR                  R                  5      nU(       a  X!4-   $ g)ag  Returns the API Enablement info or None if prompting is not necessary.

Args:
  exception (apitools_exceptions.HttpError): Exception if an error occurred.

Returns:
  tuple[str]: The project, service token, exception tuple to be used for
    prompting to enable the API.

Raises:
  api_exceptions.HttpException: If gcloud should not prompt to enable the API.
N)api_exceptionsHttpExceptionpayloadstatus_code)API_ENABLEMENT_ERROR_EXPECTED_STATUS_CODEr   GetApiEnablementInfostatus_message)	exceptionparsed_errorenablement_infos      r   rH   rH      s^      --i8,&&/0"77))+/_,,	r   Fc                 |    [         R                  " X5      nU(       a  U(       d  [        R                  " S5      egUe)a   Prompts to enable the API and throws if the answer is no.

Args:
  project (str): The project that the API is not enabled on.
  service_token (str): The service token of the API to prompt for.
  exception (api_Exceptions.HttpException): Exception to throw if the prompt
    is denied.
  is_batch_request: If the request is a batch request. This determines how to
    get apitools to retry the request.

Raises:
  api_exceptions.HttpException: API not enabled error if the user chooses to
    not enable the API.
RetryN)r   PromptToEnableApiapitools_exceptionsRequestError)projectservice_tokenrJ   is_batch_requestapi_enable_attempteds        r   rO   rO      s;      (99,,W55  Or   c                 .   ^ ^^ SS0mU U4S jmU4S jnU$ )z+Returns a callback for checking API errors.already_prompted_to_enableFc                    > [        U 5      nU(       a5  TS   (       d  T(       a  [        R                  " S5      eSTS'   [        U6   g g )NrW   rN   T)rH   rP   rQ   rO   )response_as_errorrL   skip_activation_promptstates     r   _CheckForApiEnablementError2CheckResponse.<locals>._CheckForApiEnablementError   sH    
 ++<=O	+	,0F!..w77,0e())	 r   c                   > U c#  [         R                  " SU R                  -  5      eU R                  [        :X  au  U R
                  (       a  U R
                  [        :  a  gU R
                  (       a  [         R                  R                  U 5      e[         R                  R                  U 5      eU R                  S:  a  [         R                  R                  U 5      eU R
                  (       a  [         R                  R                  U 5      e[         R                  R                  U 5      n[        R                  R                  R                  R                  5       (       a	  T" U5        gg)a@  Checks API error.

If it's an enablement error, prompt to enable & retry.
If it's a resource exhausted error, no retry & return.

Args:
  response: response that had an error.

Raises:
  apitools_exceptions.RequestError: error which should signal apitools to
    retry.
  api_exceptions.HttpException: the parsed error.
Nz,Request to url %s did not return a response.i  )rP   rQ   request_urlrF   RESOURCE_EXHAUSTED_STATUS_CODEretry_afterMAX_RETRY_DELAY_SECRetryAfterErrorFromResponseBadStatusCodeError	HttpErrorr
   r8   coreshould_prompt_to_enable_apiGetBool)responserY   r\   s     r   _CheckResponse%CheckResponse.<locals>._CheckResponse   s     ,,
88;O;O
O  
		!?	?			("6"69L"L			!11>>xHH!44AA(KK				$22??II			//<<XFF+55BB8L99AACC!"34 Dr   r   )rZ   rk   r\   r[   s   ` @@r   CheckResponserm      s     '
/%
*'5R 
r   c                 .    [         R                  " X5      $ )a  Returns the client class for the API specified in the args.

Args:
  api_name: str, The API name (or the command surface name, if different).
  api_version: str, The version of the API.

Returns:
  base_api.BaseApiClient, Client class for the specified API.
)r   _GetClientClass)r)   r+   s     r   GetClientClassrp      s     
	&	&x	==r   c           	      F    [         R                  " U UUS[        U5      US9$ )a  Returns an instance of the API client specified in the args.

Args:
  api_name: str, The API name (or the command surface name, if different).
  api_version: str, The version of the API.
  no_http: bool, True to not create an http object for this client.
  http_timeout_sec: int, seconds for http timeout, default if None.
  skip_activation_prompt: bool, if true, do not prompt for service activation.

Returns:
  base_api.BaseApiClient, An instance of the specified API client.
N)http_timeout_sec)r   _GetClientInstancerm   )r)   r+   no_httprr   rZ   s        r   GetClientInstanceru     s/    ( 
	)	)
*+'
 r   c                 |    U[         R                  R                  :X  a
  [        5       e[        R
                  " XUS9$ )at  Returns the GAPIC client class for the API specified in the args.

Args:
  api_name: str, The API name (or the command surface name, if different).
  api_version: str, The version of the API.
  transport: apis_util.GapicTransport, The transport class to obtain.

Raises:
  GapicRestUnsupportedError: If transport is REST.

Returns:
  The specified GAPIC API Client class.
)transport_choice)r	   GapicTransportRESTr   r   _GetGapicClientClass)r)   r+   	transports      r   GetGapicClientClassr|      s9      )**///
#
%%		+	+i
9 9r   c                     SSK Jn  U[        R                  R                  :X  a
  [        5       eUR                  5       n[        R                  " U UUUUUUUS9$ )aj  Returns an instance of the GAPIC API client specified in the args.

Args:
  api_name: str, The API name (or the command surface name, if different).
  api_version: str, The version of the API.
  address_override_func: function, function to call to override the client
    host. It takes a single argument which is the original host.
  transport: apis_util.GapicTransport, The transport to be used by the 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.
  custom_interceptors: list[grpc interceptor], a list of custom
    interceptors to add to the channel.

Raises:
  GapicRestUnsupportedError: If transport is REST.

Returns:
  An instance of the specified GAPIC API client.
r   )
gapic_util)address_override_funcrw   attempt_direct_pathredact_request_body_reasoncustom_interceptors)	googlecloudsdk.corer~   r	   rx   ry   r   GetGapicCredentialsr   _GetGapicClientInstance)	r)   r+   r   r{   r   r   r   r~   credentialss	            r   GetGapicClientInstancer   7  s]    > -)**///
#
%%..0+		.	.1 -!;-	
 	r   c                 2    [         R                  " U UU5      $ )z)Returns effective endpoint for given api.)r   _GetEffectiveApiEndpoint)r)   r+   client_classs      r   GetEffectiveApiEndpointr   h  s     
	/	/0;0<
> >r   c                 l    [         R                  " X5      n[        UR                  R                  S/S9$ )a  Returns the messages module for the API specified in the args.

Args:
  api_name: str, The API name (or the command surface name, if different).
  api_version: str, The version of the API.

Returns:
  Module containing the definitions of messages for the specified API.
	something)fromlist)r   	GetApiDef
__import__apitoolsmessages_full_modulepath)r)   r+   r,   s      r   GetMessagesModuler   p  s6     ##H:' 
G$$==)]
, ,r   c                 .    [         R                  " U 5      $ r#   )r   UniversifyAddress)addresss    r   r   r     s    		(	(	11r   r#   )F)FNF)'r   
__future__r   r   r   apitools.base.pyr   rP   googlecloudsdk.api_lib.utilr   r   r	   rC   r   r
   %googlecloudsdk.generated_clients.apisr   r$   r   r   r-   r'   r5   r@   rG   r`   rb   rH   rO   rm   rp   ru   rx   GRPCr|   r   r   r   r   r   r   r   <module>r      s     6 &  ' > 6 5 1 D * * : 
J 
P P4(0. 6B -0 )!$  4 (-29x>"  @ #,":":"?"?94 &&++#.b>,$2r   