Skip to content

Instantly share code, notes, and snippets.

@ojii
Created January 19, 2011 15:18
Show Gist options
  • Save ojii/786294 to your computer and use it in GitHub Desktop.
Save ojii/786294 to your computer and use it in GitHub Desktop.
import urllib2
import urllib
class Rest(object):
def __init__(self, base):
self.base = base
self.opener = urllib2.build_opener(urllib2.HTTPHandler)
def get(self, path):
return urllib2.urlopen(self.base + path).read()
def post(self, path, **data):
try:
return urllib2.urlopen(self.base + path, urllib.urlencode(data)).read()
except Exception, e:
return e.read()
def delete(self, path, pk):
request = urllib2.Request(self.base + path + str(pk) + '/')
request.get_method = lambda: 'DELETE'
try:
return self.opener.open(request).read()
except Exception, e:
return e.read()
def put(self, path, pk, **data):
request = urllib2.Request(self.base + path + str(pk) + '/', urllib.urlencode(data))
request.get_method = lambda: 'PUT'
try:
return self.opener.open(request).read()
except Exception, e:
return e.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment