o
    㥵i                     @  s   d dl mZ d dlZd dlmZ d dl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 dd	lmZ d
dlmZmZ d
dlmZ dddZG dd dZG dd dZdS )    )annotationsN)
isfunction)TYPE_CHECKINGAnyCallable	GeneratorList)cast)Message)Literal   )AsyncViewType   )request	websocket)HttpResponsemethodstrreturn(Callable[[AsyncViewType], AsyncViewType]c                   s:   dkrddhnh dd  id fdd	}|S )z;
    Set the acceptable request method of the function
    GETHEADAllow, functionr   r   c                   s:   t  std fdd}t|d  tt|S )Nz,`required_method` can only decorate functionc                     s8   t j v r I d H S t jdkrtdS tddS )NOPTIONSheaders  status_coder   )r   r   r    )allow_methodsr   r   r!   B/home/ubuntu/.local/lib/python3.10/site-packages/kui/asgi/views.pywrapper   s   


z3required_method.<locals>.decorator.<locals>.wrapper
__method__)r   	TypeErrorsetattruppertyping_castr   )r   r$   r"   r   r   )r   r#   	decorator   s
   
z"required_method.<locals>.decoratorN)r   r   r   r   )join)r   r+   r!   r*   r#   required_method   s   r-   c                   @  sX   e Zd ZU g dZerded< dddZdd	d
ZdddZdddZ	e
dddZdS )HttpView)getpostputpatchdeleteheadoptionstracez	List[str]__methods__r   Nonec                   s    fdd j D  _d S )Nc                   s   g | ]}t  |r| qS r!   )hasattrr(   ).0mclsr!   r#   
<listcomp>9   s    z.HttpView.__init_subclass__.<locals>.<listcomp>)HTTP_METHOD_NAMESr7   r<   r!   r<   r#   __init_subclass__8   s   zHttpView.__init_subclass__Generator[None, None, Any]c                 C     |    S N__impl__	__await__selfr!   r!   r#   rF   ;      zHttpView.__await__r   c                   s"   t | tj | j}| I d H S rC   )getattrr   r   lowerhttp_method_not_allowed)rH   handlerr!   r!   r#   rE   >   s   zHttpView.__impl__r   c                   s   t ddd| jidS )Nr   r   r   r   r   r,   r7   rG   r!   r!   r#   rL   B   s   z HttpView.http_method_not_allowedc                   s   t dd| jidS )z8Handle responding to requests for the OPTIONS HTTP verb.r   r   r   rN   r<   r!   r!   r#   r5   G   s   zHttpView.optionsNr   r8   )r   rA   )r   r   )r   r   )__name__
__module____qualname__r?   r   __annotations__r@   rF   rE   rL   classmethodr5   r!   r!   r!   r#   r.   )   s   
 



r.   c                   @  sT   e Zd ZU dZded< dd Zddd	ZdddZdddZdddZ	dddZ
dS )
SocketViewanystrz*Literal['anystr', 'text', 'bytes', 'json']encodingc                 C  rB   rC   rD   rG   r!   r!   r#   rF   P   rI   zSocketView.__await__r   r8   c              
     s   d}zZz>|   I d H  	 t I d H }|d dkr+| |I d H }| |I d H  n|d dkr8|dd}n	td|d  qW n tyR } zd}|d }~ww W | |I d H  d S | |I d H  w )	Ni  Ttypezwebsocket.receivezwebsocket.disconnectcodezUnexpected message type: %si  )	
on_connectr   receivedecode
on_receiver/   RuntimeError	Exceptionon_disconnect)rH   
close_codemessagedataexcr!   r!   r#   rE   S   s,   &zSocketView.__impl__rb   r
   r   c                   s   | j dkrd|vrtjddI d H  td|d S | j dkr5d|vr1tjddI d H  td|d S | j dkrj|dd urF|d }n|d d}zt|W S  tjj	yi   tjddI d H  td	w |drs|d S |d S )
Ntexti  rY   z/Expected text websocket messages, but got bytesbytesz/Expected bytes websocket messages, but got textjsonzutf-8zMalformed JSON data received.)
rW   r   closer^   r/   r\   rh   loadsdecoderJSONDecodeError)rH   rb   re   r!   r!   r#   r\   g   s,   



zSocketView.decodec                   s   t  I dH  dS )z3Override to handle an incoming websocket connectionN)r   acceptrG   r!   r!   r#   rZ      s   zSocketView.on_connectrc   c                   s   dS )z0Override to handle an incoming websocket messageNr!   )rH   rc   r!   r!   r#   r]      s    zSocketView.on_receivera   intc                   s   t j|dI dH  dS )z,Override to handle a disconnecting websocketrf   N)r   ri   )rH   ra   r!   r!   r#   r`      s   zSocketView.on_disconnectNrO   )rb   r
   r   r   )rc   r   r   r8   )ra   rn   r   r8   )rP   rQ   rR   rW   rS   rF   rE   r\   rZ   r]   r`   r!   r!   r!   r#   rU   M   s   
 



rU   )r   r   r   r   )
__future__r   rh   inspectr   typingr   r   r   r   r   r	   r)   baize.typingr
   typing_extensionsr   routingr   requestsr   r   	responsesr   r-   r.   rU   r!   r!   r!   r#   <module>   s    
$