o
    tBh*                    @   s  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 d dlmZ d dl	m
Z
mZ d dlmZmZmZmZmZmZmZmZmZmZmZmZ d dlZd dlmZ d dlmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ dd	l%m&Z& dd
l'm(Z( ddl)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 ddl0m2Z3 ddl0m4Z4 ddl5m6Z6 	 ej7dd dkrd dlm8Z8 nd dl9m8Z8 e:e;Z<G dd dZ=G dd dZ>G dd dZ?ededef dZ@ee@ge@f ZAG dd de?ZBdS )     N)Future)Queue)EventThread)AnyAsyncGeneratorCallableDict	GeneratorListOptionalTupleTypeVarUnioncastoverload)
fail_after)DocumentNodeExecutionResultGraphQLSchemaIntrospectionQuerybuild_ast_schemaget_introspection_queryparsevalidate   )GraphQLRequest)AsyncTransport)TransportClosedTransportQueryError)LocalSchemaTransport)	Transport)build_client_schema)parse_result)serialize_variable_values)str_first_element   )      )Literalc                   @   s
  e Zd ZdZ										dXdeeeef  dee deee	e
f  d	ed
ee deeeef  dedededefddZedd ZdefddZdefddZe				dYdddedeeeef  dee dee dee ded d eeef fd!d"Ze				dYdedeeeef  dee dee dee ded# d efd$d"Ze				dYdedeeeef  dee dee dee ded eeeef ef fd%d"Z					dZdedeeeef  dee dee dee ded eeeef ef fd&d"Zeddd'd(ee dee dee ded d eeeef  f
d)d*Zeddd'd(ee dee dee ded# d ee f
d+d*Zeddd'd(ee dee dee ded eeeeef  ee f f
d,d*Zdddd-d(ee dee dee ded eeeeef  ee f f
d.d*Ze				dYdddedeeeef  dee dee dee ded d eeef fd/d0Ze				dYdedeeeef  dee dee dee ded# d efd1d0Ze				dYdedeeeef  dee dee dee ded eeeef ef fd2d0Z					dZdedeeeef  dee dee dee ded eeeef ef fd3d0Ze				dYdddedeeeef  dee dee dee ded d eeef fd4d5Ze				dYdedeeeef  dee dee dee ded# d efd6d5Ze				dYdedeeeef  dee dee dee ded eeeef ef fd7d5Z					dZdedeeeef  dee dee dee ded eeeef ef fd8d5Zeddd'd(ee dee dee ded d eeeef  f
d9d:Zeddd'd(ee dee dee ded# d ee f
d;d:Zeddd'd(ee dee dee ded eeeeef  ee f f
d<d:Zdddd-d(ee dee dee ded eeeeef  ee f f
d=d:Ze				dYdddedeeeef  dee dee dee ded d e eeef df fd>d?Z!e				dYdedeeeef  dee dee dee ded# d e edf fd@d?Z!e				dYdedeeeef  dee dee dee ded ee eeef df e edf f fdAd?Z!					dZdedeeeef  dee dee dee ded ee eeef df e edf f fdBd?Z!e				dYdddedeeeef  dee dee dee ded d e"eeef ddf fdCdDZ#e				dYdedeeeef  dee dee dee ded# d e"eddf fdEdDZ#e				dYdedeeeef  dee dee dee ded ee"eeef ddf e"eddf f fdFdDZ#				d[dddedeeeef  dee dee dee ded ee"eeef ddf e"eddf f fdGdDZ#d\dHdIZ$dJdK Z%dLdM Z&dNdO Z'dPdQ Z(dRdS Z)dTdU Z*dVdW Z+dS )]Clienta   The Client class is the main entrypoint to execute GraphQL requests
    on a GQL transport.

    It can take sync or async transports as argument and can either execute
    and subscribe to requests itself with the
    :func:`execute <gql.client.Client.execute>` and
    :func:`subscribe <gql.client.Client.subscribe>` methods
    OR can be used to get a sync or async session depending on the
    transport type.

    To connect to an :ref:`async transport <async_transports>` and get an
    :class:`async session <gql.client.AsyncClientSession>`,
    use :code:`async with client as session:`

    To connect to a :ref:`sync transport <sync_transports>` and get a
    :class:`sync session <gql.client.SyncClientSession>`,
    use :code:`with client as session:`
    NF
   r   schemaintrospection	transportfetch_schema_from_transportintrospection_argsexecute_timeoutserialize_variablesparse_resultsbatch_interval	batch_maxc                 C   s   |r|rJ dt |}t|trt|}t|}|r.|r.|r#J dt|jdkr.J d|r6|s6t|}|| _|| _	|| _
|| _|du rHi n|| _|| _|| _|| _|	| _|
| _dS )a"  Initialize the client with the given parameters.

        :param schema: an optional GraphQL Schema for local validation
                See :ref:`schema_validation`
        :param transport: The provided :ref:`transport <Transports>`.
        :param fetch_schema_from_transport: Boolean to indicate that if we want to fetch
                the schema from the transport using an introspection query.
        :param introspection_args: arguments passed to the get_introspection_query
                method of graphql-core.
        :param execute_timeout: The maximum time in seconds for the execution of a
                request before a TimeoutError is raised. Only used for async transports.
                Passing None results in waiting forever for a response.
        :param serialize_variables: whether the variable values should be
            serialized. Used for custom scalars and/or enums. Default: False.
        :param parse_results: Whether gql will try to parse the serialized output
                sent by the backend. Can be used to unserialize custom scalars or enums.
        :param batch_interval: Time to wait in seconds for batching requests together.
                Batching is disabled (by default) if 0.
        :param batch_max: Maximum number of requests in a single batch.
        z9Cannot provide introspection and schema at the same time.z>Cannot fetch the schema from transport if is already provided.AppSyncWebsocketsTransportzfetch_schema_from_transport=True is not allowed for AppSyncWebsocketsTransport because only subscriptions are allowed on the realtime endpoint.N)r"   
isinstancestrr   r   type__name__r    r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   )selfr,   r-   r.   r/   r0   r1   r2   r3   r4   r5   type_def_ast r=   a/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/gql/client.py__init__Q   s<   "

zClient.__init__c                 C   s
   | j dkS )Nr   )r4   r;   r=   r=   r>   batching_enabled      
zClient.batching_enableddocumentc                 C   s*   | j sJ dt| j |}|r|d dS ):meta private:z@Cannot validate the document locally, you need to pass a schema.r   N)r,   r   )r;   rC   validation_errorsr=   r=   r>   r      s   zClient.validateexecution_resultc                 C   sH   |j rtdt|j  d|j |j|jdtt|j| _t| j| _	d S )NzError while fetching schema: zT
If you don't need the schema, you can try with: "fetch_schema_from_transport=False"errorsdata
extensions)
rH   r   r%   rI   rJ   r   r   r-   r"   r,   )r;   rF   r=   r=   r>    _build_schema_from_introspection   s   z'Client._build_schema_from_introspection.get_execution_resultvariable_valuesoperation_namer#   rM   returnc                K      d S Nr=   r;   rC   rN   rO   r2   r#   rM   kwargsr=   r=   r>   execute_sync      zClient.execute_syncTc                K   rQ   rR   r=   rS   r=   r=   r>   rU      rV   c                K   rQ   rR   r=   rS   r=   r=   r>   rU      rV   c           	   	   K   sF   | }|j |f|||||d|W  d   S 1 sw   Y  dS )rD   rN   rO   r2   r#   rM   Nexecute	r;   rC   rN   rO   r2   r#   rM   rT   sessionr=   r=   r>   rU      s   $r2   r#   requestsc                K   rQ   rR   r=   r;   r]   r2   r#   rM   rT   r=   r=   r>   execute_batch_sync     
zClient.execute_batch_syncc                K   rQ   rR   r=   r^   r=   r=   r>   r_     r`   c                K   rQ   rR   r=   r^   r=   r=   r>   r_     r`   r2   r#   rM   c                K   sB   | }|j |f|||d|W  d   S 1 sw   Y  dS )rD   ra   N)execute_batch)r;   r]   r2   r#   rM   rT   r[   r=   r=   r>   r_   &  s   
$c                      d S rR   r=   rS   r=   r=   r>   execute_async9     zClient.execute_asyncc                   rc   rR   r=   rS   r=   r=   r>   rd   G  re   c                   rc   rR   r=   rS   r=   r=   r>   rd   U  re   c           	   	      sb   | 4 I dH }|j |f|||||d|I dH W  d  I dH  S 1 I dH s*w   Y  dS rD   NrW   rX   rZ   r=   r=   r>   rd   c  s   
0c                K   rQ   rR   r=   rS   r=   r=   r>   rY   y  rV   zClient.executec                K   rQ   rR   r=   rS   r=   r=   r>   rY     rV   c                K   rQ   rR   r=   rS   r=   r=   r>   rY     rV   c           
   
   K   s   t | jtrVz!t  tjddd t }W d   n1 s!w   Y  W n ty9   t	 }t
| Y nw | rBJ d|| j|f|||||d|}	|	S | j|f|||||d|S )ab  Execute the provided document AST against the remote server using
        the transport provided during init.

        This function **WILL BLOCK** until the result is received from the server.

        Either the transport is sync and we execute the query synchronously directly
        OR the transport is async and we execute the query in the asyncio loop
        (blocking here until answer).

        This method will:

         - connect using the transport to get a session
         - execute the GraphQL request on the transport session
         - close the session and close the connection to the server

         If you have multiple requests to send, it is better to get your own session
         and execute the requests in your session.

         The extra arguments passed in the method will be passed to the transport
         execute method.
        ignoreThere is no current event loopmessageNzpCannot run client.execute(query) if an asyncio loop is running. Use 'await client.execute_async(query)' instead.rW   )r7   r.   r   warningscatch_warningsfilterwarningsasyncioget_event_loopRuntimeErrornew_event_loopset_event_loop
is_runningrun_until_completerd   rU   )
r;   rC   rN   rO   r2   r#   rM   rT   looprI   r=   r=   r>   rY     sR    


c                K   rQ   rR   r=   r^   r=   r=   r>   rb     r`   zClient.execute_batchc                K   rQ   rR   r=   r^   r=   r=   r>   rb     r`   c                K   rQ   rR   r=   r^   r=   r=   r>   rb     r`   c                K   s.   t | jtr
td| j|f|||d|S )af  Execute multiple GraphQL requests in a batch against the remote server using
        the transport provided during init.

        This function **WILL BLOCK** until the result is received from the server.

        Either the transport is sync and we execute the query synchronously directly
        OR the transport is async and we execute the query in the asyncio loop
        (blocking here until answer).

        This method will:

         - connect using the transport to get a session
         - execute the GraphQL requests on the transport session
         - close the session and close the connection to the server

         If you want to perform multiple executions, it is better to use
         the context manager to keep a session active.

         The extra arguments passed in the method will be passed to the transport
         execute method.
        z*Batching is not implemented for async yet.ra   )r7   r.   r   NotImplementedErrorr_   r^   r=   r=   r>   rb     s   c                K   rQ   rR   r=   rS   r=   r=   r>   subscribe_async=  rV   zClient.subscribe_asyncc                K   rQ   rR   r=   rS   r=   r=   r>   rw   K  rV   c                K   rQ   rR   r=   rS   r=   r=   r>   rw   Y     c              	   K  sx   | 4 I dH (}|j |f|||||d|}	|	2 z	3 dH W }
|
V  q6 W d  I dH  dS 1 I dH s5w   Y  dS rf   )	subscribe)r;   rC   rN   rO   r2   r#   rM   rT   r[   	generatorresultr=   r=   r>   rw   i  s"   
.c                K   rQ   rR   r=   rS   r=   r=   r>   ry     rV   zClient.subscribec                K   rQ   rR   r=   rS   r=   r=   r>   ry     rV   c                K   rQ   rR   r=   rS   r=   r=   r>   ry     rx   c             
   k   s   z!t   t jddd t }W d   n1 sw   Y  W n ty4   t }t| Y nw | j|f|||||d|}	|	 rLJ dz	 tj
|	 |d}
||
}|V  qN tyi   Y dS  tttfy   tj
|	 |d |
  ||   w )	z|Execute a GraphQL subscription with a python generator.

        We need an async transport for this functionality.
        rg   rh   ri   NrW   ztCannot run client.subscribe(query) if an asyncio loop is running. Use 'await client.subscribe_async(query)' instead.T)ru   )rk   rl   rm   rn   ro   rp   rq   rr   rw   rs   ensure_future	__anext__rt   StopAsyncIterationKeyboardInterrupt	ExceptionGeneratorExitaclosecancelshutdown_asyncgens)r;   rC   rN   rO   r2   r#   rM   rT   ru   async_generatorgenerator_taskr{   r=   r=   r>   ry     s^   


c                    s   t | jtsJ d|r tdd| i|| _| j I dH  n| j I dH  t| d| _z| jrA| j	sE| j
 I dH  W | jS W | jS W | jS  tyX   | j I dH   w )a`  Connect asynchronously with the underlying async transport to
        produce a session.

        That session will be a permanent auto-reconnecting session
        if :code:`reconnecting=True`.

        If you call this method, you should call the
        :meth:`close_async <gql.client.Client.close_async>` method
        for cleanup.

        :param reconnecting: if True, create a permanent reconnecting session
        :param \**kwargs: additional arguments for the
            :meth:`ReconnectingAsyncClientSession init method
            <gql.client.ReconnectingAsyncClientSession.__init__>`.
        zBOnly a transport of type AsyncTransport can be used asynchronouslyclientNr   r=   )r7   r.   r   ReconnectingAsyncClientSessionr[   start_connecting_taskconnectAsyncClientSessionr/   r,   fetch_schemar   close)r;   reconnectingrT   r=   r=   r>   connect_async  s.   		zClient.connect_asyncc                    s2   t | jtr| j I dH  | j I dH  dS )zBClose the async transport and stop the optional reconnecting task.N)r7   r[   r   stop_connecting_taskr.   r   r@   r=   r=   r>   close_async%  s   zClient.close_asyncc                    s   |   I d H S rR   )r   r@   r=   r=   r>   
__aenter__-  s   zClient.__aenter__c                    s   |   I d H  d S rR   )r   )r;   exc_typeexctbr=   r=   r>   	__aexit__0  s   zClient.__aexit__c                 C   s~   t | jtr
J dt| dst| d| _| j  z| jr*| js.| j	  W | jS W | jS W | jS  t
y>   | j   w )zConnect synchronously with the underlying sync transport to
        produce a session.

        If you call this method, you should call the
        :meth:`close_sync <gql.client.Client.close_sync>` method
        for cleanup.
        zSOnly a sync transport can be used. Use 'async with Client(...) as session:' insteadr[   r   )r7   r.   r   hasattrSyncClientSessionr[   r   r/   r,   r   r   r   r@   r=   r=   r>   connect_sync3  s$   	

		
zClient.connect_syncc                 C   s   | j   dS )zClose the sync session and the sync transport.

        If batching is enabled, this will block until the remaining queries in the
        batching queue have been processed.
        N)r[   r   r@   r=   r=   r>   
close_syncS  s   zClient.close_syncc                 C   s   |   S rR   )r   r@   r=   r=   r>   	__enter__[  s   zClient.__enter__c                 G   s   |    d S rR   )r   )r;   argsr=   r=   r>   __exit__^  s   zClient.__exit__)
NNNFNr+   FFr   r+   ....NNNNFNNNN)F),r:   
__module____qualname____doc__r   r   r8   r   r   r!   r   boolr	   intfloatr?   propertyrA   r   r   r   rK   r   r   r)   rU   r   r   r_   rd   rY   rb   r   rw   r
   ry   r   r   r   r   r   r   r   r   r=   r=   r=   r>   r*   =   s   	

R





	





	




	
K
+



	



"
"

L) r*   c                   @   sb  e Zd ZdZdefddZ				d1dedeee	e
f  dee	 d	ee d
ee defddZe				d2dddedeee	e
f  dee	 d	ee d
ee ded dee	e
f fddZe				d2dedeee	e
f  dee	 d	ee d
ee ded defddZe				d2dedeee	e
f  dee	 d	ee d
ee dedeee	e
f ef fddZ					d3dedeee	e
f  dee	 d	ee d
ee dedeee	e
f ef fddZdddddee d	ee d
ee dee dee f
ddZeddddee d	ee d
ee ded deee	e
f  f
ddZeddddee d	ee d
ee ded dee f
d dZeddddee d	ee d
ee dedeeee	e
f  ee f f
d!dZdddd"dee d	ee d
ee dedeeee	e
f  ee f f
d#dZd4d$d%Zd&edefd'd(Zd)d* Zd+d, Zd4d-d.Zed/d0 ZdS )5r   zAn instance of this class is created when using :code:`with` on the client.

    It contains the sync method execute to send queries
    on a sync transport using the same session.
    r   c                 C   
   || _ dS z;:param client: the :class:`client <gql.client.Client>` usedNr   r;   r   r=   r=   r>   r?   i  rB   zSyncClientSession.__init__NrC   rN   rO   r2   r#   rP   c           
      K   s   | j jr"| j | |dur"|s|du r"| j jr"t| j j|||d}| j jr7t|||d}| |}| }	n| j	j
|f||d|}	| j jr^|sR|du r^| j jr^t| j j||	j|d|	_|	S )a  Execute the provided document AST synchronously using
        the sync transport, returning an ExecutionResult object.

        :param document: GraphQL query as AST Node object.
        :param variable_values: Dictionary of input parameters.
        :param operation_name: Name of the operation that shall be executed.
        :param serialize_variables: whether the variable values should be
            serialized. Used for custom scalars and/or enums.
            By default use the serialize_variables argument of the client.
        :param parse_result: Whether gql will unserialize the result.
            By default use the parse_results argument of the client.

        The extra arguments are passed to the transport execute method.NrO   rN   rO   )r   r,   r   r2   r$   rA   r   _execute_futurer{   r.   rY   r3   parse_result_fnrI   )
r;   rC   rN   rO   r2   r#   rT   requestfuture_resultr{   r=   r=   r>   _executem  sH   

zSyncClientSession._execute.rL   rM   Fc                K   rQ   rR   r=   rS   r=   r=   r>   rY     rV   zSyncClientSession.executeTc                K   rQ   rR   r=   rS   r=   r=   r>   rY     rV   c                K   rQ   rR   r=   rS   r=   r=   r>   rY     rV   c           	      K   s^   | j |f||||d|}|jrtt|j|j|j|jd|jdus(J d|r,|S |jS )a  Execute the provided document AST synchronously using
        the sync transport.

        Raises a TransportQueryError if an error has been returned in
            the ExecutionResult.

        :param document: GraphQL query as AST Node object.
        :param variable_values: Dictionary of input parameters.
        :param operation_name: Name of the operation that shall be executed.
        :param serialize_variables: whether the variable values should be
            serialized. Used for custom scalars and/or enums.
            By default use the serialize_variables argument of the client.
        :param parse_result: Whether gql will unserialize the result.
            By default use the parse_results argument of the client.
        :param get_execution_result: return the full ExecutionResult instance instead of
            only the "data" field. Necessary if you want to get the "extensions" field.

        The extra arguments are passed to the transport execute method.rN   rO   r2   r#   rG   N<Transport returned an ExecutionResult without data or errorsr   rH   r   r%   rI   rJ   	r;   rC   rN   rO   r2   r#   rM   rT   r{   r=   r=   r>   rY     s.   
r2   r#   validate_documentr]   r   c          	         s    j jr%|r|D ]	} j |j q|s|du r% j jr% fdd|D } jj|fi |} j jrP|s=|du rP j jrP|D ]}t j j|j|j	|j
d|_	q?|S )a  Execute multiple GraphQL requests in a batch, using
        the sync transport, returning a list of ExecutionResult objects.

        :param requests: List of requests that will be executed.
        :param serialize_variables: whether the variable values should be
            serialized. Used for custom scalars and/or enums.
            By default use the serialize_variables argument of the client.
        :param parse_result: Whether gql will unserialize the result.
            By default use the parse_results argument of the client.
        :param validate_document: Whether we still need to validate the document.

        The extra arguments are passed to the transport execute method.Nc                    s(   g | ]}|j d ur| jjn|qS rR   )rN   r$   r   r,   ).0reqr@   r=   r>   
<listcomp>5  s    
z4SyncClientSession._execute_batch.<locals>.<listcomp>r   )r   r,   r   rC   r2   r.   rb   r3   r   rI   rO   )	r;   r]   r2   r#   r   rT   r   resultsr{   r=   r@   r>   _execute_batch  s(   

z SyncClientSession._execute_batchr\   c                K   rQ   rR   r=   r^   r=   r=   r>   rb   K  r`   zSyncClientSession.execute_batchc                K   rQ   rR   r=   r^   r=   r=   r>   rb   W  r`   c                K   rQ   rR   r=   r^   r=   r=   r>   rb   c  r`   ra   c                K   s~   | j |f||d|}|D ]}|jr!tt|j|j|j|jd|jdus*J dq|r/|S tttt	t
f  dd |D S )a  Execute multiple GraphQL requests in a batch, using
        the sync transport. This method sends the requests to the server all at once.

        Raises a TransportQueryError if an error has been returned in any
          ExecutionResult.

        :param requests: List of requests that will be executed.
        :param serialize_variables: whether the variable values should be
            serialized. Used for custom scalars and/or enums.
            By default use the serialize_variables argument of the client.
        :param parse_result: Whether gql will unserialize the result.
            By default use the parse_results argument of the client.
        :param get_execution_result: return the full ExecutionResult instance instead of
            only the "data" field. Necessary if you want to get the "extensions" field.

        The extra arguments are passed to the transport execute method.r\   rG   Nr   c                 S   s   g | ]}|j qS r=   )rI   )r   r{   r=   r=   r>   r     s    z3SyncClientSession.execute_batch.<locals>.<listcomp>)r   rH   r   r%   rI   rJ   r   r   r	   r8   r   )r;   r]   r2   r#   rM   rT   r   r{   r=   r=   r>   rb   o  s,    c              
   C   s2  d}|sg }| j  }|du rn|| | j  | jjd k r't| jj t	| jjd D ]}| j 
 r8 n| j  }|du rEd} n|| q/dd |D }dd |D }z| j|dddd}W n ty } z|D ]}	|	| qnW Y d}~qd}~ww t||D ]	\}
}	|	|
 q|r| j  dS )	zTmain loop of the thread used to wait for requests
        to execute them in a batchFNr   Tc                 S   s   g | ]\}}|qS r=   r=   )r   r   _r=   r=   r>   r         z1SyncClientSession._batch_loop.<locals>.<listcomp>c                 S   s   g | ]\}}|qS r=   r=   )r   r   futurer=   r=   r>   r     r   r   )batch_queuegetappendqsizer   r5   timesleepr4   rangeemptyr   r   set_exceptionzip
set_result_batch_thread_stopped_eventset)r;   	stop_looprequests_and_futuresrequest_and_futurer   r]   futuresr   r   r   r{   r=   r=   r>   _batch_loop  sH   




-zSyncClientSession._batch_loopr   c                 C   s:   t | ds	J d| jrJ dt }| j||f |S )zIf batching is enabled, this method will put a request in the batching queue
        instead of executing it directly so that the requests could be put in a batch.
        r   zBatching is not enabledz Batching thread has been stopped)r   _batch_thread_stop_requestedr   r   put)r;   r   r   r=   r=   r>   r     s
   z!SyncClientSession._execute_futurec                 C   sF   | j jrt | _d| _t | _t| jdd| _	| j	
  | j  dS )z]Connect the transport and initialize the batch threading loop if batching
        is enabled.FT)targetdaemonN)r   rA   r   r   r   r   r   r   r   _batch_threadstartr.   r   r@   r=   r=   r>   r     s   
zSyncClientSession.connectc                 C   s4   t | drd| _| jd | j  | j  dS )zClose the transport and cleanup the batching thread if batching is enabled.

        Will wait until all the remaining requests in the batch processing queue
        have been executed.
        r   TN)r   r   r   r   r   waitr.   r   r@   r=   r=   r>   r     s
   

zSyncClientSession.closec                 C   s2   t di | jj}| jt|}| j| dS zFetch the GraphQL schema explicitly using introspection.

        Don't use this function and instead set the fetch_schema_from_transport
        attribute to TrueNr=   r   r   r0   r.   rY   r   rK   r;   introspection_queryrF   r=   r=   r>   r     s   zSyncClientSession.fetch_schemac                 C      | j jS rR   r   r.   r@   r=   r=   r>   r.        zSyncClientSession.transportr   r   r   rP   N)r:   r   r   r   r*   r?   r   r   r	   r8   r   r   r   r   r   r)   rY   r   r   r   r   rb   r   r   r   r   r   r   r   r.   r=   r=   r=   r>   r   b  sn   
D



	
=
7

55


r   c                   @   s  e Zd ZdZdefddZ				d#dedeee	e
f  dee	 d	ee d
ee deedf fddZe				d$dddedeee	e
f  dee	 d	ee d
ee ded deee	e
f df fddZe				d$dedeee	e
f  dee	 d	ee d
ee ded deedf fddZe				d$dedeee	e
f  dee	 d	ee d
ee dedeeee	e
f df eedf f fddZ					d%dedeee	e
f  dee	 d	ee d
ee dedeeee	e
f df eedf f fddZ				d#dedeee	e
f  dee	 d	ee d
ee defddZe				d$dddedeee	e
f  dee	 d	ee d
ee ded dee	e
f fddZe				d$dedeee	e
f  dee	 d	ee d
ee ded defddZe				d$dedeee	e
f  dee	 d	ee d
ee dedeee	e
f ef fddZ					d%dedeee	e
f  dee	 d	ee d
ee dedeee	e
f ef fddZd&dd Zed!d" ZdS )'r   zAn instance of this class is created when using :code:`async with` on a
    :class:`client <gql.client.Client>`.

    It contains the async methods (execute, subscribe) to send queries
    on an async transport using the same session.
    r   c                 C   r   r   r   r   r=   r=   r>   r?     rB   zAsyncClientSession.__init__NrC   rN   rO   r2   r#   rP   c           	      K  s   | j jr#| j | |dur#|s|du r#| j jr#t| j j|||d}| jj|f||d|}|| _z1|2 z#3 dH W }| j jrV|sJ|du rV| j jrVt	| j j||j
|d|_
|V  q66 W | I dH  dS | I dH  w )a  Coroutine to subscribe asynchronously to the provided document AST
        asynchronously using the async transport,
        returning an async generator producing ExecutionResult objects.

        * Validate the query with the schema if provided.
        * Serialize the variable_values if requested.

        :param document: GraphQL query as AST Node object.
        :param variable_values: Dictionary of input parameters.
        :param operation_name: Name of the operation that shall be executed.
        :param serialize_variables: whether the variable values should be
            serialized. Used for custom scalars and/or enums.
            By default use the serialize_variables argument of the client.
        :param parse_result: Whether gql will unserialize the result.
            By default use the parse_results argument of the client.

        The extra arguments are passed to the transport subscribe method.Nr   r   )r   r,   r   r2   r$   r.   ry   
_generatorr3   r   rI   r   	r;   rC   rN   rO   r2   r#   rT   inner_generatorr{   r=   r=   r>   
_subscribe"  sH   
"zAsyncClientSession._subscribe.rL   rM   Fc                K   rQ   rR   r=   rS   r=   r=   r>   ry   m  rV   zAsyncClientSession.subscribeTc                K   rQ   rR   r=   rS   r=   r=   r>   ry   {  rV   c                K   rQ   rR   r=   rS   r=   r=   r>   ry     rx   c           
      K  s   | j |f||||d|}z4|2 z&3 dH W }	|	jr)tt|	j|	j|	j|	jd|	jdur8|r4|	V  q|	jV  q6 W | I dH  dS | I dH  w )a  Coroutine to subscribe asynchronously to the provided document AST
        asynchronously using the async transport.

        Raises a TransportQueryError if an error has been returned in
            the ExecutionResult.

        :param document: GraphQL query as AST Node object.
        :param variable_values: Dictionary of input parameters.
        :param operation_name: Name of the operation that shall be executed.
        :param serialize_variables: whether the variable values should be
            serialized. Used for custom scalars and/or enums.
            By default use the serialize_variables argument of the client.
        :param parse_result: Whether gql will unserialize the result.
            By default use the parse_results argument of the client.
        :param get_execution_result: yield the full ExecutionResult instance instead of
            only the "data" field. Necessary if you want to get the "extensions" field.

        The extra arguments are passed to the transport subscribe method.r   NrG   )r   rH   r   r%   rI   rJ   r   )
r;   rC   rN   rO   r2   r#   rM   rT   r   r{   r=   r=   r>   ry     s6   	
"c                    s   | j jr#| j | |dur#|s|du r#| j jr#t| j j|||d}t| j j | jj|f||d|I dH }W d   n1 sDw   Y  | j jrc|sW|du rc| j j	rct
| j j||j|d|_|S )af  Coroutine to execute the provided document AST asynchronously using
        the async transport, returning an ExecutionResult object.

        * Validate the query with the schema if provided.
        * Serialize the variable_values if requested.

        :param document: GraphQL query as AST Node object.
        :param variable_values: Dictionary of input parameters.
        :param operation_name: Name of the operation that shall be executed.
        :param serialize_variables: whether the variable values should be
            serialized. Used for custom scalars and/or enums.
            By default use the serialize_variables argument of the client.
        :param parse_result: Whether gql will unserialize the result.
            By default use the parse_results argument of the client.

        The extra arguments are passed to the transport execute method.Nr   r   )r   r,   r   r2   r$   r   r1   r.   rY   r3   r   rI   )r;   rC   rN   rO   r2   r#   rT   r{   r=   r=   r>   r     s>   	zAsyncClientSession._executec                   rc   rR   r=   rS   r=   r=   r>   rY     re   zAsyncClientSession.executec                   rc   rR   r=   rS   r=   r=   r>   rY   "  re   c                   rc   rR   r=   rS   r=   r=   r>   rY   0  re   c           	         sf   | j |f||||d|I dH }|jr#tt|j|j|j|jd|jdus,J d|r0|S |jS )a  Coroutine to execute the provided document AST asynchronously using
        the async transport.

        Raises a TransportQueryError if an error has been returned in
            the ExecutionResult.

        :param document: GraphQL query as AST Node object.
        :param variable_values: Dictionary of input parameters.
        :param operation_name: Name of the operation that shall be executed.
        :param serialize_variables: whether the variable values should be
            serialized. Used for custom scalars and/or enums.
            By default use the serialize_variables argument of the client.
        :param parse_result: Whether gql will unserialize the result.
            By default use the parse_results argument of the client.
        :param get_execution_result: return the full ExecutionResult instance instead of
            only the "data" field. Necessary if you want to get the "extensions" field.

        The extra arguments are passed to the transport execute method.r   NrG   r   r   r   r=   r=   r>   rY   >  s0   
c                    s:   t di | jj}| jt|I dH }| j| dS r   r   r   r=   r=   r>   r   w  s   zAsyncClientSession.fetch_schemac                 C   r   rR   r   r@   r=   r=   r>   r.     r   zAsyncClientSession.transportr   r   r   r   )r:   r   r   r   r*   r?   r   r   r	   r8   r   r   r   r   r   r   r)   ry   r   r   rY   r   r   r.   r=   r=   r=   r>   r     s   

K



	
?
?



	

9
r   
_CallableT.)boundc                       s4  e Zd ZdZ		ddedeeef deeef fddZdd	 Z	d
d Z
dd Z				ddedeeeef  dee dee dee def fddZ				ddedeeeef  dee dee dee defddZ				ddedeeeef  dee dee dee deedf f fddZ  ZS )r   a<  An instance of this class is created when using the
    :meth:`connect_async <gql.client.Client.connect_async>` method of the
    :class:`Client <gql.client.Client>` class with :code:`reconnecting=True`.

    It is used to provide a single session which will reconnect automatically if
    the connection fails.
    Tr   retry_connectretry_executec                 C   s   || _ d| _t | _t | _|du rtjtjt	dd| _
n|du r)dd | _
n	t|s/J || _
|du rDtjtjt	dd	d d
| _n|du rNdd | _n	t|sTJ || _| | j| _| 
| jj| _dS )a  
        :param client: the :class:`client <gql.client.Client>` used.
        :param retry_connect: Either a Boolean to activate/deactivate the retries
            for the connection to the transport OR a backoff decorator to
            provide specific retries parameters for the connections.
        :param retry_execute: Either a Boolean to activate/deactivate the retries
            for the execute method OR a backoff decorator to
            provide specific retries parameters for this method.
        NT<   )	max_valueFc                 S      | S rR   r=   er=   r=   r>   <lambda>      z9ReconnectingAsyncClientSession.__init__.<locals>.<lambda>   c                 S   s
   t | tS rR   )r7   r   r   r=   r=   r>   r     s   
 )	max_triesgiveupc                 S   r   rR   r=   r   r=   r=   r>   r     r   )r   _connect_taskrn   r   _reconnect_request_event_connected_eventbackoffon_exceptionexpor   r   callabler   _execute_once_execute_with_retriesr.   r   _connect_with_retries)r;   r   r   r   r=   r=   r>   r?     s6   



z'ReconnectingAsyncClientSession.__init__c                    sB   	 |   I dH  | j  | j  | j  | j I dH  q)zCoroutine used for the connection task.

        - try to connect to the transport with retries
        - send a connected event when the connection has been made
        - then wait for a reconnect request to try to connect again
        TN)r   r   r   clearr   r   r@   r=   r=   r>   _connection_loop  s   


z/ReconnectingAsyncClientSession._connection_loopc                    s:   | j rtd dS t|  | _ | j I dH  dS )zrStart the task responsible to restart the connection
        of the transport when requested by an event.
        zconnect task already started!N)r   logwarningrn   r|   r   r   r   r@   r=   r=   r>   r     s
   z4ReconnectingAsyncClientSession.start_connecting_taskc                    s$   | j dur| j   d| _ dS dS )zStop the connecting task.N)r   r   r@   r=   r=   r>   r     s
   


z3ReconnectingAsyncClientSession.stop_connecting_taskNrC   rN   rO   r2   r#   rP   c                    sH   zt  j|f||||d|I dH }W |S  ty#   | j   w )zSame Coroutine as parent method _execute but requesting a
        reconnection if we receive a TransportClosed exception.
        r   N)superr   r   r   r   )r;   rC   rN   rO   r2   r#   rT   answer	__class__r=   r>   r     s"   
z,ReconnectingAsyncClientSession._execute_oncec                    s$   | j |f||||d|I dH S )zSame Coroutine as parent, but with optional retries
        and requesting a reconnection if we receive a TransportClosed exception.
        r   N)r   )r;   rC   rN   rO   r2   r#   rT   r=   r=   r>   r     s   z'ReconnectingAsyncClientSession._executec           	        s   t  j|f||||d|}z'z|2 z	3 dH W }|V  q6 W n ty-   | j   w W | I dH  dS | I dH  w )zSame Async generator as parent method _subscribe but requesting a
        reconnection if we receive a TransportClosed exception.
        r   N)r  r   r   r   r   r   r   r  r=   r>   r   !  s*   	
"z)ReconnectingAsyncClientSession._subscribe)TTr   )r:   r   r   r   r*   r   r   
_Decoratorr?   r   r   r   r   r   r	   r8   r   r   r   r   r   r   __classcell__r=   r=   r  r>   r     s    


6	

r   )Crn   loggingsysr   rk   concurrent.futuresr   queuer   	threadingr   r   typingr   r   r   r	   r
   r   r   r   r   r   r   r   r   anyior   graphqlr   r   r   r   r   r   r   r   graphql_requestr   transport.async_transportr   transport.exceptionsr   r   transport.local_schemar    transport.transportr!   	utilitiesr"   r#   r   r$   utilsr%   version_infor)   typing_extensions	getLoggerr:   r   r*   r   r   r   r  r   r=   r=   r=   r>   <module>   sV    8(
      +   7  r