o
    hq                     @   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rjd 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  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)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   b/var/www/html/aiguide_backend/venv/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)context_value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)typehandled)client_options	mechanism)hint)r   r$   r%   r   r&   _make_response_event_processorr(   clientr	   r
   optionsr"   capture_event)errorsargskwargsr,   r-   r0   r.   r/   erroreventr9   )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 r3   )r   r$   r%   r   r&   r:   r(   r;   r	   r>   r
   r<   r"   r=   )r0   r?   r@   r,   r-   query_resultr.   r/   rA   rB   r9   )old_handle_query_resultr   r   #_sentry_patched_handle_query_result`   s2   



z;_patch_graphql.<locals>._sentry_patched_handle_query_result)ariadne_graphqlparse_queryhandle_graphql_errorshandle_query_result)r2   rD   rG   r   )rC   rF   r1   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graphql
api_targetr+   )
isinstancedictr	   intget	TypeError
ValueErrorr   r   r   r$   r;   
setdefault)rB   r9   content_lengthrequest_infor+   r   r   inner   s:   




z,_make_request_event_processor.<locals>.innerr   )r+   r[   r   rZ   r   r'      s   r'   c                    rL   )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   rT   rW   )rB   r9   r\   r]   r   r   r[      s   



z-_make_response_event_processor.<locals>.innerr   )r]   r[   r   r^   r   r:      s   r:   N)%	importlibr   sentry_sdk.hubr   r   sentry_sdk.integrationsr   r   sentry_sdk.integrations.loggingr   $sentry_sdk.integrations._wsgi_commonr   sentry_sdk.utilsr	   r
   r   sentry_sdk._typesr   rH   ImportErrortypingr   r   r   r   ariadne.typesr   r   r   r   graphql.language.astr   r   r   r   r'   r:   r   r   r   r   <module>   s*    Q 