#NEWLINE# def http_request( self , method , path , body = None , response_formatter = None , error_formatter = None , **params ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# if ( not self . _should_submit ( ) ) : #NEWLINE# #INDENT# raise HttpBackoff ( "Too many timeouts. Won't try again for {1} seconds." . format ( * self . _backoff_status ( ) ) ) #NEWLINE# #UNINDENT# if self . api_key : #NEWLINE# #INDENT# params [ 'api_key' ] = self . api_key #NEWLINE# #UNINDENT# if self . application_key : #NEWLINE# #INDENT# params [ 'application_key' ] = self . application_key #NEWLINE# #UNINDENT# url = ( '/api/%s/%s?%s' % ( self . api_version , path . lstrip ( '/' ) , urlencode ( params ) ) ) #NEWLINE# try : #NEWLINE# #INDENT# conn = self . http_conn_cls ( self . api_host , timeout = self . timeout ) #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# conn = self . http_conn_cls ( self . api_host ) #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# try : #NEWLINE# #INDENT# start_time = time . time ( ) #NEWLINE# try : #NEWLINE# #INDENT# conn . request ( method , url , body , headers ) #NEWLINE# #UNINDENT# except timeout_exceptions : #NEWLINE# #INDENT# self . _timeout_counter += 1 #NEWLINE# raise HttpTimeout ( ( '%s %s timed out after %d seconds.' % ( method , url , self . timeout ) ) ) #NEWLINE# #UNINDENT# except socket . error as e : #NEWLINE# #INDENT# raise ClientError ( ( 'Could not request %s %s%s: %s' % ( method , self . api_host , url , e ) ) ) #NEWLINE# #UNINDENT# self . _timeout_counter = 0 #NEWLINE# response = conn . getresponse ( ) #NEWLINE# duration = round ( ( ( time . time ( ) - start_time ) * 1000.0 ) , 4 ) #NEWLINE# log . info ( ( '%s %s %s (%sms)' % ( response . status , method , url , duration ) ) ) #NEWLINE# response_str = response . read ( ) #NEWLINE# if response_str : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# if is_p3k ( ) : #NEWLINE# #INDENT# response_obj = json . loads ( response_str . decode ( 'utf-8' ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# response_obj = json . loads ( response_str ) #NEWLINE# #UNINDENT# #UNINDENT# except ValueError : #NEWLINE# #INDENT# raise ValueError ( 'Invalid JSON response: {0}' . format ( response_str ) ) #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_obj is None ) and self . json_responses ) : #NEWLINE# #INDENT# response_obj = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# if ( self . json_responses or ( response_formatter is None ) ) : #NEWLINE# #INDENT# return response_obj #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return response_formatter ( response_obj ) #NEWLINE# #UNINDENT# #UNINDENT# finally : #NEWLINE# #INDENT# conn . close ( ) #NEWLINE# #UNINDENT# #UNINDENT# except ClientError as e : #NEWLINE# #INDENT# if self . swallow : #NEWLINE# #INDENT# log . error ( str ( e ) ) #NEWLINE# if ( self . json_responses or ( 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 self . swallow : #NEWLINE# #INDENT# for error in e . args [ 0 ] [ 'errors' ] : #NEWLINE# #INDENT# log . error ( str ( error ) ) #NEWLINE# #UNINDENT# if ( self . json_responses or ( method 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