a
    xd                     @   s   d Z ddlZddlmZmZ ddlmZ ddlmZ erhddl	m
Z
 ddl	mZ ddl	mZ dd	lmZ d
d Zdd Zdd Zdd Zdd ZdS )z
Instrumentation for Django 3.0

Since this file contains `async def` it is conditionally imported in
`sentry_sdk.integrations.django` (depending on the existence of
`django.core.handlers.asgi`.
    N)Hub
_functools)MYPY)SentryAsgiMiddleware)Any)Union)Callable)HttpResponsec                    s,   ddl m  j fdd}|_d S )Nr   DjangoIntegrationc                    sL   t j d u r$| |||I d H S t| ddj}||||I d H S )NTZunsafe_context_data)r   currentget_integrationr   __get__Z
_run_asgi3)selfscopereceivesend
middlewarer   clsold_app [/var/www/html/Ranjet/env/lib/python3.9/site-packages/sentry_sdk/integrations/django/asgi.pysentry_patched_asgi_handler   s    zCpatch_django_asgi_handler_impl.<locals>.sentry_patched_asgi_handler)sentry_sdk.integrations.djangor   __call__)r   r   r   r   r   patch_django_asgi_handler_impl   s    
r   c                    s   | j  fdd}|| _ d S )Nc                    s    | | |I d H S Nr   )r   request_before_get_responseZold_get_response_asyncr   r   !sentry_patched_get_response_async0   s    zCpatch_get_response_async.<locals>.sentry_patched_get_response_async)Zget_response_async)r   r!   r"   r   r    r   patch_get_response_async,   s    r#   c                    sH   dd l }ddlm  |jdk r<j fdd}|_nt d S )Nr   r
   z3.0.0c                    sP   t jd u r" ||I d H S t fdddd}| j||I d H S )Nc                    s     S r   )r   )Z_scope)r   r   r   r   r   <lambda>H       zWpatch_channels_asgi_handler_impl.<locals>.sentry_patched_asgi_handler.<locals>.<lambda>Tr   )r   r   r   r   r   )r   r   r   r   r   r   r   r   B   s    zEpatch_channels_asgi_handler_impl.<locals>.sentry_patched_asgi_handler)channelsr   r   __version__r   r   )r   r'   r   r   r   r    patch_channels_asgi_handler_impl8   s    
r)   c                    s   t   fdd}|S )Nc                    sR   j d| jjd,  | g|R i |I d H W  d    S 1 sD0    Y  d S )Nzdjango.view)opdescription)Z
start_spanZresolver_matchZ	view_name)r   argskwargscallbackhubr   r   sentry_wrapped_callbackW   s    z0wrap_async_view.<locals>.sentry_wrapped_callback)r   wraps)r0   r/   r1   r   r.   r   wrap_async_viewU   s    r3   c                    s   G  fddd}|S )zh
    Mixin class factory that generates a middleware mixin for handling requests
    in async mode.
    c                       s8   e Zd ZerdZdd Zdd Zdd Z fdd	ZdS )
z7_asgi_middleware_mixin_factory.<locals>.SentryASGIMixinNc                 S   s   || _ d | _|   d S r   )get_response_acall_method_async_check)r   r4   r   r   r   __init__n   s    z@_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin.__init__c                 S   s   t | jrt jj| _dS )z
            If get_response is a coroutine function, turns us into async mode so
            a thread is not consumed during a whole request.
            Taken from django.utils.deprecation::MiddlewareMixin._async_check
            N)asyncioiscoroutinefunctionr4   Z
coroutinesZ_is_coroutiner&   r   r   r   r6   t   s    zD_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin._async_checkc                 S   s   t | jS )z
            Function that checks if we are in async mode,
            and if we are forwards the handling of requests to __acall__
            )r8   r9   r4   r&   r   r   r   async_route_check~   s    zI_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin.async_route_checkc                    s   | j }|d u r6t| jdr*| jj | _ }n| j | _ } |d}|d u r\||i |I d H S |$ ||i |I d H W  d    S 1 s0    Y  d S )N	__acall__)Z
old_method)r5   hasattr_innerr;   )r   r,   r-   fZmiddleware_span_check_middleware_spanr   r   r;      s    
zA_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin.__acall__)	__name__
__module____qualname__r   r=   r7   r6   r:   r;   r   r?   r   r   SentryASGIMixinj   s   
rD   r   )r@   rD   r   r?   r   _asgi_middleware_mixin_factoryc   s    -rE   )__doc__r8   Z
sentry_sdkr   r   Zsentry_sdk._typesr   Zsentry_sdk.integrations.asgir   typingr   r   r   Zdjango.http.responser	   r   r#   r)   r3   rE   r   r   r   r   <module>   s   