a
    xdB                     @   s  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZmZ d dl	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mZ d dlmZ er6d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm Z  d dlm!Z! d dl"m#Z# d dl"m$Z$ d dlm%Z%m&Z& ee' Z(zd dl)m*Z* W n  e+yf   d dl,m*Z* Y n0 G dd de-Z.d ddZ/G dd de.Z0G dd de.Z1dd Z2dS )!    )print_functionN)datetime	timedelta)defaultdict)Dsnloggercapture_internal_exceptions
json_dumps)BackgroundWorker)EnvelopeItem
PayloadRef)MYPY)Any)Callable)Dict)Iterable)Optional)Tuple)Type)Union)DefaultDict)PoolManager)ProxyManager)EventEndpointType)
getproxiesc                   @   sR   e Zd ZdZdZdddZdd Zdd Zdd	d
Zdd Z	dddZ
dd ZdS )	TransportzWBaseclass for all transports.

    A transport is used to send an event to sentry.
    Nc                 C   s8   || _ |r.|d d ur.|d r.t|d | _nd | _d S )Ndsn)optionsr   
parsed_dsn)selfr    r"   L/var/www/html/Ranjet/env/lib/python3.9/site-packages/sentry_sdk/transport.py__init__2   s    zTransport.__init__c                 C   s
   t  dS )zm
        This gets invoked with the event dictionary when an event should
        be sent to sentry.
        NNotImplementedErrorr!   eventr"   r"   r#   capture_event<   s    zTransport.capture_eventc                 C   s
   t  dS )a$  
        Send an envelope to Sentry.

        Envelopes are a data container format that can hold any type of data
        submitted to Sentry. We use it for transactions and sessions, but
        regular "error" events should go through `capture_event` for backwards
        compat.
        Nr%   )r!   enveloper"   r"   r#   capture_envelopeF   s    zTransport.capture_envelopec                 C   s   dS )z=Wait `timeout` seconds for the current events to be sent out.Nr"   r!   timeoutcallbackr"   r"   r#   flushT   s    zTransport.flushc                 C   s   dS )zForcefully kills the transport.Nr"   r!   r"   r"   r#   kill]   s    zTransport.killc                 C   s   dS )zVThis increments a counter for event loss by reason and
        data category.
        Nr"   )r!   reasondata_categoryitemr"   r"   r#   record_lost_eventb   s    
zTransport.record_lost_eventc                 C   s$   z|    W n ty   Y n0 d S N)r1   	Exceptionr0   r"   r"   r#   __del__n   s    zTransport.__del__)N)N)NN)__name__
__module____qualname____doc__r    r$   r)   r+   r/   r1   r5   r8   r"   r"   r"   r#   r   *   s    


 
	  
r   c              
   c   s   |d u rt  }| dD ]n}zN| dd\}}}|tt|d }|rV|dpXdD ]}||fV  qZW q ttfy   Y qY q0 qd S )N,:   seconds;r6   )r   utcnowsplitstripr   intLookupError
ValueError)headernowlimitretry_after
categories_categoryr"   r"   r#   _parse_rate_limitsv   s    rP   c                   @   s   e Zd ZdZdd Zd(ddZdd Zd)d
dZdd Zd*ddZ	d+ddZ
dd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd,d$d%Zd&d' ZdS )-HttpTransportzThe default HTTP transport.c                 C   s   ddl m} t| | | jd us&J || _t|d d| _| jd| | _	i | _
tj | _tt| _t | _| j| j|d |d |d d	| _dd
lm} || _d S )Nr   )VERSIONZtransport_queue_size)Z
queue_sizezsentry.python/%s
http_proxyhttps_proxyca_certs)rS   rT   rU   )Hub)Zsentry_sdk.constsrR   r   r$   r    r   r
   _workerZto_auth_auth_disabled_untilurllib3utilRetry_retryr   rF   _discarded_eventstime_last_client_report_sent
_make_pool_poolZ
sentry_sdkrV   hub_cls)r!   r   rR   rV   r"   r"   r#   r$      s(    
zHttpTransport.__init__Nc                 C   sd   | j d sd S d}|d ur:|j}|dkrJt| p6d}n|d u rJtd| j||f  |7  < d S )Nsend_client_reports   
attachmentzdata category not provided)r   r3   len	get_bytes	TypeErrorr^   )r!   r2   r3   r4   quantityr"   r"   r#   r5      s    
zHttpTransport.record_lost_eventc                 C   sh   |j d}|r,td | jt| n8|jdkrdtd t	 t
| j|pVdd | jd < d S )Nzx-sentry-rate-limitsz%Rate-limited via x-sentry-rate-limits  zRate-limited via 429<   r@   )headersgetr   warningrY   updaterP   statusr   rC   r   r]   get_retry_after)r!   responserI   r"   r"   r#   _update_rate_limits   s    


z!HttpTransport._update_rate_limitsstorec                    s    fdd}| tjjtj d z$jjdtj|||d}W n& tyz   	d |d  Y n0 zl
| |jdkr	d	 n@|jd
ks|jdk rtd|j|j 	d|j |d W |  n
|  0 d S )Nc                    s6    d u rj | dd n jD ]}j | |d qd S )Nerrorr3   r4   )r5   items)r2   r4   r*   r!   r"   r#   record_loss   s    
z0HttpTransport._send_request.<locals>.record_loss)z
User-AgentzX-Sentry-AuthPOST)bodyrm   networkZnetwork_errorrk   Z
status_429i,     z%Unexpected status code: %s (body: %s)z	status_{})rp   strrX   clientZ	to_headerrb   requestZget_api_urlr7   on_dropped_eventrt   rq   r   rv   dataformatclose)r!   r}   rm   endpoint_typer*   r{   rs   r"   rz   r#   _send_request   s>    	






zHttpTransport._send_requestc                 C   s   d S r6   r"   )r!   r2   r"   r"   r#   r     s    zHttpTransport.on_dropped_eventFrl   c                 C   st   | j d sd S |s(| jt | k s(d S | j}tt| _t | _|sJd S ttt dd | D ddddS )Nrd   c                 S   s    g | ]\\}}}|||d qS ))r2   rO   rj   r"   ).0rO   r2   rj   r"   r"   r#   
<listcomp>  s   z>HttpTransport._fetch_pending_client_report.<locals>.<listcomp>)	timestampdiscarded_events)jsonclient_report)type)	r   r`   r_   r^   r   rF   r   r   ry   )r!   forceintervalr   r"   r"   r#   _fetch_pending_client_report  s&    


z*HttpTransport._fetch_pending_client_reportc                 C   s,   | j |dd}|d ur(| t|gd d S )Nrl   )r   r   )ry   )r   r+   r   )r!   r   r   r"   r"   r#   _flush_client_reports*  s    z#HttpTransport._flush_client_reportsc                    s    fdd}||p|d S )Nc                    s     j | }|d uo|t kS r6   )rY   rn   r   rC   )Zbuckettsr0   r"   r#   	_disabled2  s    z0HttpTransport._check_disabled.<locals>._disabledr"   )r!   rO   r   r"   r0   r#   _check_disabled0  s    zHttpTransport._check_disabledc                 C   s   |  dr&| d | jddd d S t }tj|dd}|t| W d    n1 s`0    Y  | j	d usxJ t
d|dpd	|d
pd	|dpd	| j	j| j	jf  | j| dddd d S )Nrv   self_rate_limitsratelimit_backoffrw   wfileobjmodez>Sending event, type:%s level:%s event_id:%s project:%s host:%sr   nulllevelZevent_idzapplication/jsongzipzContent-TypezContent-Encoding)rm   )r   r   r5   ioBytesIOr   GzipFilewriter	   r    r   debugrn   
project_idhostr   getvalue)r!   r(   r}   fr"   r"   r#   _send_event9  s.    

,
zHttpTransport._send_eventc                 C   s
  g }|j D ]>}| |jr>|jdv r.| d | jd|d q
|| q
t|j|d}|j sbd S | jdd}|d ur|j | t	
 }tj|dd	}|| W d    n1 s0    Y  | jd usJ td
|j| jj| jj | j| dddd|d d S )N)Ztransactionrv   defaultr   r   rx   )rm   ry      )r   r   r   z(Sending envelope [%s] project:%s host:%szapplication/x-sentry-enveloper   r   r*   )rm   r   r*   )ry   r   r3   r   r5   appendr   rm   r   r   r   r   r   Zserialize_intor    r   r   descriptionr   r   r   r   )r!   r*   Z	new_itemsr4   Zclient_report_itemr}   r   r"   r"   r#   _send_envelopeX  s@    


(	zHttpTransport._send_envelopec                 C   s   dd|pt  dS )Nr?   CERT_REQUIRED)	num_pools	cert_reqsrU   )certifiwhere)r!   rU   r"   r"   r#   _get_pool_options  s    
zHttpTransport._get_pool_optionsc                 C   sN   t  d}|sdS |dD ]*}| }|j|sB|j|r dS qdS )NnoFr=   T)r   rn   rD   rE   r   endswithnetloc)r!   r    no_proxyr   r"   r"   r#   _in_no_proxy  s    zHttpTransport._in_no_proxyc                 C   s   d }|  |}|jdkr6|dkr6|p4| o4t d}|sX|dkrX|pV| oVt d}| |}|rxtj|fi |S tjf i |S d S )Nhttps http)r   schemer   rn   r   rZ   r   r   )r!   r    rS   rT   rU   proxyr   optsr"   r"   r#   ra     s    

zHttpTransport._make_poolc                    s@   j j fdd}j|s<d jddd d S )Nc                	      s`   H t  "     W d    n1 s40    Y  W d    n1 sR0    Y  d S r6   )r   r   r   r"   r(   hubr!   r"   r#   send_event_wrapper  s    
z7HttpTransport.capture_event.<locals>.send_event_wrapper
full_queuequeue_overflowrv   rw   )rc   currentrW   submitr   r5   )r!   r(   r   r"   r   r#   r)     s
    
zHttpTransport.capture_eventc                    sL   j j fdd}j|sHd  jD ]}jd|d q4d S )Nc                	      s`   H t  "     W d    n1 s40    Y  W d    n1 sR0    Y  d S r6   )r   r   r   r"   r*   r   r!   r"   r#   send_envelope_wrapper  s    
z=HttpTransport.capture_envelope.<locals>.send_envelope_wrapperr   r   rx   )rc   r   rW   r   r   ry   r5   )r!   r*   r   r4   r"   r   r#   r+     s    

zHttpTransport.capture_envelopec                    s8   t d |dkr4 j fdd  j|| d S )NzFlushing HTTP transportr   c                      s    j ddS )NT)r   )r   r"   r0   r"   r#   <lambda>      z%HttpTransport.flush.<locals>.<lambda>)r   r   rW   r   r/   r,   r"   r0   r#   r/     s    
zHttpTransport.flushc                 C   s   t d | j  d S )NzKilling HTTP transport)r   r   rW   r1   r0   r"   r"   r#   r1     s    
zHttpTransport.kill)NN)ru   N)Frl   )F)N)r9   r:   r;   r<   r$   r5   rt   r   r   r   r   r   r   r   r   r   ra   r)   r+   r/   r1   r"   r"   r"   r#   rQ      s.      
  
9

	6 
rQ   c                   @   s   e Zd Zdd Zdd ZdS )_FunctionTransportc                 C   s   t |  || _d S r6   )r   r$   _func)r!   funcr"   r"   r#   r$     s    
z_FunctionTransport.__init__c                 C   s   |  | d S r6   )r   r'   r"   r"   r#   r)     s    
z _FunctionTransport.capture_eventN)r9   r:   r;   r$   r)   r"   r"   r"   r#   r     s   r   c                 C   sb   | d }|d u rt }n8t|tr$|S t|tr>t|tr>|}nt|rNt|S | d r^|| S d S )N	transportr   )rQ   
isinstancer   r   
issubclasscallabler   )r   Zref_transportZtransport_clsr"   r"   r#   make_transport   s    
r   )N)3
__future__r   r   rZ   r   r   r_   r   r   collectionsr   Zsentry_sdk.utilsr   r   r   r	   Zsentry_sdk.workerr
   Zsentry_sdk.enveloper   r   r   Zsentry_sdk._typesr   typingr   r   r   r   r   r   r   r   r   Zurllib3.poolmanagerr   r   r   r   r   ZDataCategoryurllib.requestr   ImportErrorurllibobjectr   rP   rQ   r   r   r"   r"   r"   r#   <module>   sH   L
  m