Last active
August 29, 2015 14:07
-
-
Save jgillmanjr/fb4431bec403de48cf22 to your computer and use it in GitHub Desktop.
Requests Exception Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from snakeStorm import snakeStorm | |
con = snakeStorm('validuser', 'validpass', 'storm/server/list', version = 'bleed') | |
con.changeBase('https://test-api-server.example.com', 20900) | |
print con.request() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
snakeStorm Storm API Library | |
An attempt at a psuedo-clean room implementation of a Storm API Library to learn python | |
Jason Gillman Jr. | |
""" | |
import requests | |
import json | |
def listApiMethods(apiVersion = 'v1'): | |
methodList = {} | |
apiDocs = requests.request('GET', 'https://www.stormondemand.com/api/docs/' + apiVersion + '/docs.json').json() | |
for (groupName,group) in apiDocs.iteritems(): | |
methodList[groupName] = [] | |
for (methodName, methodSpecs) in group['__methods'].iteritems(): | |
methodList[groupName].append(methodName) | |
return methodList | |
class snakeStorm: | |
def __init__(self, username, password, method, parameters = {}, version = 'v1'): | |
self.username = username | |
self.password = password | |
self.method = method | |
self.parameters = parameters | |
self.version = version | |
self.verify = True | |
#self.baseURI = 'https://api.stormondemand.com' | |
self.baseURI = 'https://api-public.jgillman.dev.liquidweb.com' | |
self.apiPort = 20900 #443 | |
self.apiFormat = 'json' | |
self.fullURI = '%s:%s/%s/%s.%s' % (self.baseURI, str(self.apiPort), self.version, self.method, self.apiFormat) | |
## Request specific variables ## | |
self.lastCall = None # Store the result of the last Storm API Call here | |
self.postData = {'params': None} # What will get sent if actually passing params | |
## Local Parameter Methods ## | |
def addParams(self, **params): | |
""" Add parameters. If a parameter is already set, it will be overwritten. """ | |
for (key,value) in params.iteritems(): | |
self.parameters[key] = value | |
def clearParams(self): | |
""" Remove all set parameters """ | |
self.parameters = {} | |
def listParams(self): | |
""" A holdover from me being used to having variable visibility """ | |
return self.parameters | |
def removeParams(self, *params): | |
""" Remove specific parameters by key """ | |
for key in params: | |
if self.parameters.has_key(key): | |
del self.parameters[key] | |
## API Interaction Methods ## | |
def request(self): | |
""" Send the request to the Storm API """ | |
## Do we have params or not? ## | |
if len(self.parameters) > 0: | |
self.postData['params'] = self.parameters | |
self.lastCall = requests.post(self.fullURI, data = json.dumps(self.postData), auth = (self.username, self.password), verify = self.verify).json() | |
else: | |
try: | |
self.postData['params'] = None | |
self.lastCall = requests.request('GET', self.fullURI, auth = (self.username, self.password), verify = self.verify).json() | |
except requests.exceptions.SSLError as e: | |
return e | |
return self.lastCall # For immediate usage | |
## Misc. Methods ## | |
def changeBase(self, baseURI = 'https://api.stormondemand.com', apiPort = 443): | |
""" You probably won't need this method... """ | |
self.baseURI = baseURI | |
self.apiPort = apiPort | |
self.fullURI = '%s:%s/%s/%s.%s' % (self.baseURI, str(self.apiPort), self.version, self.method, self.apiFormat) # Update the fullURI |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment