github.com/deis/deis@v1.13.5-0.20170519182049-1d9e59fbdbfc/contrib/linode/linodeapi.py (about) 1 #!/usr/bin/env python 2 """ 3 Provides a class for Linode API commands 4 5 Usage: used by other files as a base class 6 """ 7 import requests 8 import threading 9 from colorama import Fore, Style 10 11 12 class LinodeApiCommand: 13 def __init__(self, arguments): 14 self._arguments = vars(arguments) 15 self._linode_api_key = arguments.linode_api_key if arguments.linode_api_key is not None else '' 16 17 def __getattr__(self, name): 18 return self._arguments.get(name) 19 20 def request(self, action, **kwargs): 21 data = '' 22 if self._linode_api_key: 23 kwargs['params'] = dict({'api_key': self._linode_api_key, 'api_action': action}.items() + kwargs.get('params', {}).items()) 24 response = requests.request('get', 'https://api.linode.com/api/', **kwargs) 25 26 json = response.json() 27 errors = json.get('ERRORARRAY', []) 28 data = json.get('DATA') 29 30 if len(errors) > 0: 31 raise IOError(str(errors)) 32 else: 33 self.info('Linode api key not provided. Please provide at the start of script to perform this function.') 34 35 return data 36 37 38 def run(self): 39 raise NotImplementedError 40 41 def info(self, message): 42 print(Fore.MAGENTA + threading.current_thread().name + ': ' + Fore.CYAN + message + Fore.RESET) 43 44 def success(self, message): 45 print(Fore.MAGENTA + threading.current_thread().name + ': ' + Fore.GREEN + message + Fore.RESET)