Skip to content

Instantly share code, notes, and snippets.

@mfojtik
Created February 16, 2011 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfojtik/829449 to your computer and use it in GitHub Desktop.
Save mfojtik/829449 to your computer and use it in GitHub Desktop.
Simple REST client to use with DeltaCloud API
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