o
    êi                     @   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Zd dlZd dl	m
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 d dlmZmZ d dlmZ ddlmZmZmZmZmZ ddlmZmZ dd	l m!Z! ej"d
 Z#e$dZ%e$dZ&ej$dddZ'ej$dddZ(dZ)dZ*dZ+dZ,G dd de-Z.G dd de-Z/G dd dZ0G dd dZ1de2e3 dB de4e3ej5f fddZ6G dd  d ej7e' Z8ej9	d0e)e*e+e,de0d!d"e3d#ej:dB d$e;d%e;d&e<dB d'e<dB de2e3 dB d(e=e' d)ej5dej>e'ddf fd*d+Z?G d,d- d-ej7e( Z@ejA	d0e)e*e+e,de1d!d"e3d#ejBdB d$e;d%e;d&e<dB d'e<dB de2e3 dB d(e=e( d)ej5dejCe(df fd.d/ZDdS )1    N)TracebackType)MemoryObjectReceiveStreamMemoryObjectSendStream)AsyncNetworkStreamNetworkStream)CloseReason   )HTTPXWSExceptionWebSocketDisconnectWebSocketInvalidTypeReceivedWebSocketNetworkErrorWebSocketUpgradeError)AsyncPingManagerPingManager)ASGIWebSocketAsyncNetworkStreamtextbinaryTaskFunction
TaskResultSyncSessionWebSocketSession)boundAsyncSessionAsyncWebSocketSessioni   i   g      4@c                   @      e Zd ZdS )ShouldCloseN__name__
__module____qualname__ r!   r!   A/home/ubuntu/.local/lib/python3.10/site-packages/httpx_ws/_api.pyr   *       r   c                   @   r   )EndOfStreamNr   r!   r!   r!   r"   r$   .   r#   r$   c                   @   s  e Zd ZU dZedB ed< ejdB ed< ee	e
edddededed	edB d
edB dejdB ddfddZdeejjdf fddZd@ddZdd ZdAdedejfddZdejjddfddZdeddfddZdeddfdd ZdBde j!d"e"ddfd#d$Z#dCd%edB dejjfd&d'Z$dCd%edB defd(d)Z%dCd%edB defd*d+Z&	!dDd%edB d"e"de j!fd,d-Z'dEd/ed0edB fd1d2Z(d3eddfd4d5Z)	dCd6ed7edB ddfd8d9Z*d:e j+d;e,f de,fd<d=Z-d3edefd>d?Z.dS )Fr   a+  
    Sync context manager representing an opened WebSocket session.

    Attributes:
        subprotocol (typing.Optional[str]):
            Optional protocol that has been accepted by the server.
        response (typing.Optional[httpx.Response]):
            The webSocket handshake response.
    Nsubprotocolresponsemax_message_size_bytes
queue_sizekeepalive_ping_interval_secondskeepalive_ping_timeout_secondsr&   streamr(   r)   r*   r+   returnc                C   s   || _ tjtjj| _|| _| jd ur| jjd| _	nd | _	t
|| _t | _t | _t | _d | _d | _|| _|| _|| _|| _d S Nsec-websocket-protocol)r,   wsproto
connection
ConnectionConnectionTypeCLIENTr&   headersgetr%   queueQueue_eventsr   _ping_manager	threadingEvent_should_closeLock_write_lock_should_close_task	_executor_max_message_size_bytes_queue_size _keepalive_ping_interval_seconds_keepalive_ping_timeout_secondsselfr,   r(   r)   r*   r+   r&   r!   r!   r"   __init__@   s"   



zWebSocketSession.__init__zconcurrent.futures.Future[bool]c                 C   sB   | j d u rtj | _| j| jj| _ | jd usJ | j| j fS N)r@   
concurrentfuturesThreadPoolExecutorrA   submitr=   waitrG   r!   r!   r"   _get_executor_should_close_taskc   s
   
z0WebSocketSession._get_executor_should_close_taskc                 C   sX   t j| j| jfd| _| j  d | _| jd ur*t j| j| j| j	fd| _| j  | S )N)targetargs)
r;   Thread_background_receiverB   _background_receive_taskstart_background_keepalive_ping_taskrD   _background_keepalive_pingrE   rO   r!   r!   r"   	__enter__n   s   



zWebSocketSession.__enter__c                 C   s.   |    | j  | jd ur| j  d S d S rI   )closerU   joinrW   rG   exc_typeexctbr!   r!   r"   __exit__   s
   

zWebSocketSession.__exit__    payloadc                 C   s*   | j |\}}tj|}| | |S )a?  
        Send a Ping message.

        Args:
            payload:
                Payload to attach to the Ping event.
                Internally, it's used to track this specific event.
                If left empty, a random one will be generated.

        Returns:
            An event that can be used to wait for the corresponding Pong response.

        Examples:
            Send a Ping and wait for the Pong

                pong_callback = ws.ping()
                # Will block until the corresponding Pong is received.
                pong_callback.wait()
        r:   creater0   eventsPingsendrG   rb   ping_idcallbackeventr!   r!   r"   ping   s   
zWebSocketSession.pingrk   c              
   C   sz   z$| j |}| j | j| W d   W dS 1 sw   Y  W dS  tjy< } z| tj	d t
 |d}~ww )a  
        Send an Event message.

        Mainly useful to send events that are not supported by the library.
        Most of the time, [ping()][httpx_ws.WebSocketSession.ping],
        [send_text()][httpx_ws.WebSocketSession.send_text],
        [send_bytes()][httpx_ws.WebSocketSession.send_bytes]
        and [send_json()][httpx_ws.WebSocketSession.send_json] are preferred.

        Args:
            event: The event to send.

        Raises:
            WebSocketNetworkError: A network error occured.

        Examples:
            Send an event.

                event = wsproto.events.Message(b"Hello!")
                ws.send(event)
        NStream write errorr1   rg   r?   r,   writehttpcore
WriteErrorrZ   r   INTERNAL_ERRORr   rG   rk   dataer!   r!   r"   rg      s   &zWebSocketSession.sendrt   c                 C      t jj|d}| | dS )a  
        Send a text message.

        Args:
            data: The text to send.

        Raises:
            WebSocketNetworkError: A network error occured.

        Examples:
            Send a text message.

                ws.send_text("Hello!")
        rt   Nr0   re   TextMessagerg   rG   rt   rk   r!   r!   r"   	send_text      zWebSocketSession.send_textc                 C   rv   )a  
        Send a bytes message.

        Args:
            data: The data to send.

        Raises:
            WebSocketNetworkError: A network error occured.

        Examples:
            Send a bytes message.

                ws.send_bytes(b"Hello!")
        rw   Nr0   re   BytesMessagerg   rz   r!   r!   r"   
send_bytes   r|   zWebSocketSession.send_bytesr   modec                 C   s@   |dv sJ t |}|dkr| | dS | |d dS )a  
        Send JSON data.

        Args:
            data:
                The data to send. Must be serializable by [json.dumps][json.dumps].
            mode:
                The sending mode. Should either be `'text'` or `'bytes'`.

        Raises:
            WebSocketNetworkError: A network error occured.

        Examples:
            Send JSON data.

                data = {"message": "Hello!"}
                ws.send_json(data)
        r   r   utf-8Njsondumpsr{   r   encoderG   rt   r   serialized_datar!   r!   r"   	send_json   s
   
zWebSocketSession.send_jsontimeoutc                 C   s>   | j jd|d}t|tr|t|tjjrt|j|j	|S )aE  
        Receive an event from the server.

        Mainly useful to receive raw [wsproto.events.Event][wsproto.events.Event].
        Most of the time, [receive_text()][httpx_ws.WebSocketSession.receive_text],
        [receive_bytes()][httpx_ws.WebSocketSession.receive_bytes],
        and [receive_json()][httpx_ws.WebSocketSession.receive_json] are preferred.

        Args:
            timeout:
                Number of seconds to wait for an event.
                If `None`, will block until an event is available.

        Returns:
            A raw [wsproto.events.Event][wsproto.events.Event].

        Raises:
            queue.Empty: No event was received before the timeout delay.
            WebSocketDisconnect: The server closed the websocket.
            WebSocketNetworkError: A network error occured.

        Examples:
            Wait for an event until one is available.

                try:
                    event = ws.receive()
                except WebSocketDisconnect:
                    print("Connection closed")

            Wait for an event for 2 seconds.

                try:
                    event = ws.receive(timeout=2.)
                except queue.Empty:
                    print("No event received.")
                except WebSocketDisconnect:
                    print("Connection closed")
        T)blockr   )
r9   r6   
isinstancer	   r0   re   CloseConnectionr
   codereasonrG   r   rk   r!   r!   r"   receive   s   '
zWebSocketSession.receivec                 C   &   |  |}t|tjjr|jS t|)a&  
        Receive text from the server.

        Args:
            timeout:
                Number of seconds to wait for an event.
                If `None`, will block until an event is available.

        Returns:
            Text data.

        Raises:
            queue.Empty: No event was received before the timeout delay.
            WebSocketDisconnect: The server closed the websocket.
            WebSocketNetworkError: A network error occured.
            WebSocketInvalidTypeReceived: The received event was not a text message.

        Examples:
            Wait for text until available.

                try:
                    text = ws.receive_text()
                except WebSocketDisconnect:
                    print("Connection closed")

            Wait for text for 2 seconds.

                try:
                    event = ws.receive_text(timeout=2.)
                except queue.Empty:
                    print("No text received.")
                except WebSocketDisconnect:
                    print("Connection closed")
        r   r   r0   re   ry   rt   r   r   r!   r!   r"   receive_text*     
#zWebSocketSession.receive_textc                 C   r   )a,  
        Receive bytes from the server.

        Args:
            timeout:
                Number of seconds to wait for an event.
                If `None`, will block until an event is available.

        Returns:
            Bytes data.

        Raises:
            queue.Empty: No event was received before the timeout delay.
            WebSocketDisconnect: The server closed the websocket.
            WebSocketNetworkError: A network error occured.
            WebSocketInvalidTypeReceived: The received event was not a bytes message.

        Examples:
            Wait for bytes until available.

                try:
                    data = ws.receive_bytes()
                except WebSocketDisconnect:
                    print("Connection closed")

            Wait for bytes for 2 seconds.

                try:
                    data = ws.receive_bytes(timeout=2.)
                except queue.Empty:
                    print("No data received.")
                except WebSocketDisconnect:
                    print("Connection closed")
        r   r   r0   re   r~   rt   r   r   r!   r!   r"   receive_bytesR  r   zWebSocketSession.receive_bytesc                 C   s<   |dv sJ |dkr|  |}n	|dkr| |}t|S )a  
        Receive JSON data from the server.

        The received data should be parseable by [json.loads][json.loads].

        Args:
            timeout:
                Number of seconds to wait for an event.
                If `None`, will block until an event is available.
            mode:
                Receive mode. Should either be `'text'` or `'bytes'`.

        Returns:
            Parsed JSON data.

        Raises:
            queue.Empty: No event was received before the timeout delay.
            WebSocketDisconnect: The server closed the websocket.
            WebSocketNetworkError: A network error occured.
            WebSocketInvalidTypeReceived: The received event
                didn't correspond to the specified mode.

        Examples:
            Wait for data until available.

                try:
                    data = ws.receive_json()
                except WebSocketDisconnect:
                    print("Connection closed")

            Wait for data for 2 seconds.

                try:
                    data = ws.receive_json(timeout=2.)
                except queue.Empty:
                    print("No data received.")
                except WebSocketDisconnect:
                    print("Connection closed")
        r   r   r   r   r   r   loadsrG   r   r   rt   r!   r!   r"   receive_jsonz  s   *

zWebSocketSession.receive_json  r   r   c                 C   s   | j   | jdur| jd | jjtjjjtjjj	hvrQtj
||}| j|}z| j | j| W d   n1 s@w   Y  W n
 tjyP   Y nw | j  dS )a  
        Close the WebSocket session.

        Internally, it'll send the
        [CloseConnection][wsproto.events.CloseConnection] event.

        *This method is automatically called when exiting the context manager.*

        Args:
            code:
                The integer close code to indicate why the connection has closed.
            reason:
                Additional reasoning for why the connection has closed.

        Examples:
            Close the WebSocket session.

                ws.close()
        NF)r=   setrA   shutdownr1   stater0   ConnectionStateLOCAL_CLOSINGCLOSEDre   r   rg   r?   r,   ro   rp   rq   rZ   rG   r   r   rk   rt   r!   r!   r"   rZ     s$   

zWebSocketSession.close	max_bytesc              
   C   s  d}z| j  s| | j|}| j| | j D ]}t|tjj	rE| j
| }| j | j| W d   n1 s?w   Y  qt|tjjrT| j|j qt|tjjr`| j   t|tjjr|jsx|du rr|j}n$||j7 }n|du r| j| nt|}|||j }d}| j| q| j| q| j  rW dS W dS  tjtjtfy   | t j!d | jt"  Y dS  t#y   Y dS w )ao  
        Background thread listening for data from the server.

        Internally, it'll:

        * Answer to Ping events.
        * Acknowledge Pong events.
        * Put other events in the [_events][_events]
        queue that'll eventually be consumed by the user.

        Args:
            max_bytes: The maximum chunk size to read at each iteration.
        NStream error)$r=   is_set_wait_until_closed_read_streamr1   receive_datare   r   r0   rf   rg   r&   r?   r,   ro   Pongr:   ackrb   r   r   Messagemessage_finishedrt   r9   puttyperp   	ReadErrorrq   r$   rZ   r   rr   r   r   rG   r   partial_message_bufferrt   rk   
event_typefull_message_eventr!   r!   r"   rT     sN   

"z$WebSocketSession._background_receiveinterval_secondstimeout_secondsc                 C   s   z<| j  s:| | j j|}|rt |  }|d ur2| |j|}|s2| tjd | j	
t  | j  rW d S W d S  tyF   Y d S w NzKeepalive ping timeout)r=   r   r   rN   r   rl   rZ   r   rr   r9   r   r   )rG   r   r   should_closepong_callbackacknowledgedr!   r!   r"   rX     s&   
z+WebSocketSession._background_keepalive_pingcallable.c              
   O   s   z|   \}}|j|g|R i |}W n ty$ } zt |d }~ww tjj||ftjjd\}}	||v r:t ||v s@J | }
|
S )N)return_when)	rP   rM   RuntimeErrorr   rJ   rK   rN   FIRST_COMPLETEDresult)rG   r   rR   kwargsexecutorshould_close_task	todo_taskru   done_r   r!   r!   r"   r     s    
z#WebSocketSession._wait_until_closedc                 C   s   | j |}|dkrt |S Nra   r,   readr$   rG   r   rt   r!   r!   r"   r   0  s   zWebSocketSession._read_stream)r-   r   ra   r   rI   Nr   r   N)/r   r   r    __doc__str__annotations__httpxResponseDEFAULT_MAX_MESSAGE_SIZE_BYTESDEFAULT_QUEUE_SIZE'DEFAULT_KEEPALIVE_PING_INTERVAL_SECONDS&DEFAULT_KEEPALIVE_PING_TIMEOUT_SECONDSr   intfloatrH   tuplerJ   rK   rL   rP   rY   r`   bytesr;   r<   rl   r0   re   rg   r{   r   typingAnyJSONModer   r   r   r   r   rZ   rT   rX   Callabler   r   r   r!   r!   r!   r"   r   2   s   
 


#


.()
2$9

c                   @   s  e Zd ZU dZedB ed< ejdB ed< ee	j
jeB  ed< ee	j
jeB  ed< eeeeddded	ed
ededB dedB dejdB ddfddZd>ddZdee dB dedB dedB ddfddZd?dedejfddZde	j
jddfddZdeddfdd Zdeddfd!d"Z d@de!j"d$e#ddfd%d&Z$dAd'edB de	j
jfd(d)Z%dAd'edB defd*d+Z&dAd'edB defd,d-Z'	#dBd'edB d$e#de!j"fd.d/Z(dCd1ed2edB fd3d4Z)d5eddfd6d7Z*	dAd8ed9edB ddfd:d;Z+d5edefd<d=Z,dS )Dr   a,  
    Async context manager representing an opened WebSocket session.

    Attributes:
        subprotocol (typing.Optional[str]):
            Optional protocol that has been accepted by the server.
        response (typing.Optional[httpx.Response]):
            The webSocket handshake response.
    Nr%   r&   _send_event_receive_eventr'   r,   r(   r)   r*   r+   r-   c                C   s   || _ tjtjj| _|| _| jd ur| jjd| _	nd | _	t
 | _t | _t | _|| _|| _t|trAd | _d | _d S || _|| _d S r.   )r,   r0   r1   r2   r3   r4   r&   r5   r6   r%   r   r:   anyior<   r=   r>   r?   rB   rC   r   r   rD   rE   rF   r!   r!   r"   rH   G  s    





zAsyncWebSocketSession.__init__c              	      s   t  4 I d H `}tjtjjtB   \| _| _	|
| j |
| j	 t | _|| jI d H  | j| j| j | jd urK| j| j| j| j || jjj || j | | _W d   I d H  | S 1 I d H sow   Y  | S rI   )
contextlibAsyncExitStackr   create_memory_object_streamr0   re   r<   r	   r   r   enter_contextcreate_task_group_background_task_groupenter_async_context
start_soonrT   rB   rD   rX   rE   rj   cancel_scopecancelpush_async_callbackrZ   pop_all_exit_stack)rG   
exit_stackr!   r!   r"   
__aenter__j  s2   


z AsyncWebSocketSession.__aenter__r]   r^   r_   c                    s   | j  I d H  d S rI   )r   acloser\   r!   r!   r"   	__aexit__  s   zAsyncWebSocketSession.__aexit__ra   rb   c                    s2   | j |\}}tj|}| |I dH  |S )aK  
        Send a Ping message.

        Args:
            payload:
                Payload to attach to the Ping event.
                Internally, it's used to track this specific event.
                If left empty, a random one will be generated.

        Returns:
            An event that can be used to wait for the corresponding Pong response.

        Examples:
            Send a Ping and wait for the Pong

                pong_callback = await ws.ping()
                # Will block until the corresponding Pong is received.
                await pong_callback.wait()
        Nrc   rh   r!   r!   r"   rl     s
   zAsyncWebSocketSession.pingrk   c              
      s   z1| j |}| j4 I dH  | j|I dH  W d  I dH  W dS 1 I dH s+w   Y  W dS  tjyM } z| tj	dI dH  t
 |d}~ww )a  
        Send an Event message.

        Mainly useful to send events that are not supported by the library.
        Most of the time, [ping()][httpx_ws.AsyncWebSocketSession.ping],
        [send_text()][httpx_ws.AsyncWebSocketSession.send_text],
        [send_bytes()][httpx_ws.AsyncWebSocketSession.send_bytes]
        and [send_json()][httpx_ws.AsyncWebSocketSession.send_json] are preferred.

        Args:
            event: The event to send.

        Raises:
            WebSocketNetworkError: A network error occured.

        Examples:
            Send an event.

                event = await wsproto.events.Message(b"Hello!")
                ws.send(event)
        Nrm   rn   rs   r!   r!   r"   rg     s   2zAsyncWebSocketSession.sendrt   c                    $   t jj|d}| |I dH  dS )a  
        Send a text message.

        Args:
            data: The text to send.

        Raises:
            WebSocketNetworkError: A network error occured.

        Examples:
            Send a text message.

                await ws.send_text("Hello!")
        rw   Nrx   rz   r!   r!   r"   r{        zAsyncWebSocketSession.send_textc                    r   )a  
        Send a bytes message.

        Args:
            data: The data to send.

        Raises:
            WebSocketNetworkError: A network error occured.

        Examples:
            Send a bytes message.

                await ws.send_bytes(b"Hello!")
        rw   Nr}   rz   r!   r!   r"   r     r   z AsyncWebSocketSession.send_bytesr   r   c                    sN   |dv sJ t |}|dkr| |I dH  dS | |dI dH  dS )a  
        Send JSON data.

        Args:
            data:
                The data to send. Must be serializable by [json.dumps][json.dumps].
            mode:
                The sending mode. Should either be `'text'` or `'bytes'`.

        Raises:
            WebSocketNetworkError: A network error occured.

        Examples:
            Send JSON data.

                data = {"message": "Hello!"}
                await ws.send_json(data)
        r   r   Nr   r   r   r!   r!   r"   r     s   
zAsyncWebSocketSession.send_jsonr   c                    sj   t | | j I dH }W d   n1 sw   Y  t|tr%|t|tjjr3t	|j
|j|S )ab  
        Receive an event from the server.

        Mainly useful to receive raw [wsproto.events.Event][wsproto.events.Event].
        Most of the time, [receive_text()][httpx_ws.AsyncWebSocketSession.receive_text],
        [receive_bytes()][httpx_ws.AsyncWebSocketSession.receive_bytes],
        and [receive_json()][httpx_ws.AsyncWebSocketSession.receive_json] are preferred.

        Args:
            timeout:
                Number of seconds to wait for an event.
                If `None`, will block until an event is available.

        Returns:
            A raw [wsproto.events.Event][wsproto.events.Event].

        Raises:
            TimeoutError: No event was received before the timeout delay.
            WebSocketDisconnect: The server closed the websocket.
            WebSocketNetworkError: A network error occured.

        Examples:
            Wait for an event until one is available.

                try:
                    event = await ws.receive()
                except WebSocketDisconnect:
                    print("Connection closed")

            Wait for an event for 2 seconds.

                try:
                    event = await ws.receive(timeout=2.)
                except TimeoutError:
                    print("No event received.")
                except WebSocketDisconnect:
                    print("Connection closed")
        N)r   
fail_afterr   r   r   r	   r0   re   r   r
   r   r   r   r!   r!   r"   r     s   '
zAsyncWebSocketSession.receivec                    .   |  |I dH }t|tjjr|jS t|)a4  
        Receive text from the server.

        Args:
            timeout:
                Number of seconds to wait for an event.
                If `None`, will block until an event is available.

        Returns:
            Text data.

        Raises:
            TimeoutError: No event was received before the timeout delay.
            WebSocketDisconnect: The server closed the websocket.
            WebSocketNetworkError: A network error occured.
            WebSocketInvalidTypeReceived: The received event was not a text message.

        Examples:
            Wait for text until available.

                try:
                    text = await ws.receive_text()
                except WebSocketDisconnect:
                    print("Connection closed")

            Wait for text for 2 seconds.

                try:
                    event = await ws.receive_text(timeout=2.)
                except TimeoutError:
                    print("No text received.")
                except WebSocketDisconnect:
                    print("Connection closed")
        Nr   r   r!   r!   r"   r   1  
   #z"AsyncWebSocketSession.receive_textc                    r   )a:  
        Receive bytes from the server.

        Args:
            timeout:
                Number of seconds to wait for an event.
                If `None`, will block until an event is available.

        Returns:
            Bytes data.

        Raises:
            TimeoutError: No event was received before the timeout delay.
            WebSocketDisconnect: The server closed the websocket.
            WebSocketNetworkError: A network error occured.
            WebSocketInvalidTypeReceived: The received event was not a bytes message.

        Examples:
            Wait for bytes until available.

                try:
                    data = await ws.receive_bytes()
                except WebSocketDisconnect:
                    print("Connection closed")

            Wait for bytes for 2 seconds.

                try:
                    data = await ws.receive_bytes(timeout=2.)
                except TimeoutError:
                    print("No data received.")
                except WebSocketDisconnect:
                    print("Connection closed")
        Nr   r   r!   r!   r"   r   Y  r   z#AsyncWebSocketSession.receive_bytesc                    sJ   |dv sJ |dkr|  |I dH }n|dkr | |I dH }t|S )a  
        Receive JSON data from the server.

        The received data should be parseable by [json.loads][json.loads].

        Args:
            timeout:
                Number of seconds to wait for an event.
                If `None`, will block until an event is available.
            mode:
                Receive mode. Should either be `'text'` or `'bytes'`.

        Returns:
            Parsed JSON data.

        Raises:
            TimeoutError: No event was received before the timeout delay.
            WebSocketDisconnect: The server closed the websocket.
            WebSocketNetworkError: A network error occured.
            WebSocketInvalidTypeReceived: The received event
                didn't correspond to the specified mode.

        Examples:
            Wait for data until available.

                try:
                    data = await ws.receive_json()
                except WebSocketDisconnect:
                    print("Connection closed")

            Wait for data for 2 seconds.

                try:
                    data = await ws.receive_json(timeout=2.)
                except TimeoutError:
                    print("No data received.")
                except WebSocketDisconnect:
                    print("Connection closed")
        r   r   Nr   r   r   r!   r!   r"   r     s   *
z"AsyncWebSocketSession.receive_jsonr   r   r   c              	      s   | j   | jjtjjjtjjjhvrTtj	||}| j
|}z(| j4 I dH  | j|I dH  W d  I dH  n1 I dH sCw   Y  W n
 tjyS   Y nw | j I dH  dS )a  
        Close the WebSocket session.

        Internally, it'll send the
        [CloseConnection][wsproto.events.CloseConnection] event.

        *This method is automatically called when exiting the context manager.*

        Args:
            code:
                The integer close code to indicate why the connection has closed.
            reason:
                Additional reasoning for why the connection has closed.

        Examples:
            Close the WebSocket session.

                await ws.close()
        N)r=   r   r1   r   r0   r   r   r   re   r   rg   r?   r,   ro   rp   rq   r   r   r!   r!   r"   rZ     s"   
(zAsyncWebSocketSession.closer   c              
      s  d}z| j  s| |I dH }| j| | j D ]}t|tjjrT| j	|
 }| j4 I dH  | j|I dH  W d  I dH  n1 I dH sNw   Y  qt|tjjrc| j|j qt|tjjro| j   t|tjjr|js|du r|j}n*||j7 }n$|du r| j	|I dH  nt|}|||j }d}| j	|I dH  q| j	|I dH  q| j  r	W dS W dS  tjtjtfy   | tjdI dH  | j	t  I dH  Y dS w )am  
        Background task listening for data from the server.

        Internally, it'll:

        * Answer to Ping events.
        * Acknowledge Pong events.
        * Put other events in the [_events][_events]
        queue that'll eventually be consumed by the user.

        Args:
            max_bytes: The maximum chunk size to read at each iteration.
        Nr   )!r=   r   r   r1   r   re   r   r0   rf   rg   r&   r?   r,   ro   r   r:   r   rb   r   r   r   r   rt   r   r   rp   r   rq   r$   rZ   r   rr   r   r   r!   r!   r"   rT     sL   
(
"z)AsyncWebSocketSession._background_receiver   r   c                    s   | j  skt|I d H  z	|  I d H }W n tjjy#   Y d S w |d urdzt| |	 I d H  W d    n1 s@w   Y  W n t
yc   | tjdI d H  | jt I d H  Y nw | j  rd S d S r   )r=   r   r   sleeprl   r0   	utilitiesLocalProtocolErrorr   rN   TimeoutErrorrZ   r   rr   r   rg   r   )rG   r   r   r   r!   r!   r"   rX     s*   

z0AsyncWebSocketSession._background_keepalive_pingc                    s&   | j |I d H }|dkrt |S r   r   r   r!   r!   r"   r   !  s
   z"AsyncWebSocketSession._read_stream)r-   r   r   r   rI   r   r   )-r   r   r    r   r   r   r   r   r   r0   re   r<   r	   r   r   r   r   r   r   r   r   rH   r   r   BaseExceptionr   r   r   r   rl   rg   r{   r   r   r   r   r   r   r   r   r   rZ   rT   rX   r   r!   r!   r!   r"   r   7  s   
 



#

/()
2"7
subprotocolsr-   c                 C   s:   ddt tdddd}| d urd| |d< |S )	Nupgrade	websocket   r   13)r1   r   zsec-websocket-keyzsec-websocket-versionz, r/   )base64	b64encodesecretstoken_bytesdecoder[   )r   r5   r!   r!   r"   _get_headers(  s   r  c                   @   s   e Zd ZdZeeeeedde	j
dedededB dedB d	ee d
dfddZej	ddedee dB dejd
ejeddf fddZdS )WebSocketClienta6  
    An sync WebSocket client.

    This class provides an API for connecting to WebSocket.

    Attributes:
        client:
            HTTPX client to use.
            If not provided, a default one will be initialized.
        max_message_size_bytes:
            Message size in bytes to receive from the server.
            Defaults to 65 KiB.
        queue_size:
            Size of the queue where the received messages will be held
            until they are consumed.
            If the queue is full, the client will stop receive messages
            from the server until the queue has room available.
            Defaults to 512.
        keepalive_ping_interval_seconds:
            Interval at which the client will automatically send a Ping event
            to keep the connection alive. Set it to `None` to disable this mechanism.
            Defaults to 20 seconds.
        keepalive_ping_timeout_seconds:
            Maximum delay the client will wait for an answer to its Ping event.
            If the delay is exceeded,
            [WebSocketNetworkError][httpx_ws.WebSocketNetworkError]
            will be raised and the connection closed.
            Defaults to 20 seconds.
        session_class:
            The session class to use.
            Defaults to [WebSocketSession][httpx_ws.WebSocketSession].
    r(   r)   r*   r+   session_classclientr(   r)   r*   Nr+   r	  r-   c                C   (   || _ || _|| _|| _|| _|| _d S rI   r
  r(   r)   r*   r+   r	  rG   r
  r(   r)   r*   r+   r	  r!   r!   r"   rH   X     
zWebSocketClient.__init__urlr   r   c              	   k   s    | di }|t| | jjd|fd|i|A}|jdkr%t|| j|jd | j	| j
| j| j|d}| |V  W d   n1 sGw   Y  W d   dS W d   dS 1 s_w   Y  dS )a'  
        Start a sync WebSocket session.

        It returns a context manager that'll automatically
        call [close()][httpx_ws.WebSocketSession.close] when exiting.

        Args:
            url: The WebSocket URL.
            subprotocols:
                Optional list of subprotocols to negotiate with the server.
            **kwargs:
                Additional keyword arguments that will be passed to
                the [HTTPX stream()](https://www.python-httpx.org/api/#request) method.

        Returns:
            A [context manager][contextlib.AbstractContextManager]
                for [WebSocketSession][httpx_ws.WebSocketSession].

        Examples:
            Initialize the client and connect to a WebSocket.

                with httpx.Client() as client:
                    ws_client = WebSocketClient(client)
                    with ws_client.connect("http://localhost:8000/ws") as ws:
                        message = ws.receive_text()
                        print(message)
                        ws.send_text("Hello!")
        r5   GETe   network_streamr'   Npopupdater  r
  r,   status_coder   r	  
extensionsr(   r)   r*   r+   rG   r  r   r   r5   r&   sessionr!   r!   r"   connectk  s(   $
"zWebSocketClient.connectrI   )r   r   r    r   r   r   r   r   r   r   Clientr   r   r   r   rH   r   contextmanagerr   listr   r   	Generatorr  r!   r!   r!   r"   r  6  sJ    %


r  )r(   r)   r*   r+   r   r	  r  r
  r(   r)   r*   r+   r	  r   c             	   k   s    |du rLt  9}t||||||d}	|	j| fd|i|}
|
V  W d   n1 s-w   Y  W d   dS W d   dS 1 sEw   Y  dS t||||||d}	|	j| fd|i|}
|
V  W d   dS 1 spw   Y  dS )a%	  
    Start a sync WebSocket session.

    It returns a context manager that'll automatically
    call [close()][httpx_ws.WebSocketSession.close] when exiting.

    Args:
        url: The WebSocket URL.
        client:
            HTTPX client to use.
            If not provided, a default one will be initialized.
        max_message_size_bytes:
            Message size in bytes to receive from the server.
            Defaults to 65 KiB.
        queue_size:
            Size of the queue where the received messages will be held
            until they are consumed.
            If the queue is full, the client will stop receive messages
            from the server until the queue has room available.
            Defaults to 512.
        keepalive_ping_interval_seconds:
            Interval at which the client will automatically send a Ping event
            to keep the connection alive. Set it to `None` to disable this mechanism.
            Defaults to 20 seconds.
        keepalive_ping_timeout_seconds:
            Maximum delay the client will wait for an answer to its Ping event.
            If the delay is exceeded,
            [WebSocketNetworkError][httpx_ws.WebSocketNetworkError]
            will be raised and the connection closed.
            Defaults to 20 seconds.
        subprotocols:
            Optional list of subprotocols to negotiate with the server.
        session_class:
            The session class to use.
            Defaults to [WebSocketSession][httpx_ws.WebSocketSession].
        **kwargs:
            Additional keyword arguments that will be passed to
            the [HTTPX stream()](https://www.python-httpx.org/api/#request) method.

    Returns:
        A [context manager][contextlib.AbstractContextManager]
            for [WebSocketSession][httpx_ws.WebSocketSession].

    Examples:
        Without explicit HTTPX client.

            with connect_ws("http://localhost:8000/ws") as ws:
                message = ws.receive_text()
                print(message)
                ws.send_text("Hello!")

        With explicit HTTPX client.

            with httpx.Client() as client:
                with connect_ws("http://localhost:8000/ws", client) as ws:
                    message = ws.receive_text()
                    print(message)
                    ws.send_text("Hello!")
    Nr  r   )r   r  r  r  r  r
  r(   r)   r*   r+   r   r	  r   	ws_clientr   r!   r!   r"   
connect_ws  sF   J
	""r!  c                   @   s   e Zd ZdZeeeeedde	j
dedededB dedB d	ee d
dfddZej	ddedee dB dejd
ejedf fddZdS )AsyncWebSocketClientaA  
    An async WebSocket client.

    This class provides an API for connecting to WebSocket.

    Attributes:
        client:
            HTTPX client to use.
            If not provided, a default one will be initialized.
        max_message_size_bytes:
            Message size in bytes to receive from the server.
            Defaults to 65 KiB.
        queue_size:
            Size of the queue where the received messages will be held
            until they are consumed.
            If the queue is full, the client will stop receive messages
            from the server until the queue has room available.
            Defaults to 512.
        keepalive_ping_interval_seconds:
            Interval at which the client will automatically send a Ping event
            to keep the connection alive. Set it to `None` to disable this mechanism.
            Defaults to 20 seconds.
        keepalive_ping_timeout_seconds:
            Maximum delay the client will wait for an answer to its Ping event.
            If the delay is exceeded,
            [WebSocketNetworkError][httpx_ws.WebSocketNetworkError]
            will be raised and the connection closed.
            Defaults to 20 seconds.
        session_class:
            The session class to use.
            Defaults to [AsyncWebSocketSession][httpx_ws.AsyncWebSocketSession].
    r  r
  r(   r)   r*   Nr+   r	  r-   c                C   r  rI   r  r  r!   r!   r"   rH   )  r  zAsyncWebSocketClient.__init__r  r   r   c              
   K  s   | di }|t| | jjd|fd|i|4 I dH F}|jdkr)t|| j|jd | j	| j
| j| j|d}|4 I dH  |V  W d  I dH  n1 I dH sUw   Y  W d  I dH  dS 1 I dH skw   Y  dS )am  
        Start an async WebSocket session.

        It returns an async context manager that'll automatically
        call [close()][httpx_ws.AsyncWebSocketSession.close] when exiting.

        Args:
            url: The WebSocket URL.
            subprotocols:
                Optional list of subprotocols to negotiate with the server.
            **kwargs:
                Additional keyword arguments that will be passed to
                the [HTTPX stream()](https://www.python-httpx.org/api/#request) method.

        Returns:
            An [async context manager][contextlib.AbstractAsyncContextManager]
                for [AsyncWebSocketSession][httpx_ws.AsyncWebSocketSession].

        Examples:
            Initialize the client and connect to a WebSocket.

                async with httpx.AsyncClient() as client:
                    ws_client = AsyncWebSocketClient(client)
                    async with ws_client.connect("http://localhost:8000/ws") as ws:
                        message = await ws.receive_text()
                        print(message)
                        await ws.send_text("Hello!")
        r5   r  Nr  r  r'   r  r  r!   r!   r"   r  <  s2   #
*.zAsyncWebSocketClient.connectrI   )r   r   r    r   r   r   r   r   r   r   AsyncClientr   r   r   r   rH   r   asynccontextmanagerr   r  r   r   AsyncGeneratorr  r!   r!   r!   r"   r"    sJ    %


r"  c             
   K  s  |du rXt  4 I dH >}t||||||d}	|	j| fd|i|4 I dH }
|
V  W d  I dH  n1 I dH s;w   Y  W d  I dH  dS 1 I dH sQw   Y  dS t||||||d}	|	j| fd|i|4 I dH }
|
V  W d  I dH  dS 1 I dH sw   Y  dS )a	  
    Start an async WebSocket session.

    It returns an async context manager that'll automatically
    call [close()][httpx_ws.AsyncWebSocketSession.close] when exiting.

    Args:
        url: The WebSocket URL.
        client:
            HTTPX client to use.
            If not provided, a default one will be initialized.
        max_message_size_bytes:
            Message size in bytes to receive from the server.
            Defaults to 65 KiB.
        queue_size:
            Size of the queue where the received messages will be held
            until they are consumed.
            If the queue is full, the client will stop receive messages
            from the server until the queue has room available.
            Defaults to 512.
        keepalive_ping_interval_seconds:
            Interval at which the client will automatically send a Ping event
            to keep the connection alive. Set it to `None` to disable this mechanism.
            Defaults to 20 seconds.
        keepalive_ping_timeout_seconds:
            Maximum delay the client will wait for an answer to its Ping event.
            If the delay is exceeded,
            [WebSocketNetworkError][httpx_ws.WebSocketNetworkError]
            will be raised and the connection closed.
            Defaults to 20 seconds.
        subprotocols:
            Optional list of subprotocols to negotiate with the server.
        session_class:
            The session class to use.
            Defaults to [AsyncWebSocketSession][httpx_ws.AsyncWebSocketSession].
        **kwargs:
            Additional keyword arguments that will be passed to
            the [HTTPX stream()](https://www.python-httpx.org/api/#request) method.

    Returns:
        An [async context manager][contextlib.AbstractAsyncContextManager]
            for [AsyncWebSocketSession][httpx_ws.AsyncWebSocketSession].

    Examples:
        Without explicit HTTPX client.

            async with aconnect_ws("http://localhost:8000/ws") as ws:
                message = await ws.receive_text()
                print(message)
                await ws.send_text("Hello!")

        With explicit HTTPX client.

            async with httpx.AsyncClient() as client:
                async with aconnect_ws("http://localhost:8000/ws", client) as ws:
                    message = await ws.receive_text()
                    print(message)
                    await ws.send_text("Hello!")
    Nr  r   )r   r#  r"  r  r  r!   r!   r"   aconnect_wst  sP   J*..r&  rI   )Er  concurrent.futuresrJ   r   r   r7   r  r;   r   typesr   r   rp   r   r0   wsproto.utilitiesanyio.streams.memoryr   r   r   r   wsproto.frame_protocolr   _exceptionsr	   r
   r   r   r   _pingr   r   	transportr   Literalr   TypeVarr   r   r   r   r   r   r   r   	Exceptionr   r$   r   r   r  r   dictr   r  Genericr  r  r  r   r   r   r  r!  r"  r$  r#  r%  r&  r!   r!   r!   r"   <module>   s    


    	   t

l

dm

