#NEWLINE# @ classmethod #NEWLINE# def request( cls , method , path , body = None , attach_host_name = False , response_formatter = None , error_formatter = None , **params ) : #NEWLINE# #INDENT# '\n        Make an HTTP API request\n\n        :param method: HTTP method to use to contact API endpoint\n        :type method: HTTP method string\n\n        :param path: API endpoint url\n        :type path: url\n\n        :param body: dictionary to be sent in the body of the request\n        :type body: dictionary\n\n        :param response_formatter: function to format JSON response from HTTP API request\n        :type response_formatter: JSON input function\n\n        :param error_formatter: function to format JSON error response from HTTP API request\n        :type error_formatter: JSON input function\n\n        :param attach_host_name: link the new resource object to the host name\n        :type attach_host_name: bool\n\n        :param params: dictionary to be sent in the query string of the request\n        :type params: dictionary\n\n        :returns: JSON or formated response from HTTP API request\n        ' #NEWLINE# try : #NEWLINE# #INDENT# if ( not cls . _should_submit ( ) ) : #NEWLINE# #INDENT# raise HttpBackoff ( "Too many timeouts. Won't try again for {1} seconds." . format ( * cls . _backoff_status ( ) ) ) #NEWLINE# #UNINDENT# from datadog.api import _api_key , _application_key , _api_host , _mute , _host_name , _proxies , _max_retries , _timeout , _cacert #NEWLINE# if ( _api_key is None ) : #NEWLINE# #INDENT# raise ApiNotInitialized ( "API key is not set. Please run 'initialize' method first." ) #NEWLINE# #UNINDENT# params [ 'api_key' ] = _api_key #NEWLINE# if _application_key : #NEWLINE# #INDENT# params [ 'application_key' ] = _application_key #NEWLINE# #UNINDENT# url = ( '%s/api/%s/%s' % ( _api_host , cls . _api_version , path . lstrip ( '/' ) ) ) #NEWLINE# if ( attach_host_name and body ) : #NEWLINE# #INDENT# if ( 'series' in body ) : #NEWLINE# #INDENT# for obj_params in body [ 'series' ] : #NEWLINE# #INDENT# if ( obj_params . get ( 'host' , '' ) == '' ) : #NEWLINE# #INDENT# obj_params [ 'host' ] = _host_name #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( body . get ( 'host' , '' ) == '' ) : #NEWLINE# #INDENT# body [ 'host' ] = _host_name #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( ( 'tags' in params ) and isinstance ( params [ 'tags' ] , list ) ) : #NEWLINE# #INDENT# params [ 'tags' ] = ',' . join ( params [ 'tags' ] ) #NEWLINE# #UNINDENT# headers = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# if isinstance ( body , dict ) : #NEWLINE# #INDENT# body = json . dumps ( body ) #NEWLINE# headers [ 'Content-Type' ] = 'application/json' #NEWLINE# #UNINDENT# start_time = time . time ( ) #NEWLINE# try : #NEWLINE# #INDENT# s = requests . Session ( ) #NEWLINE# http_adapter = requests . adapters . HTTPAdapter ( max_retries = _max_retries ) #NEWLINE# s . mount ( 'https://' , http_adapter ) #NEWLINE# result = s . request ( method , url , headers = headers , params = start_time , data = body , timeout = _timeout , proxies = _proxies , verify = _cacert ) #NEWLINE# result . raise_for_status ( ) #NEWLINE# #UNINDENT# except requests . ConnectionError as e : #NEWLINE# #INDENT# raise ClientError ( ( 'Could not request %s %s%s: %s' % ( method , _api_host , url , e ) ) ) #NEWLINE# #UNINDENT# except requests . exceptions . Timeout as e : #NEWLINE# #INDENT# cls . _timeout_counter += 1 #NEWLINE# raise HttpTimeout ( ( '%s %s timed out after %d seconds.' % ( method , url , _timeout ) ) ) #NEWLINE# #UNINDENT# except requests . exceptions . HTTPError as e : #NEWLINE# #INDENT# if ( e . response . status_code in ( 400 , 403 , 404 , 409 ) ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# #UNINDENT# except TypeError as e : #NEWLINE# #INDENT# raise TypeError ( "Your installed version of 'requests' library seems not compatible withDatadog's usage. We recommand upgrading it ('pip install -U requests').If you need help or have any question, please contact support@datadoghq.com" ) #NEWLINE# #UNINDENT# duration = round ( ( ( time . time ( ) - start_time ) * 1000.0 ) , 4 ) #NEWLINE# log . info ( ( '%s %s %s (%sms)' % ( result . status_code , method , url , duration ) ) ) #NEWLINE# cls . _timeout_counter = 0 #NEWLINE# content = result . content #NEWLINE# if content : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# if is_p3k ( ) : #NEWLINE# #INDENT# response_obj = json . loads ( content . decode ( 'utf-8' ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# response_obj = json . loads ( content ) #NEWLINE# #UNINDENT# #UNINDENT# except ValueError : #NEWLINE# #INDENT# raise ValueError ( 'Invalid JSON response: {0}' . format ( content ) ) #NEWLINE# #UNINDENT# if ( response_obj and ( 'errors' in response_obj ) ) : #NEWLINE# #INDENT# raise ApiError ( response_obj ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# response_obj = None #NEWLINE# #UNINDENT# if ( response_formatter is None ) : #NEWLINE# #INDENT# return response_obj #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return response_formatter ( response_obj ) #NEWLINE# #UNINDENT# #UNINDENT# except ClientError as e : #NEWLINE# #INDENT# if _mute : #NEWLINE# #INDENT# log . error ( str ( e ) ) #NEWLINE# if ( error_formatter is None ) : #NEWLINE# #INDENT# return { #NEWLINE# #INDENT# 'errors' : e . args [ 0 ] , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return error_formatter ( { #NEWLINE# #INDENT# 'errors' : e . args [ 0 ] , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# #UNINDENT# except ApiError as e : #NEWLINE# #INDENT# if _mute : #NEWLINE# #INDENT# for error in e . args [ 0 ] [ 'errors' ] : #NEWLINE# #INDENT# log . error ( str ( error ) ) #NEWLINE# #UNINDENT# if ( error_formatter is None ) : #NEWLINE# #INDENT# return e . args [ 0 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return error_formatter ( e . args [ 0 ] ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise