
    &                        S r SSKrSSKrSSKrSSKJrJrJrJr  SSK	r	SSK
Jr  SSKJr  SSKJr  \R                  rSrSrS	\S
\4S jr\R(                  \SSSS4S\S\S\\   S\S\S\S
\4S jjrS\S
\4S jrS	\S\" SS\R0                  \\      4S\R0                  \\      4S\R0                  \\      4S\R0                  \   4S\R0                  \   4S\R0                  \   4S\R0                  \   4/5      S
\4S jr  S,S \S\\   S\" SS!\R0                  \\      4/5      S"\\   S#\\   S
\4S$ jjr S-S%\S&\S
\4S' jjrS(\\\4   S
\\\4   4S) jrS\\   S
\4S* jrS\\   S
\4S+ jrg).zAPI utils for the BQ CLI.    N)Dict
NamedTupleOptionalUnion)flags)	bq_consts)bq_errorz_https?://(www|(staging-www|test-bigquery).sandbox|bigquery-sq|bigquery).(google|googleapis).comzgoogleapis.comservicereturnc                 6   U [         R                  L a  gU [         R                  L a  gU [         R                  L a  gU [         R                  L a  gU [         R
                  L a  gU [         R                  L a  gU [         R                  L a  g[        SU  35      e)z4Returns the service name for the given service enum.bigquerybigqueryconnectionbigqueryreservationbigquerydatatransferanalyticshubbiglakezUnsupported service: )	ServiceBIGQUERYCONNECTIONSRESERVATIONSDTSANALYTICS_HUBBIGLAKEBQ_IAM
ValueError)r
   s    !platform/bq/utils/bq_api_utils.py_get_service_name_from_enumr      s       '%%%'&&& '++!'''''//!'.. 
,WI6
77    Fservice_enumuniverse_domainregionis_mtlsis_repis_lepc           	      x   [         R                  " SU UUU(       a  SOSU(       a  SOSU(       a  SOS5        [        U 5      nU(       a  U(       a  U(       a
  U SU SU 3$ U(       d  U(       a  U(       a
  U SU SU 3$ U(       a  U(       d  U SU 3$ U(       d  U(       a  U(       a
  U SU SU 3$ U SU 3$ )	z*Returns the TPC service endpoint hostname.zgBuilding a root URL for the %s service in the "%s" universe for region "%s" %s mTLS, %s REP, and %s LEPwithwithout.z
.rep.mtls.z.rep.z.mtls.-)logginginfor   )r   r    r!   r"   r#   r$   r
   s          r   "_get_tpc_service_endpoint_hostnamer,   ,   s     
,,*fYfIfI	 (5' FYaxz/):;;	VYaxu_$566VYf_-..	VXQwiq 1229Ao&	''r   urlc                 <    U R                  S5      (       d  U S-   $ U $ )N/)endswithr-   s    r   add_trailing_slash_if_missingr2   V   s     	c		9	*r   inputted_flagsInputtedFlagsAPIUNIVERSE_DOMAINLOCATIONUSE_LEPUSE_REPUSE_REGIONAL_ENDPOINTSMTLSc                    UR                   R                  UR                  R                  UR                  R                  /R	                  S5      nUS:  a  [
        R                  " S5      eUS:X  a1  UR                  R                  (       d  [
        R                  " S5      eUR                  R                  (       aJ  [        R                  " SUR                  R                  5        [        UR                  R                  5      $ US:X  a  UR                  R                  (       a  SnOUR                  R                  nUR                  R                  (       a  [        R                  " S5        SnOUR                   R                  nUR                  R                  (       a  UR                  R                  nO[        n[        U UUUR                   R                  UR                  R                  US	9n[        ["        R$                  R'                  ["        R$                  R)                  S
USSSS95      5      n[        R                  " SU5        U$ )a3  Takes BQ CLI flags to build a root URL to make requests to.

If the `api` flag is set, and is a http/https URL then it will be used
otherwise the result is built up from the different options for a TPC service
endpoint.

Args:
  service: The service that this request will be made to. Usually the API that
    is being hit.
  inputted_flags: The flags set, usually straight from bq_flags.

Returns:
  The root URL to be used for BQ requests. This is built from the service
  being targeted and a number of flags as arguments. It's intended to be used
  both for building the URL to request the discovery doc from, and to override
  the rootUrl and servicePath values of the discovery doc when they're
  incorrect. It always ends with a trailing slash.

Raises:
  BigqueryClientError: If the flags are used incorrectly.
T   zLOnly one of use_lep, use_rep or use_regional_endpoints can be used at a timezVA region is needed when the use_lep, use_rep or use_regional_endpoints flags are used.zCLooking for a root URL and an `api` value was found, using that: %sr   NzHBuilding a root URL and `use_regional_endpoints` is present, forcing LEP)r   r    r!   r"   r#   r$   https )schemenetlocpathqueryfragmentzFinal root URL built as: %s)r8   valuer9   r:   countr	   BigqueryClientErrorr7   r5   presentr*   r+   r2   r6   _GDU_DOMAINr,   r;   urllibparse
urlunsplitSplitResult)r
   r3   )number_of_flags_requesting_a_regional_apir!   r$   r    hostnameroot_urls           r   get_tpc_root_url_from_flagsrQ   \   s   L """"++11/ 
E$K	 , /2

&
&	  014%%++

&
&	    LLM   )););)A)ABB 014

!
!
'
'F$$**F **00LL	 F##))F ##))$44::O!O/%!!''##))( +ll
,,
"
"XBb2 # ( 
,,,h7	/r   discovery_urlBIGQUERY_DISCOVERY_API_KEY_FLAGkeylabelsc                 ~   U(       a"  SU 3U ;   a  [         R                  " SU5        U $ SU ;   a  [         R                  " S5        U $ U(       d-  UR                  R                  n[         R                  " SU5        U(       a:  SU ;   a  SnOSnX SU 3-  n U(       a  U SU 3-  n [         R                  " SU 5        U $ )	zAdds an API key to the URL.zkey=zHAPI key %s has already been added, presumably from --discovery_url_extrazfAn API key already exists in the URL, presumably from --discovery_url_extra, so not adding any new keyzrNo API key has been set, using value from the `bigquery_discovery_api_key` flag targeting the universe_domain (%s)?&z&labels=z#Discovery URL has been updated (%s))r*   r+   rS   rE   )rR   r    r3   rT   rU   	delimiters         r   add_api_key_to_discovery_urlrZ      s    $ 	tC5\]*LL	!
 }LL	< 	

8
8
>
>CLL	 		 	
mii{$se,,M&**mLL6F	r   rP   api_versionc                     [         R                  R                  U 5      n[         R                  R                  SU05      nUR	                  SUS9n[         R                  R                  U5      $ )z.Returns the discovery doc URL from a root URL.versionz/$discovery/rest)rB   rC   )rJ   rK   urlsplit	urlencode_replacerL   )rP   r[   partsrC   s       r   get_discovery_url_from_root_urlrb     sY     ,,


)%
,,
 
 )[!9
:%
..0.
>%		 	 	''r   discovery_documentc                     [        U [        5      (       a  [        R                  " U 5      $ [        U [        5      (       a%  [        R                  " U R                  S5      5      $ [        S[        U 5       35      e)zTakes a downloaded discovery document and parses it.

Args:
  discovery_document: The discovery doc to parse.

Returns:
  The parsed api doc.
zutf-8z%Unsupported discovery document type: )
isinstancestrjsonloadsbytesdecoder   type)rc   s    r   parse_discovery_docrl     si     "C((::())$e,,::(//899-d3E.F-GH	 r   c                 $    U (       d  gU [         :H  $ )z+Returns whether the universe domain is GDU.F)rI   )r    s    r   is_gdu_universern   ,  s    		K	''r   c                     U (       d  g[         U ;   =(       d+    [        R                  " [        5      R	                  U 5      SL$ )zReturns whether the url is GDU.FN)rI   recompileSTATIC_HTTP_ENDPOINT_RE_PREFIXmatchr1   s    r   
is_gdu_urlrt   3  s7    			 
jj/066s;4Gr   )NN)v2) __doc__rg   r*   rp   typingr   r   r   r   rJ   abslr   utilsr   r	   r   rr   rI   rf   r   r   boolr,   r2   
FlagHolderrQ   rZ   rb   ri   rl   rn   rt    r   r   <module>r}      s~      	 4 4    


 
 8 8S 8* $,,& '('('( SM'( 	'(
 '( '( 	'(Ts s xxE$$Xc]34 0 0# ?@))(3-89((./((./%u'7'7'=>U%%d+,	
x 	xN  22c]2  2  #/	
2 
#2 SM2 	2l '+(( #((c5j)	#s(^((Xc] (t (HSM d r   