#NEWLINE# @ classmethod #NEWLINE# def do_request( self , method , url , * args , **kwargs ) : #NEWLINE# #INDENT# auth_token = kwargs . get ( 'token' , None ) #NEWLINE# params = kwargs . get ( 'params' , None ) #NEWLINE# headers = kwargs . get ( 'headers' , None ) #NEWLINE# proxy = kwargs . get ( 'proxy' , None ) #NEWLINE# if ( not auth_token ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# config = ConfigParser . ConfigParser ( ) #NEWLINE# config . read ( os . path . expanduser ( '~/.do.cfg' ) ) #NEWLINE# auth_token = ( config . get ( 'docli' , 'auth_token' ) or os . getenv ( 'do_auth_token' ) ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# auth_token = None #NEWLINE# #UNINDENT# #UNINDENT# if ( not auth_token ) : #NEWLINE# #INDENT# data = { #NEWLINE# #INDENT# 'has_error' : True , #NEWLINE# 'error_message' : 'Authentication token not provided.' , #NEWLINE# #UNINDENT# } #NEWLINE# return data #NEWLINE# #UNINDENT# if headers : #NEWLINE# #INDENT# headers . update ( { #NEWLINE# #INDENT# 'Authorization' : ( 'Bearer ' + auth_token ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# headers = { #NEWLINE# #INDENT# 'Authorization' : ( 'Bearer ' + auth_token ) , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# if proxy : #NEWLINE# #INDENT# proxy = { #NEWLINE# #INDENT# 'http' : proxy , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# request_method = { #NEWLINE# #INDENT# 'GET' : requests . get , #NEWLINE# 'POST' : requests . post , #NEWLINE# 'PUT' : requests . put , #NEWLINE# 'DELETE' : requests . delete , #NEWLINE# #UNINDENT# } #NEWLINE# request_url = ( self . api_url + url ) #NEWLINE# req = request_method [ method ] #NEWLINE# try : #NEWLINE# #INDENT# res = req ( request_url , headers = headers , params = params , proxies = proxy ) #NEWLINE# #UNINDENT# except ( requests . exceptions . ConnectionError , requests . exceptions . RequestException ) as e : #NEWLINE# #INDENT# data = { #NEWLINE# #INDENT# 'has_error' : True , #NEWLINE# 'error_message' : data . message , #NEWLINE# #UNINDENT# } #NEWLINE# return data #NEWLINE# #UNINDENT# if ( res . status_code == 204 ) : #NEWLINE# #INDENT# data = { #NEWLINE# #INDENT# 'has_error' : False , #NEWLINE# 'error_message' : '' , #NEWLINE# #UNINDENT# } #NEWLINE# return data #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# data = res . json ( ) #NEWLINE# data . update ( { #NEWLINE# #INDENT# 'has_error' : False , #NEWLINE# 'error_message' : '' , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# except ValueError as e : #NEWLINE# #INDENT# msg = ( 'Cannot read response, %s' % e . message ) #NEWLINE# data = { #NEWLINE# #INDENT# 'has_error' : True , #NEWLINE# 'error_message' : msg , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# if ( not res . ok ) : #NEWLINE# #INDENT# msg = data [ 'message' ] #NEWLINE# data . update ( { #NEWLINE# #INDENT# 'has_error' : True , #NEWLINE# 'error_message' : msg , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# return data