Created
November 21, 2013 20:20
-
-
Save charlesmcchan/7588896 to your computer and use it in GitHub Desktop.
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
import httplib | |
import json | |
class REST(object): | |
def __init__(self, server): | |
self.server = server | |
def get(self, path): | |
ret = self.rest_call(path, {}, 'GET') | |
return json.loads(ret[2]) | |
def post(self, path, data): | |
ret = self.rest_call(path, data, 'POST') | |
return ret[0] == 200 | |
def delete(self, path, data): | |
ret = self.rest_call(path, data, 'DELETE') | |
return ret[0] == 200 | |
def rest_call(self, path, data, action): | |
headers = { | |
'Content-type': 'application/json', | |
'Accept': 'application/json', | |
} | |
body = json.dumps(data) | |
conn = httplib.HTTPConnection(self.server, 8080) | |
conn.request(action, path, body, headers) | |
response = conn.getresponse() | |
ret = (response.status, response.reason, response.read()) | |
conn.close() | |
return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment