o
    id(                     @   s   d dl Z d dlZd dlZd dlZd dlmZmZmZ d dlm	Z	m
Z
mZmZmZ d dlmZ d dlmZ d dlmZmZ edZG dd	 d	eZdS )
    N)_api_version_max_timeouts_backoff_period)ClientErrorApiErrorHttpBackoffHttpTimeoutApiNotInitialized)resolve_http_client)is_p3k)construct_urlnormalize_tagszdatadog.apic                   @   sh   e Zd ZdZeZeZdZdZdZdZ	e
dd Ze
							dddZe
d	d
 Ze
dd ZdS )	APIClientz_
    Datadog API client: format and submit API calls to Datadog.
    Embeds a HTTP client.
    Nr   Fc                 C   s   | j st | _ | j S )z6
        Getter for the embedded HTTP client.
        )_http_clientr
   )cls r   J/home/ubuntu/.local/lib/python3.10/site-packages/datadog/api/api_client.py_get_http_client%   s   zAPIClient._get_http_clientc
           "      K   s  zE|   s|  \}}t|ddlm}m}m}m}m}m	}m
}m}m}m} |du r0tdi }||d< |r<||d< |s@t}|rh|rhd|v r\|d D ]}|dd	d	krZ||d< qLn|dd	d	krh||d< d
|
v rt|
d
 trt|
d
 }d||
d
< d|
v rt|
d trddd |
d D |
d< t|trtj|| jd}d|d< |	rt|d}d|d< t|||}t }|  j||||
|||||d	}t t | d d}t!"d|j#|||f  d| _$|j%}|r1zt& rt'|(d}nt'|}W n t)y   t)d*|w t|tr0|r0d|v r0|r,|j#|v s0t+|nd}|dur<||}|rD||fW S |W S  t,yV   |  j$d7  _$  t-y }  z,|rt!.t/|  |du rzd| j0d iW  Y d} ~ S |d| j0d iW  Y d} ~ S  d} ~ w t+y }  z5|r| j0d dpg D ]}!t!.|! q|du r| j0d W  Y d} ~ S || j0d W  Y d} ~ S  d} ~ ww )a  
        Make an HTTP API request

        :param method: HTTP method to use to contact API endpoint
        :type method: HTTP method string

        :param path: API endpoint url
        :type path: url

        :param api_version: The API version used

        :param body: dictionary to be sent in the body of the request
        :type body: dictionary

        :param response_formatter: function to format JSON response from HTTP API request
        :type response_formatter: JSON input function

        :param error_formatter: function to format JSON error response from HTTP API request
        :type error_formatter: JSON input function

        :param attach_host_name: link the new resource object to the host name
        :type attach_host_name: bool

        :param suppress_response_errors_on_codes: suppress ApiError on `errors` key in the response for the given HTTP
                                                  status codes
        :type suppress_response_errors_on_codes: None|list(int)

        :param compress_payload: compress the payload using zlib
        :type compress_payload: bool

        :param params: dictionary to be sent in the query string of the request
        :type params: dictionary

        :returns: JSON or formatted response from HTTP API request
        r   )
_api_key_application_key	_api_host_mute
_host_name_proxies_max_retries_timeout_cacert_return_raw_responseNz9API key is not set. Please run 'initialize' method first.z
DD-API-KEYzDD-APPLICATION-KEYserieshost tags,monitor_idsc                 s   s    | ]}t |V  qd S )N)str).0ir   r   r   	<genexpr>   s    z#APIClient.submit.<locals>.<genexpr>)	sort_keyszapplication/jsonzContent-Typezutf-8deflatezContent-Encoding)	methodurlheadersparamsdatatimeoutmax_retriesproxiesverifyg     @@   z%s %s %s (%sms)zInvalid JSON response: {0}errors   )1_should_submit_backoff_statusr   datadog.apir   r   r   r   r   r   r   r   r   r   r	   r   get
isinstancelistr   joindictjsondumps
_sort_keyszlibcompressencoder   timer   requestroundloginfostatus_code_timeout_countercontentr   loadsdecode
ValueErrorformatr   r   r   errorr$   args)"r   r*   pathapi_versionbodyattach_host_nameresponse_formattererror_formatter!suppress_response_errors_on_codescompress_payloadr-   _backoff_time_leftr   r   r   r   r   r   r   r   r   r   r,   
obj_paramstag_listr+   
start_timeresultdurationrK   response_objerP   r   r   r   submit/   s   10





zAPIClient.submitc                 C   s   t   }d}| js| j| jkrtd| j || _d}|S | jrD|  \}}|dk r:td| d| _d| _d}|S td| d}|S d}|S )z
        Returns True if we're in a state where we should make a request
        (backoff expired, no backoff in effect), false otherwise.
        FzCMax number of datadog timeouts exceeded, backing off for %s secondsr   zHExiting backoff state after %s seconds, will try to submit metrics againNTz=In backoff state, won't submit metrics for another %s seconds)rD   _backoff_timestamprJ   r   rG   rH   r   r7   )r   nowshould_submitbacked_off_timer[   r   r   r   r6      s:   
zAPIClient._should_submitc                 C   s0   t   }|| j }| j| }t|dt|dfS )zN
        Get a backoff report, i.e. backoff total and remaining time.
           )rD   rd   r   rF   )r   re   rg   r[   r   r   r   r7     s   

zAPIClient._backoff_status)NNFNNNF)__name__
__module____qualname____doc__r   r   rd   rJ   r@   r   classmethodr   rc   r6   r7   r   r   r   r   r      s0    
	 ?
+r   )r>   loggingrD   rA   r8   r   r   r   datadog.api.exceptionsr   r   r   r   r	   datadog.api.http_clientr
   datadog.util.compatr   datadog.util.formatr   r   	getLoggerrG   objectr   r   r   r   r   <module>   s   
