o
    tBh7A                     @   s`  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZm	Z	m
Z
 d dlmZmZmZ d dlmZmZ d dlmZ d dlmZmZmZ d dlmZ d dlmZ d	Zd
Zdedee fddZd#de defddZ!de	deeef fddZ"de	deeef fddZ#dedefddZ$de	dee fddZ%de	defddZ&de	defdd Z'd$d!d"Z(dS )%    N)ArgumentParser	NamespaceRawTextHelpFormatter)AnyDictOptional)GraphQLErrorprint_schema)URL)Client__version__gql)AsyncTransport)TransportQueryErrorz
Send GraphQL queries from the command line using http(s) or websockets.
If used interactively, write your query, then use Ctrl-D (EOF) to execute it.
a  
EXAMPLES
========

# Simple query using https
echo 'query { continent(code:"AF") { name } }' | gql-cli https://countries.trevorblades.com

# Simple query using websockets
echo 'query { continent(code:"AF") { name } }' | gql-cli wss://countries.trevorblades.com/graphql

# Query with variable
echo 'query getContinent($code:ID!) { continent(code:$code) { name } }' | gql-cli https://countries.trevorblades.com --variables code:AF

# Interactive usage (insert your query in the terminal, then press Ctrl-D to execute it)
gql-cli wss://countries.trevorblades.com/graphql --variables code:AF

# Execute query saved in a file
cat query.gql | gql-cli wss://countries.trevorblades.com/graphql

# Print the schema of the backend
gql-cli https://countries.trevorblades.com/graphql --print-schema

	value_strreturnc                 C   s@   zt | }W n ty   |  dkrY dS  w |dk rt|S )zConvert a string argument value into either an int or None.

    Raise a ValueError if the argument is negative or a string which is not "none"
    noneNr   )int
ValueErrorlower)r   	value_int r   ^/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/gql/cli.pypositive_int_or_none2   s   r   Fwith_examplesc                 C   s<  t t| rtndtd}|jddd |jdddd	d
 |jddddd
 |jdddt d | }|jdddddtjd |jdddddtj	d |jddddd |jd d!d"d#d$ |jd%dt
d&d'd( |jd)d*td+d,d- |jd.d/g d0d1d2d3 d4}|jd5|d6}| }|jd7d8d9d |jd:d;d<d |S )=aS  Provides an ArgumentParser for the gql-cli script.

    This function is also used by sphinx to generate the script documentation.

    :param with_examples: set to False by default so that the examples are not
                          present in the sphinx docs (they are put there with
                          a different layout)
    N)descriptionepilogformatter_classserverz?the server url starting with http://, https://, ws:// or wss://)helpz-Vz--variables*z*query variables in the form key:json_value)nargsr   z-Hz	--headersz"http headers in the form key:valuez	--versionversionv)actionr"   z-dz--debugz4print lots of debugging statements (loglevel==DEBUG)store_constloglevel)r   r$   destconstz-vz	--verbosez(show low level messages (loglevel==INFO)z-oz--operation-namezset the operation_name valueoperation_name)r   r'   z--print-schemaz/get the schema from instrospection and print it
store_truer	   )r   r$   r'   z--schema-downloada0  select the introspection query arguments to download the schema.
            Only useful if --print-schema is used.
            By default, it will:

             - request field descriptions
             - not request deprecated input fields

            Possible options:

             - descriptions:false             for a compact schema without comments
             - input_value_deprecation:true   to download deprecated input fields
             - specified_by_url:true
             - schema_description:true
             - directive_is_repeatable:trueschema_download)r!   r   r'   z--execute-timeoutz<set the execute_timeout argument of the Client (default: 10)
   execute_timeout)r   typedefaultr'   z--transportauto)r0   aiohttpphoenix
websocketsappsync_httpappsync_websocketszVselect the transport. 'auto' by default: aiohttp or websockets depending on url scheme	transport)r/   choicesr   r'   z
By default, for an AppSync backend, the IAM authentication is chosen.

If you want API key or JWT authentication, you can provide one of the
following arguments:zAWS AppSync options)r   z	--api-keyz%Provide an API key for authenticationapi_keyz--jwtz,Provide an JSON Web token for authenticationjwt)r   r   examplesr   add_argumentr   add_mutually_exclusive_grouploggingDEBUGINFOtextwrapdedentr   add_argument_group)r   parsergroupappsync_descriptionappsync_groupappsync_auth_groupr   r   r   
get_parserE   s   

	rH   argsc              	   C   sl   i }i }| j dur+| j D ]}z|dd\}}|||< W q ty*   td| w | j dur4||d< |S )zExtract extra arguments necessary for the transport
    from the parsed command line args

    Will create a headers dict by splitting the colon
    in the --headers arguments

    :param args: parsed command line arguments
    N:   zInvalid header: headers)rL   splitr   )rI   transport_argsrL   header
header_keyheader_valuer   r   r   get_transport_args   s   



rR   c                 C   s   i }| j dur| j |d< | jdurai }| jD ]F}z6|dd\}}zt|}W n  tjyH   ztd| d}W n
 tjyE   tw Y nw |||< W q ty\   td| w ||d< |S )a  Extract extra arguments necessary for the execute or subscribe
    methods from the parsed command line args

    Extract the operation_name

    Extract the variable_values from the --variables argument
    by splitting the first colon, then loads the json value,
    We try to add double quotes around the value if it does not work first
    in order to simplify the passing of simple string values
    (we allow --variables KEY:VALUE instead of KEY:"VALUE")

    :param args: parsed command line arguments
    Nr)   rJ   rK   "zInvalid variable: variable_values)r)   	variablesrM   jsonloadsJSONDecodeErrorr   )rI   execute_argsrU   varvariable_keyvariable_json_valuevariable_valuer   r   r   get_execute_args   s0   



r^   urlc                 C   s(   | j dv r	d}|S | j dv sJ d}|S )z8Detects which transport should be used depending on url.)wswssr3   )httphttpsr1   )scheme)r_   transport_namer   r   r   autodetect_transport  s   
rf   c                 C   s  t | j}|jdvrtdt| }| jdkrt|}n| j}|dkr3ddlm} |dd| ji|S |dkrGdd	l	m
} |dd| ji|S |d
krbddlm} |jdk|d< |dd| ji|S ddlm} |dv snJ |jdusuJ | jrddlm} ||j| jd}	n1| jrddlm}
 |
|j| jd}	nddlm} ddlm} z||jd}	W n
 |y   Y dS w |	|d< |dkrddlm} |dd| ji|S ddlm} z|dd| ji|W S  ty   Y dS w )zsInstantiate a transport from the parsed command line arguments

    :param args: parsed command line arguments
    )rb   rc   r`   ra   z3URL protocol should be one of: http, https, ws, wssr0   r1   r   )AIOHTTPTransportr_   r2   )!PhoenixChannelWebsocketsTransportr3   )WebsocketsTransportra   ssl)AppSyncAuthentication)r4   r5   N)AppSyncApiKeyAuthentication)hostr8   )AppSyncJWTAuthentication)rm   r9   )AppSyncIAMAuthentication)NoRegionError)rm   authr4   )AppSyncWebsocketsTransportr   )r
   r   rd   r   rR   r6   rf   gql.transport.aiohttprg   (gql.transport.phoenix_channel_websocketsrh   gql.transport.websocketsri   gql.transport.appsync_authrk   rm   r8   rl   r9   rn   ro   botocore.exceptionsrp    gql.transport.appsync_websocketsrr   	Exception)rI   r_   rN   re   rg   rh   ri   rk   rl   rq   rn   ro   rp   rr   r   r   r   get_transport*  sV   



rz   c              	   C   s   i }g d}| j durL| j D ]=}z,|dd\}}||vr%td| j  | }|dvr5td| j  |dk||< W q tyK   td| j  w |S )zDGet the introspection args depending on the schema_download argument)descriptionsspecified_by_urldirective_is_repeatableschema_descriptioninput_value_deprecationNrJ   rK   zInvalid schema_download: )truefalser   )r+   rM   r   r   )rI   introspection_argspossible_argsargarg_key	arg_valuer   r   r   get_introspection_args  s    

r   c           
         s$  | j durtj| j d zt| }|du rW dS t| }W n ty; } ztd| tjd W Y d}~dS d}~ww d}t	|| j
t| | jd4 I dH }| j
rht
|jj}t| |W  d  I dH  S 	 tj  }t|dkrwnzt|}W n ty } zt|tjd d}W Y d}~qhd}~ww zAz|j|fi |2 z3 dH W }	tt|	 q6 W n$ ty   Y n ty   |j|fi |I dH }	tt|	 Y nw W n ttfy } zt|tjd d}W Y d}~nd}~ww qiW d  I dH  |S 1 I dH sw   Y  |S )zMain entrypoint of the gql-cli script

    :param args: The parsed command line arguments
    :return: The script exit code (0 = ok, 1 = error)
    N)levelrK   zError: )filer   )r6   fetch_schema_from_transportr   r-   )r&   r=   basicConfigrz   r^   r   printsysstderrr   r	   r   r-   clientschemastdinreadstriplenr   r   	subscriberV   dumpsKeyboardInterruptNotImplementedErrorexecuter   )
rI   r6   rY   e	exit_codesession
schema_str	query_strqueryresultr   r   r   main  sx   
--r   c               	   C   s   t dd} |  }zOt }t| tjt||d}dD ]}tt|d}|du r+qz	|	||j
 W q ty=   Y qw d}z||}W |  n|  w t| W dS  tyb   Y dS w )zSynchronously invoke ``main`` with the parsed command line arguments.

    Formerly ``scripts/gql-cli``, now registered as an ``entry_point``
    T)r   )loop)SIGINTSIGTERMCTRL_C_EVENTCTRL_BREAK_EVENTNr   )rH   
parse_argsasyncionew_event_loopset_event_loopensure_futurer   getattrsignal_moduleadd_signal_handlercancelr   run_until_completecloser   exitr   )rC   rI   r   	main_tasksignal_namesignalr   r   r   r   gql_cli  s.   

r   )F)r   N))r   rV   r=   r   r   r   r@   argparser   r   r   typingr   r   r   graphqlr   r	   yarlr
   r   r   r   gql.transportr   gql.transport.exceptionsr   r   r:   strr   r   boolrH   rR   r^   rf   rz   r   r   r   r   r   r   r   <module>   s2      6Y$N