Browse Source

initial commit

master
Brett Langdon 13 years ago
commit
a4fd05eab6
4 changed files with 102 additions and 0 deletions
  1. +3
    -0
      .gitignore
  2. +28
    -0
      README.md
  3. +59
    -0
      mongorest/__init__.py
  4. +12
    -0
      setup.py

+ 3
- 0
.gitignore View File

@ -0,0 +1,3 @@
*.egg-info
build
dist

+ 28
- 0
README.md View File

@ -0,0 +1,28 @@
mongorest
=========
This is just a toy module for creating a REST interface to MongoDB which uses `jsonschema`
to validate data before trying to save into MongoDB collection.
## Example
This example creates a REST server with the following routes:
* `GET` `/users/<string:id>` - return a single document from `users` as JSON or `204`
* `PUT` `/users/<string:id>` - `PUT` a JSON document into `users` or `400` if data does not validate with the schema
* `GET` `/users` - get all documents from `users` as JSON
```python
from mongorest import Server
app = Server('test', host='my.db.server', port=27017)
users_schema = {'type': 'object',
'properties': {
'name': {'type': 'string'},
'age': {'type': 'number',
'minimum': 0},
'admin': {'type': 'boolean'}},
'required': ['name', 'age', 'admin']}
app.register_collection('users', users_schema)
app.run()
```

+ 59
- 0
mongorest/__init__.py View File

@ -0,0 +1,59 @@
from bson.objectid import ObjectId
from flask import Flask, request
from flask.ext.restful import Resource, Api
from jsonschema import validate, ValidationError
from pymongo import MongoClient
def convert(docs):
for doc in docs:
doc['_id'] = str(doc['_id'])
yield doc
def generate_resource(collection, schema, db):
class CollectionResource(Resource):
def get(self, doc_id):
doc_id = ObjectId(doc_id)
doc = db[collection].find_one({'_id': doc_id})
if doc is not None:
doc['_id'] = str(doc['_id'])
return doc, 200
return '', 204
def put(self, doc_id):
try:
validate(request.json, schema)
doc = request.json
doc['_id'] = ObjectId(doc_id)
db[collection].save(doc)
except ValidationError, ve:
return str(ve), 400
return '', 200
return CollectionResource
def generate_list(collection, db):
class CollectionList(Resource):
def get(self):
docs = db[collection].find()
return list(convert(docs)), 200
return CollectionList
class Server(Api):
def __init__(self, database, host='127.0.0.1', port=27017):
self.app = Flask(__name__)
super(Server, self).__init__(self.app)
self.client = MongoClient(host, port)
self.db = self.client[database]
def register_collection(self, collection, schema):
collection_resource = generate_resource(collection, schema, self.db)
collection_list = generate_list(collection, self.db)
self.add_resource(collection_resource, '/%s/<string:doc_id>' % collection)
self.add_resource(collection_list, '/%s' % collection)
def run(self, *args, **kwargs):
self.app.run(*args, **kwargs)

+ 12
- 0
setup.py View File

@ -0,0 +1,12 @@
#!/usr/bin/env python
from setuptools import find_packages, setup
setup(name='Mongo REST',
version='0.1.0',
description='MongoDB REST Server',
author='Brett Langdon',
author_email='brett@blangdon.com',
url='http://ww.github.com/brettlangdon/mongo-rest',
install_requires=['pymongo', 'flask', 'flask-restful', 'jsonschema'],
packages=find_packages())

Loading…
Cancel
Save