o
    ZhT                     @   s  d dl mZ d dlmZmZ d dlmZm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 zedZW n eyC   ed	w erld d
lmZmZmZmZ d dlmZmZmZmZ d dlmZ d dlm Z m!Z! G dd deZ"dd Z#dd Z$dd Z%dS )    )import_module)Hub_should_send_default_pii)DidNotEnableIntegration)ignore_logger)request_body_within_bounds)capture_internal_exceptionsevent_from_exceptionpackage_version)TYPE_CHECKINGzariadne.graphqlzariadne is not installed)AnyDictListOptional)GraphQLErrorGraphQLResultGraphQLSchemaQueryParser)DocumentNode)EventEventProcessorc                   @   s   e Zd ZdZedd ZdS )AriadneIntegrationariadnec                  C   s:   t d} | d u rtd| dk rtdtd t  d S )Nr   zUnparsable ariadne version.)r      zariadne 0.20 or newer required.)r   r   r   _patch_graphql)version r   V/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/integrations/ariadne.py
setup_once    s   
zAriadneIntegration.setup_onceN)__name__
__module____qualname__
identifierstaticmethodr   r   r   r   r   r      s    r   c                     sL   t jt j t jfdd}  fdd}fdd}| t _|t _|t _d S )Nc                    sn   t j}|t}|d u r | ||S | }t|}|| W d    n1 s*w   Y   | ||}|S )N)r   currentget_integrationr   configure_scope_make_request_event_processoradd_event_processor)Zcontext_valueZquery_parserdatahubintegrationscopeevent_processorresult)old_parse_queryr   r   _sentry_patched_parse_query6   s   

z3_patch_graphql.<locals>._sentry_patched_parse_queryc                    s   t j}|t}|d u r | g|R i |S  | g|R i |}| }t|d }|| W d    n1 s<w   Y  |jrst $ | D ]}t	||jj
|jddd\}	}
|j|	|
d qJW d    |S 1 snw   Y  |S N   F)typeZhandled)Zclient_optionsZ	mechanism)hint)r   r%   r&   r   r'   _make_response_event_processorr)   clientr	   r
   optionsr#   capture_event)errorsargskwargsr+   r,   r/   r-   r.   erroreventr5   )old_handle_errorsr   r   %_sentry_patched_handle_graphql_errorsD   s2   



z=_patch_graphql.<locals>._sentry_patched_handle_graphql_errorsc                    s   t j}|t}|d u r | g|R i |S  | g|R i |}| }t|d }|| W d    n1 s<w   Y  |jrvt ' | j	pLg D ]}t
||jj|jddd\}	}
|j|	|
d qMW d    |S 1 sqw   Y  |S r2   )r   r%   r&   r   r'   r6   r)   r7   r	   r:   r
   r8   r#   r9   )r/   r;   r<   r+   r,   Zquery_resultr-   r.   r=   r>   r5   )old_handle_query_resultr   r   #_sentry_patched_handle_query_result`   s2   



z;_patch_graphql.<locals>._sentry_patched_handle_query_result)ariadne_graphqlZparse_queryZhandle_graphql_errorsZhandle_query_result)r1   r@   rB   r   )r?   rA   r0   r   r   0   s   
r   c                        fdd}|S )z*Add request data and api_target to events.c              
      s   t  ts| S t d zt dpi dd}W n ttfy.   |  Y W  d    S w t rHtt	j
j|rH| di }d|d<  |d< n| di dr^| d d= W d    | S W d    | S W d    | S 1 sqw   Y  | S )NheaderszContent-Lengthr   requestgraphqlZ
api_targetr*   )
isinstancedictr	   intget	TypeError
ValueErrorr   r   r   r%   r7   
setdefault)r>   r5   content_lengthZrequest_infor*   r   r   inner   s:   




z,_make_request_event_processor.<locals>.innerr   )r*   rQ   r   rP   r   r(      s   r(   c                    rD   )z2Add response data to the event's response context.c                    st   t  - t r  dr(| di }d i|d< W d    | S W d    | S W d    | S 1 s3w   Y  | S )Nr:   contextsr*   response)r	   r   rK   rN   )r>   r5   rR   rS   r   r   rQ      s   



z-_make_response_event_processor.<locals>.innerr   )rS   rQ   r   rT   r   r6      s   r6   N)&	importlibr   Zsentry_sdk.hubr   r   Zsentry_sdk.integrationsr   r   Zsentry_sdk.integrations.loggingr   Z$sentry_sdk.integrations._wsgi_commonr   Zsentry_sdk.utilsr	   r
   r   Zsentry_sdk._typesr   rC   ImportErrortypingr   r   r   r   Zariadne.typesr   r   r   r   Zgraphql.language.astr   r   r   r   r   r(   r6   r   r   r   r   <module>   s*    Q 