o
    i.!                     @   s.  d Z ddlZddlZddlZddlZddlmZ z
ddlZddlZW n e	y-   dZY nw z
ddl
mZmZ W n e	yE   d\ZZY nw zddlZW n e	yW   dZY nw ddlmZmZmZmZ edZdd	 Zd
d ZG dd deZG dd deZG dd deZG dd deZdd ZdS )z
Available HTTP Client for Datadog API client.

Priority:
1. `requests` 3p module
2. `urlfetch` 3p module - Google App Engine only
    N)Lock)urlfetchurlfetch_errors)NN)
ProxyErrorClientError	HTTPErrorHttpTimeoutzdatadog.apic                  C   s4   ddl m}  dj| jt t  t  dS )Nr   versionz:datadogpy/{version} (python {pyver}; os {os}; arch {arch}))r
   pyverosarch)	datadogr
   format__version__platformpython_versionsystemlowermachiner	    r   K/home/ubuntu/.local/lib/python3.10/site-packages/datadog/api/http_client.py_get_user_agent_header*   s   

r   c                 C   s
   d| _ | S )zZPython3: remove context from chained exceptions to prevent leaking API keys in tracebacks.N)	__cause__)excr   r   r   _remove_context5   s   r   c                   @   s   e Zd ZdZedd ZdS )
HTTPClientz[
    An abstract generic HTTP client. Subclasses must implement the `request` methods.
    c
           
      C   s   t d)a  
        Main method to be implemented by HTTP clients.

        The returned data structure has the following fields:
        * `content`: string containing the response from the server
        * `status_code`: HTTP status code returned by the server

        Can raise the following exceptions:
        * `ClientError`: server cannot be contacted
        * `HttpTimeout`: connection timed out
        * `HTTPError`: unexpected HTTP response code
        z-Must be implemented by HTTPClient subclasses.)NotImplementedError)
clsmethodurlheadersparamsdatatimeoutproxiesverifymax_retriesr   r   r   request@   s   zHTTPClient.requestN)__name__
__module____qualname____doc__classmethodr(   r   r   r   r   r   ;   s    r   c                   @   s&   e Zd ZdZdZe Zedd ZdS )RequestClientz
    HTTP client based on 3rd party `requests` module, using a single session.
    This allows us to keep the session alive to spare some execution time.
    Nc
              
   C   sh  zJ| j * | jd u r't | _tjj|	d}
| jd|
 | jjdt	 i W d    n1 s1w   Y  | jj
||||||||d}|  W |S  tjjy` } ztt|||d }~w tjyt } ztt|||d }~w tjjy   tt||| tjjy } z|jjdv rn
tt|jj|jW Y d }~|S d }~w ty   tdw )N)r'   zhttps://
User-Agent)r!   r"   r#   r$   r%   r&   i  i  i  i  i  i  zYour installed version of `requests` library seems not compatible withDatadog's usage. We recommend upgrading it ('pip install -U requests').If you need help or have any question, please contact support@datadoghq.com)_session_lock_sessionrequestsSessionadaptersHTTPAdaptermountr!   updater   r(   raise_for_status
exceptionsr   r   ConnectionErrorr   Timeoutr   r   responsestatus_codereason	TypeError)r   r   r    r!   r"   r#   r$   r%   r&   r'   http_adapterresulter   r   r   r(   Z   sD   




zRequestClient.request)	r)   r*   r+   r,   r2   r   r1   r-   r(   r   r   r   r   r.   Q   s    r.   c                   @   s(   e Zd ZdZedd Zedd ZdS )URLFetchClientzC
    HTTP client based on Google App Engine `urlfetch` module.
    c
              
   C   s   |rdnd}
dj |t|d}t|}t |d< ztj||||
||dd}| | W |S  tj	yA } zt
|||d}~w tjyN   t|||w )zh
        Wrapper around `urlfetch.fetch` method.

        TO IMPLEMENT:
        * `max_retries`
        TFz{url}?{params})r    r"   r/   )r    r   r!   validate_certificatedeadlinepayloadfollow_redirectsN)r   urllib	urlencodecopydeepcopyr   r   fetchraise_on_statusDownloadErrorr   r   DeadlineExceededErrorr   )r   r   r    r!   r"   r#   r$   r%   r&   r'   rE   url_with_params
newheadersrB   rC   r   r   r   r(      s,   	

zURLFetchClient.requestc                 C   s*   |j }|d dkr|dv rdS t|dS )3
        Raise on HTTP status code errors.
        d      r0   N)r>   r   )r   rB   r>   r   r   r   rN      s   zURLFetchClient.raise_on_statusN)r)   r*   r+   r,   r-   r(   rN   r   r   r   r   rD      s    
%rD   c                   @   s2   e Zd ZdZdZe Zedd Zedd Z	dS )Urllib3Clientz:
    HTTP client based on 3rd party `urllib3` module.
    Nc
              
   C   s,  zB| j  | jdu rtj|	||rdndd| _W d   n1 s!w   Y  t|}
t |
d< | jj|||||
d}| | W |S  tj	j
yX } ztt
|||d}~w tj	jym } ztt|||d}~w tj	jy } ztt|||d}~w tj	jy } ztt|d}~ww )z
        Wrapper around `urllib3.PoolManager.request` method. This method will raise
        exceptions for HTTP status codes that are not 2xx.
        NCERT_REQUIRED	CERT_NONE)retriesr$   	cert_reqsr/   )bodyfieldsr!   )
_pool_lock_poolurllib3PoolManagerrK   rL   r   r(   rN   r:   r   r   MaxRetryErrorr   TimeoutErrorr   r   )r   r   r    r!   r"   r#   r$   r%   r&   r'   rR   r=   rC   r   r   r   r(      s<   




zUrllib3Client.requestc                 C   s2   |j }|dk s|dkr|dvrt||jdS dS )rS      i,  r0   N)statusr   r?   )r   r=   r>   r   r   r   rN      s   zUrllib3Client.raise_on_status)
r)   r*   r+   r,   r^   r   r]   r-   r(   rN   r   r   r   r   rV      s    
 rV   c                   C   sB   t r	td tS trtrtd tS trtd tS t	d)z]
    Resolve an appropriate HTTP client based the defined priority and user environment.
    z!Use `requests` based HTTP client.z!Use `urlfetch` based HTTP client.z Use `urllib3` based HTTP client.z[Datadog API client was unable to resolve a HTTP client.  Please install `requests` library.)
r3   logdebugr.   r   r   rD   r_   rV   ImportErrorr   r   r   r   resolve_http_client   s   


rh   )r,   rK   loggingr   rI   	threadingr   r3   requests.adaptersrg   google.appengine.apir   r   r_   datadog.api.exceptionsr   r   r   r   	getLoggerre   r   r   objectr   r.   rD   rV   rh   r   r   r   r   <module>   s>   
094