<s> '''<STR_LIT>''' <EOL> import requests <EOL> import time <EOL> import json as json_module <EOL> API_ENDPOINT = '<STR_LIT>' <EOL> class VultrError ( RuntimeError ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> class VultrBase ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , api_key ) : <EOL> self . api_endpoint = API_ENDPOINT <EOL> self . api_key = api_key <EOL> self . set_requests_per_second ( <NUM_LIT:1> ) <EOL> def set_requests_per_second ( self , req_per_second ) : <EOL> '''<STR_LIT>''' <EOL> self . req_per_second = req_per_second <EOL> self . req_duration = <NUM_LIT:1> / self . req_per_second <EOL> def _request_get_helper ( self , url , params = None ) : <EOL> '''<STR_LIT>''' <EOL> if not isinstance ( params , dict ) : <EOL> params = dict ( ) <EOL> if self . api_key : <EOL> params [ '<STR_LIT>' ] = self . api_key <EOL> return requests . get ( url , params = params , timeout = <NUM_LIT> ) <EOL> def _request_post_helper ( self , url , params = None ) : <EOL> '''<STR_LIT>''' <EOL> if self . api_key : <EOL> query = { '<STR_LIT>' : self . api_key } <EOL> return requests . post ( url , params = query , data = params , timeout = <NUM_LIT> ) <EOL> def _request_helper ( self , url , params , method ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> if method == '<STR_LIT:POST>' : <EOL> return self . _request_post_helper ( url , params ) <EOL> elif method == '<STR_LIT:GET>' : <EOL> return self . _request_get_helper ( url , params ) <EOL> raise VultrError ( '<STR_LIT>' % method ) <EOL> except requests . RequestException as ex : <EOL> raise RuntimeError ( ex ) <EOL> def request ( self , path , params = None , method = '<STR_LIT:GET>' ) : <EOL> '''<STR_LIT>''' <EOL> _start = time . time ( ) <EOL> if not path . startswith ( '<STR_LIT:/>' ) : <EOL> path = '<STR_LIT:/>' + path <EOL> resp = self . _request_helper ( self . api_endpoint + path , params , method ) <EOL> if resp . status_code != <NUM_LIT:200> : <EOL> if resp . status_code == <NUM_LIT> : <EOL> raise VultrError ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> elif resp . status_code == <NUM_LIT> : <EOL> raise VultrError ( '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> elif resp . status_code == <NUM_LIT> : <EOL> raise VultrError ( '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> elif resp . status_code == <NUM_LIT> : <EOL> raise VultrError ( '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> resp . text ) <EOL> elif resp . status_code == <NUM_LIT> : <EOL> raise VultrError ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> elif resp . status_code == <NUM_LIT> : <EOL> raise VultrError ( '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> _elapsed = time . time ( ) - _start <EOL> if _elapsed < self . req_duration : <EOL> time . sleep ( self . req_duration - _elapsed ) <EOL> return resp . json ( ) if resp . text else json_module . loads ( '<STR_LIT:{}>' ) <EOL> def update_params ( params , updates ) : <EOL> '''<STR_LIT>''' <EOL> params = params . copy ( ) if isinstance ( params , dict ) else dict ( ) <EOL> params . update ( updates ) <EOL> return params </s>
