Created
February 16, 2011 14:26
-
-
Save mfojtik/829449 to your computer and use it in GitHub Desktop.
Simple REST client to use with DeltaCloud API
This file contains hidden or 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 httplib2 import Http | |
from urllib import urlencode | |
import libxml2 | |
class RestClient: | |
"""A simple REST client library""" | |
def __init__(self, api_url, api_user, api_password): | |
self.url, self.user, self.password = api_url, api_user, api_password | |
self.client = Http() | |
self.client.follow_all_redirect = True | |
self.client.add_credentials(self.user, self.password) | |
def GET(self, uri): | |
status, response = self.client.request('{url}{uri}'.format(url=self.url, uri=uri), 'GET', headers={'accept':'application/xml'}) | |
response = self.parse_xml(response) | |
return status, response | |
def POST(self, uri, params={}): | |
if not params: | |
params = {} | |
status, response = self.client.request('{url}{uri}'.format(url=self.url, uri=uri), 'POST', | |
urlencode(params), headers={'accept':'application/xml'}) | |
response = self.parse_xml(response) | |
return status, response | |
def DELETE(self, uri): | |
return self.client.request('{url}{uri}'.format(url=self.url, uri=uri), 'DELETE') | |
def PUT(self, uri): | |
return self.client.request('{url}{uri}'.format(url=self.url, uri=uri), 'PUT') | |
def parse_xml(self, response): | |
return libxml2.parseDoc(response) | |
restclient = RestClient('http://10.34.37.122:3002/api', 'mockuser', 'mockpassword') | |
# List all images | |
# | |
status, content = restclient.GET('/images') | |
print content | |
# Create new instance | |
# | |
status, content = restclient.POST('/instances', { 'image_id':'img1' }) | |
print content | |
# Stop running instance | |
# | |
# status, content = restclient.POST('/instances/inst7/stop') | |
# print status | |
# print content | |
# Destroy instance | |
# | |
#status, content = restclient.DELETE('/instances/inst5') | |
#print status | |
#print content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment