o
    OÊÆh’  ã                   @   s²   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m	Z	 d dl
mZmZmZmZmZ e jege je f Ze jeege je f Ze  d¡ZG dd„ dƒZG d	d
„ d
e	ƒZdS )é    N)ÚBackgroundTask)ÚRequest)ÚContentStreamÚResponseÚStreamingResponse)ÚASGIAppÚMessageÚReceiveÚScopeÚSendÚTc                   @   s\   e Zd Z	ddedeje ddfdd„Zdede	d	e
ddfd
d„Zdededefdd„ZdS )ÚBaseHTTPMiddlewareNÚappÚdispatchÚreturnc                 C   s$   || _ |d u r| j| _d S || _d S ©N)r   r   Údispatch_func)Úselfr   r   © r   úf/var/www/html/HyperOracle/crypto_trader/venv/lib/python3.10/site-packages/starlette/middleware/base.pyÚ__init__   s   zBaseHTTPMiddleware.__init__ÚscopeÚreceiveÚsendc              	   ƒ   sÄ   �ˆd dkrˆ  ˆ||¡I d H  d S t ¡ ‰ dtdtf‡ ‡‡‡fdd„}t ¡ 4 I d H š(‰tˆ|d�}ˆ ||¡I d H }|ˆ||ƒI d H  ˆ  ¡  W d   ƒI d H  d S 1 I d H s[w   Y  d S )NÚtypeÚhttpÚrequestr   c                 “   s.  �d ‰ t  ¡ \‰‰dtf‡‡fdd„‰d‡‡fdd„}dtdd f‡fdd„‰d‡ ‡‡‡‡‡fd	d
„}ˆ	 |¡ ˆ	 |¡ z ˆ ¡ I d H }| dd ¡}|d dkr[|d ur[ˆ ¡ I d H }W n t jyn   ˆ d urjˆ ‚tdƒ‚w |d dkswJ ‚dtj	t
d f f‡ ‡fdd„}t|d |ƒ |d�}|d |_|S )Nr   c               	   “   s¦   �ˆ  ¡ r	ddiS t ¡ 4 I d H š-‰ dtjg tjt f dtf‡ fdd„} ˆ  | ˆj¡ | ˆj	ƒI d H }W d   ƒI d H  n1 I d H sDw   Y  ˆ  ¡ rQddiS |S )Nr   zhttp.disconnectÚfuncr   c                 “   s   �| ƒ I d H }ˆ j  ¡  |S r   )Úcancel_scopeÚcancel)r   Úresult©Ú
task_groupr   r   Úwrap)   s   €
z[BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnect.<locals>.wrap)
Úis_setÚanyioÚcreate_task_groupÚtypingÚCallableÚ	Awaitabler   Ú
start_soonÚwaitr   )r#   Úmessage)r   Úresponse_sentr!   r   Úreceive_or_disconnect#   s   €&(ø
zMBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnectc                   “   s   �ˆ  ¡ I d H  ˆ  ¡  d S r   )r+   Úcloser   )Úrecv_streamr-   r   r   Ú"close_recv_stream_on_response_sent6   s   €zZBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.close_recv_stream_on_response_sentr,   c                 “   s0   �zˆ   | ¡I d H  W d S  tjy   Y d S w r   )r   r%   ÚBrokenResourceError)r,   )Úsend_streamr   r   Úsend_no_error:   s   €þzEBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.send_no_errorc                  “   s˜   �ˆ4 I d H š8 zˆ  ˆˆˆ¡I d H  W n ty( }  z| ‰ W Y d } ~ nd } ~ ww W d   ƒI d H  d S W d   ƒI d H  d S 1 I d H sEw   Y  d S r   )r   Ú	Exception)Úexc)Úapp_excr.   r   r   r4   r3   r   r   ÚcoroA   s   €€ÿÿþ.üz<BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.coroÚinfor   úhttp.response.debugzNo response returned.zhttp.response.startc               	     s„   �ˆ4 I d H š( ˆ2 z3 d H W } | d dksJ ‚|   dd¡}|r#|V  q
6 W d   ƒI d H  n1 I d H s5w   Y  ˆ d ur@ˆ ‚d S )Nr   zhttp.response.bodyÚbodyó    )Úget)r,   r;   )r7   r0   r   r   Úbody_streamY   s   €€ü(ÿÿzCBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.body_streamÚstatus)Ústatus_codeÚcontentr9   Úheaders)r   N)r%   Úcreate_memory_object_streamr   r*   r   r=   ÚEndOfStreamÚRuntimeErrorr'   ÚAsyncGeneratorÚbytesÚ_StreamingResponseÚraw_headers)r   r1   r8   r,   r9   r>   Úresponse©r-   r   r   r"   )r7   r.   r0   r   r4   r3   r   Ú	call_next   s6   €
	
€ýÿ
z.BaseHTTPMiddleware.__call__.<locals>.call_next)r   )r   r%   ÚEventr   r   r&   r   Úset)r   r   r   r   rL   r   rJ   r   rK   r   Ú__call__   s   €K
.üzBaseHTTPMiddleware.__call__r   rL   c                 Ã   s   �t ƒ ‚r   )ÚNotImplementedError)r   r   rL   r   r   r   r   p   s   €zBaseHTTPMiddleware.dispatchr   )Ú__name__Ú
__module__Ú__qualname__r   r'   ÚOptionalÚDispatchFunctionr   r
   r	   r   rO   r   ÚRequestResponseEndpointr   r   r   r   r   r   r      s"    ÿÿÿ
þXÿÿþr   c                       sŠ   e Zd Z					ddededejejeef  deje deje	 dejejeej
f  d	df‡ fd
d„Zded	df‡ fdd„Z‡  ZS )rH   éÈ   NrA   r@   rB   Ú
media_typeÚ
backgroundr9   r   c                    s   || _ tƒ  |||||¡ d S r   )Ú_infoÚsuperr   )r   rA   r@   rB   rX   rY   r9   ©Ú	__class__r   r   r   w   s   	z_StreamingResponse.__init__r   c                 ƒ   s0   �| j r|d| j dœƒI d H  tƒ  |¡I d H S )Nr:   )r   r9   )rZ   r[   Ústream_response)r   r   r\   r   r   r^   ƒ   s   €z"_StreamingResponse.stream_response)rW   NNNN)rQ   rR   rS   r   Úintr'   rT   ÚMappingÚstrr   ÚAnyr   r   r^   Ú__classcell__r   r   r\   r   rH   v   s,    ùþýüûúùørH   )r'   r%   Ústarlette.backgroundr   Ústarlette.requestsr   Ústarlette.responsesr   r   r   Ústarlette.typesr   r   r	   r
   r   r(   r)   rV   rU   ÚTypeVarr   r   rH   r   r   r   r   Ú<module>   s    ÿ
e