Created
September 3, 2010 09:42
-
-
Save yuttie/563687 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
| """ CouchDB Wrapper Class | |
| This is based on an example wrapper class found in | |
| http://wiki.apache.org/couchdb/Getting_started_with_Python. | |
| TODO: | |
| * Support Keep-Alive | |
| """ | |
| import httplib | |
| import json | |
| class Couch: | |
| """Basic wrapper class for operations on a couchDB""" | |
| def __init__(self, host, port=5984, options=None): | |
| self.host = host | |
| self.port = port | |
| def connect(self): | |
| return httplib.HTTPConnection(self.host, self.port) # No close() | |
| # Database operations | |
| def createDb(self, dbName): | |
| """Creates a new database on the server""" | |
| res = self.put('/{0}/'.format(dbName), "") | |
| success = 200 <= res.status < 300 | |
| return (success, json.loads(res.read())) | |
| def deleteDb(self, dbName): | |
| """Deletes the database on the server""" | |
| res = self.delete('/{0}/'.format(dbName)) | |
| success = 200 <= res.status < 300 | |
| return (success, json.loads(res.read())) | |
| def listDb(self): | |
| """List the databases on the server""" | |
| res = self.get('/_all_dbs') | |
| return json.loads(res.read()) | |
| def infoDb(self, dbName): | |
| """Returns info about the couchDB""" | |
| res = self.get('/{0}/'.format(dbName)) | |
| success = 200 <= res.status < 300 | |
| return (success, json.loads(res.read())) | |
| def listDocs(self, dbName): | |
| """List all documents in a given database""" | |
| res = self.get('/{0}/_all_docs'.format(dbName)) | |
| success = 200 <= res.status < 300 | |
| return (success, json.loads(res.read())['rows']) | |
| # Document operations | |
| def readDoc(self, dbName, docId): | |
| """Open a document in a given database""" | |
| res = self.get('/{0}/{1}'.format(dbName, docId)) | |
| if res.status >= 300: | |
| return None | |
| else: | |
| return res.read() | |
| def writeDoc(self, body, dbName, docId=None, rev=None): | |
| """Create/update a document in a given database""" | |
| if docId: | |
| uri = '/{0}/{1}'.format(dbName, docId) | |
| if rev: uri += '?={0}'.format(rev) | |
| res = self.put(uri, body) | |
| else: | |
| res = self.post('/{0}/'.format(dbName), body) | |
| success = 200 <= res.status < 300 | |
| return (success, json.loads(res.read())) | |
| def deleteDoc(self, dbName, docId, rev): | |
| """Delete a document of a given revision in a given database""" | |
| res = self.delete('/{0}/{1}?rev={2}'.format(dbName, docId, rev)) | |
| success = 200 <= res.status < 300 | |
| return (success, json.loads(res.read())) | |
| # Basic http methods | |
| def get(self, uri): | |
| c = self.connect() | |
| headers = {"Accept": "application/json"} | |
| c.request("GET", uri, None, headers) | |
| return c.getresponse() | |
| def post(self, uri, body): | |
| c = self.connect() | |
| headers = {"Content-type": "application/json"} | |
| c.request('POST', uri, body, headers) | |
| return c.getresponse() | |
| def put(self, uri, body): | |
| c = self.connect() | |
| if len(body) > 0: | |
| headers = {"Content-type": "application/json"} | |
| c.request("PUT", uri, body, headers) | |
| else: | |
| c.request("PUT", uri, body) | |
| return c.getresponse() | |
| def delete(self, uri): | |
| c = self.connect() | |
| c.request("DELETE", uri) | |
| return c.getresponse() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment