o
    $6d                     @   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   pC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\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   )Z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$   Z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   r2   r1   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+   r9   )r5   r/   r   r   body_streamY   s   (zCBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.body_streamstatus)status_codecontentr7   headers)r   N)r$   Zcreate_memory_object_streamr   r)   r   r;   ZEndOfStreamRuntimeErrorr&   AsyncGeneratorbytes_StreamingResponseZraw_headers)r   r0   r6   r+   r7   r<   responser,   r   r   r!   )r5   r-   r/   r   r2   r1   r   	call_next   s6   
	

z.BaseHTTPMiddleware.__call__.<locals>.call_next)r   )r   r$   Eventr   r   r%   r   set)r   r   r   r   rG   r   rE   r   rF   r   __call__   s   K
.zBaseHTTPMiddleware.__call__r   rG   c                    s   t  r   )NotImplementedError)r   r   rG   r   r   r   r   p   s   zBaseHTTPMiddleware.dispatchr   )__name__
__module____qualname__r   r&   OptionalDispatchFunctionr   r
   r	   r   rJ   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 )rD      Nr?   r>   r@   
media_type
backgroundr7   r   c                    s   || _ t ||||| d S r   )_infosuperr   )r   r?   r>   r@   rS   rT   r7   	__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 )Nr8   )r   r7   )rU   rV   stream_response)r   r   rW   r   r   rY      s   z"_StreamingResponse.stream_response)rR   NNNN)rL   rM   rN   r   intr&   rO   Mappingstrr   Anyr   r   rY   __classcell__r   r   rW   r   rD   v   s,    rD   )r&   r$   Zstarlette.backgroundr   Zstarlette.requestsr   Zstarlette.responsesr   r   r   Zstarlette.typesr   r   r	   r
   r   r'   r(   rQ   rP   TypeVarr   r   rD   r   r   r   r   <module>   s    
e