o
    tBh                     @   s   d dl 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
 d dlmZ d dlmZmZmZ ddlmZmZ ddlmZmZ dd	lmZmZ ed
Zzd dlZW n	 eyZ   Y nw G dd deZdS )    N)
SSLContext)AnyDictOptionalTupleUnioncast)urlparse)DocumentNodeExecutionResult	print_ast   )AppSyncAuthenticationAppSyncIAMAuthentication)TransportProtocolErrorTransportServerError)WebsocketsTransportWebsocketsTransportBasezgql.transport.appsyncc                       s2  e Zd ZU dZee ed< dddddddi fdedee ded d	ee	e
f d
edededeeeef  deeef ddf fddZdedeeee ee f fddZ		ddedeeeef  dee defddZejZ	 		ddedeeeef  dee defddZejZejZejZejZ  ZS )AppSyncWebsocketsTransportz:ref:`Async Transport <async_transports>` used to execute GraphQL subscription on
    AWS appsync realtime endpoint.

    This transport uses asyncio and the websockets library in order to send requests
    on a websocket connection.
    authNF
   urlsessionzbotocore.session.Sessionsslconnect_timeoutclose_timeoutack_timeoutkeep_alive_timeoutconnect_argsreturnc
              	      s`   |st t|j}
t|
|d}|| _| j|}t j|||||||	d tj	g| _
tj	| _dS )a  Initialize the transport with the given parameters.

        :param url: The GraphQL endpoint URL. Example:
            https://XXXXXXXXXXXXXXXXXXXXXXXXXX.appsync-api.REGION.amazonaws.com/graphql
        :param auth: Optional AWS authentication class which will provide the
                     necessary headers to be correctly authenticated. If this
                     argument is not provided, then we will try to authenticate
                     using IAM.
        :param ssl: ssl_context of the connection.
        :param connect_timeout: Timeout in seconds for the establishment
            of the websocket connection. If None is provided this will wait forever.
        :param close_timeout: Timeout in seconds for the close. If None is provided
            this will wait forever.
        :param ack_timeout: Timeout in seconds to wait for the connection_ack message
            from the server. If None is provided this will wait forever.
        :param keep_alive_timeout: Optional Timeout in seconds to receive
            a sign of liveness from the server.
        :param connect_args: Other parameters forwarded to websockets.connect
        )hostr   )r   r   r   r   r   r   N)strr	   netlocr   r   get_auth_urlsuper__init__r   APOLLO_SUBPROTOCOLsupported_subprotocolssubprotocol)selfr   r   r   r   r   r   r   r   r   r    	__class__ w/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/gql/transport/appsync_websockets.pyr%       s"    z#AppSyncWebsocketsTransport.__init__answerc                 C   s   d}z2t |}t|d}|dkrW dS |dkr+d|vr+|d}td|d	ttt| |W S  tyB   t	d
| w )ak  Parse the answer received from the server.

        Difference between apollo protocol and aws protocol:

        - aws protocol can return an error without an id
        - aws protocol will send start_ack messages

        Returns a list consisting of:
            - the answer_type:
              - 'connection_ack',
              - 'connection_error',
              - 'start_ack',
              - 'ka',
              - 'data',
              - 'error',
              - 'complete'
            - the answer id (Integer) if received or None
            - an execution Result if the answer_type is 'data' or None
         type	start_ack)r1   NNerroridpayloadzServer error: ''z(Server did not return a GraphQL result: )
jsonloadsr!   getr   r   _parse_answer_apollor   
ValueErrorr   )r)   r.   answer_typejson_answererror_payloadr,   r,   r-   _parse_answer\   s"   


z(AppSyncWebsocketsTransport._parse_answerdocumentvariable_valuesoperation_namec           	         s   | j }|  j d7  _ dt|i}|r||d< |r||d< tj|dd}d|i}t|d|d	}| jd us7J d
| j|i|d d< | tj|ddI d H  |S )Nr   query	variablesoperationName),:)
separatorsdatastart)r3   r0   r4   authorizationr4   
extensions)next_query_idr   r6   dumpsr!   r   get_headers_send)	r)   r?   r@   rA   query_idrH   serialized_datar4   messager,   r,   r-   _send_query   s0   
z&AppSyncWebsocketsTransport._send_queryc                    s
   t d)zThis method is not available.

        Only subscriptions are supported on the AWS realtime endpoint.

        :raise: AssertionErrorz}execute method is not allowed for AppSyncWebsocketsTransport because only subscriptions are allowed on the realtime endpoint.)AssertionError)r)   r?   r@   rA   r,   r,   r-   execute   s   z"AppSyncWebsocketsTransport.execute)NN)__name__
__module____qualname____doc__r   r   __annotations__r!   r   r   boolintfloatr   r   r%   r   r   r>   r
   rS   r   	subscriberU   r   _initialize_send_stop_message_stop_listener_send_init_message_and_wait_ack	_wait_ack__classcell__r,   r,   r*   r-   r      s   
 
	

<
3
,
r   )r6   loggingr   r   typingr   r   r   r   r   r   urllib.parser	   graphqlr
   r   r   appsync_authr   r   
exceptionsr   r   
websocketsr   r   	getLoggerlogbotocoreImportErrorr   r,   r,   r,   r-   <module>   s      
