o
    .j                     @  sh   d dl mZ d dlZd dlZd dlZzddlmZ W n ey)   ddlm	Z Y nw G dd deZ
dS )    )annotationsN   )AsyncWebSocketExtensionFromHTTP)AsyncRawExtensionFromHTTPc                   @  sP   e Zd ZdZdddZdd
dZedddZdddZdddZ	dddZ
dS )ASGIWebSocketExtensionzAsync WebSocket extension for ASGI applications.

    Uses the ASGI websocket protocol with send/receive queues to communicate
    with the application task.
    returnNonec                 C  s$   d| _ t | _t | _d | _d S )NF)_closedasyncioQueue_app_send_queue_app_receive_queue_taskself r   W/home/thesage/.local/lib/python3.10/site-packages/niquests/extensions/sgi/_async/_ws.py__init__   s   


zASGIWebSocketExtension.__init__app
typing.Anyscopedict[str, typing.Any]c                   s,  d fdd}d fdd}t |||| _ jd	d
iI dH   j I dH }|d	 dkrad _ j  t	
t j  jI dH  W d   n1 sQw   Y  td|dd |d	 dkrd _ j  t	
t j  jI dH  W d   n1 sw   Y  td|d	  dS )z<Start the ASGI app task and perform the WebSocket handshake.r   r   c                     s    j  I d H S N)r   getr   r   r   r   receive   s   z-ASGIWebSocketExtension.start.<locals>.receivemessager   c                   s    j | I d H  d S r   )r   put)r   r   r   r   send"   s   z*ASGIWebSocketExtension.start.<locals>.sendtypezwebsocket.connectNwebsocket.closeTz(WebSocket connection rejected with code code  zwebsocket.acceptz*Unexpected ASGI message during handshake: )r   r   )r   r   r   r   )r
   create_taskr   r   r   r   r   r	   cancel
contextlibsuppressCancelledErrorConnectionError)r   r   r   r   r   r   r   r   r   start   s*   

zASGIWebSocketExtension.startboolc                 C  s   | j S r   )r	   r   r   r   r   closed:   s   zASGIWebSocketExtension.closedstr | bytes | Nonec                   sj   | j rtd| j I dH }|d dkr(d|v r|d S d|v r&|d S dS |d dkr3d	| _ dS dS )
zlAwait the next message from the ASGI WebSocket app.
        Returns None when the app closes the connection.!The WebSocket extension is closedNr   zwebsocket.sendtextbytes    r   T)r	   OSErrorr   r   )r   r   r   r   r   next_payload>   s   z#ASGIWebSocketExtension.next_payloadbufstr | bytesc                   sZ   | j rtdt|ttfr| jdt|dI dH  dS | jd|dI dH  dS )z)Send a message to the ASGI WebSocket app.r,   zwebsocket.receive)r   r.   N)r   r-   )r	   r0   
isinstancer.   	bytearrayr   r   )r   r2   r   r   r   send_payloadS   s    z#ASGIWebSocketExtension.send_payloadc                   s   | j rdS d| _ | jdddI dH  | jdurC| j sE| j  ttj	 | jI dH  W d   dS 1 s<w   Y  dS dS dS )z.Close the WebSocket and clean up the app task.NTzwebsocket.disconnectr!   )r   r    )
r	   r   r   r   doner#   r$   r%   r
   r&   r   r   r   r   close]   s   
"zASGIWebSocketExtension.closeN)r   r   )r   r   r   r   r   r   )r   r)   )r   r+   )r2   r3   r   r   )__name__
__module____qualname____doc__r   r(   propertyr*   r1   r6   r8   r   r   r   r   r      s    




r   )
__future__r   r
   r$   typing0packages.urllib3.contrib.webextensions._async.wsr   ImportError1packages.urllib3.contrib.webextensions._async.rawr   r   r   r   r   r   <module>   s    