Flask-MongoRest

Flask restful API framework for MongoDB/MongoEngine
Download

Flask-MongoRest Ranking & Summary

Advertisement

  • Rating:
  • License:
  • BSD License
  • Price:
  • FREE
  • Publisher Name:
  • Anthony Nemitz
  • Publisher web site:
  • http://github.com/elasticsales/

Flask-MongoRest Tags


Flask-MongoRest Description

Flask-MongoRest is a restful API framework for MongoDB/MongoEngine.Setupfrom flask import Flaskfrom flask.ext.mongoengine import MongoEnginefrom flask.ext.mongorest import MongoRestfrom flask.ext.mongorest.views import ResourceViewfrom flask.ext.mongorest.resources import Resourcefrom flask.ext.mongorest import operators as opsfrom flask.ext.mongorest import methods app = Flask(__name__)app.config.update( MONGODB_HOST = 'localhost', MONGODB_PORT = '27017', MONGODB_DB = 'mongorest_example_app',)db = MongoEngine(app)api = MongoRest(app)class User(db.Document): email = db.EmailField(unique=True, required=True)class Content(db.EmbeddedDocument): text = db.StringField()class ContentResource(Resource): document = Contentclass Post(db.Document): title = db.StringField(max_length=120, required=True) author = db.ReferenceField(User) content = db.EmbeddedDocumentField(Content)class PostResource(Resource): document = Post related_resources = { 'content': ContentResource, } filters = { 'title': , 'author_id': , } rename_fields = { 'author': 'author_id', }@api.register(name='posts', url='/posts/')class PostView(ResourceView): resource = PostResource methods = With this app, following cURL commands could be used:Create a Post:curl -H "Content-Type: application/json" -X POST -d \'{"title": "First post!", "author_id": "author_id_from_a_previous_api_call", "content": {"text": "this is our test post content"}}' http://0.0.0.0:5000/posts/{ "id": "1", "title": "First post!", "author_id": "author_id_from_a_previous_api_call", "content": { "text": "this is our test post content" }} Get a Post:curl http://0.0.0.0:5000/posts/1/{ "id": "1", "title": "First post!", "author_id": "author_id_from_a_previous_api_call", "content": { "text": "this is our test post content" }} List all Posts or filter by the title:curl http://0.0.0.0:5000/posts/ or curl http://0.0.0.0:5000/posts/?title__startswith=First post{ "data": }Delete a Post:curl -X DELETE http://0.0.0.0:5000/posts/1/# Fails since PostView.methods does not allow DeleteRequest Params_skip and _limit => utilize the built-in functions of mongodb._fields => limit the response's fields to those named here (comma separated)._order_by => order results if this string is present in the Resource.allowed_ordering list.Resource Configurationrename_fields => dict of renaming rules. Useful for mapping _id fields such as "organization": "organization_id"filters => filter results of a List request using the allowed filters which are used like /user/?id__gt=2 or /user/?email__exact=a@b.comrelated_resources => nested resource serialization for reference/embedded fields of a documentchild_document_resources => Suppose you have a Person base class which has Male and Female subclasses. These subclasses and their respective resources share the same MongoDB collection, but have different fields and serialization characteristics. This dictionary allows you to map class instances to their respective resources to be used during serialization.AuthenticationThe AuthenticationBase class provides the ability for application's to implement their own API auth. Two common patterns are shown below along with a BaseResourceView which can be used as the parent View of all of your app's resources.class SessionAuthentication(AuthenticationBase): def authorized(self): return current_user.is_authenticated()class ApiKeyAuthentication(AuthenticationBase): """ @TODO ApiKey document and key generation left to the specific implementation """ def authorized(self): if 'AUTHORIZATION' in request.headers: authorization = request.headers.split() if len(authorization) == 2 and authorization.lower() == 'basic': try: authorization_parts = base64.b64decode(authorization).partition(':') key = smart_unicode(authorization_parts) api_key = ApiKey.objects.get(key__exact=key) if api_key.user: login_user(api_key.user) setattr(current_user, 'api_key', api_key) return True except (TypeError, UnicodeDecodeError, ApiKey.DoesNotExist): pass return Falseclass BaseResourceView(ResourceView): authentication_methods = Running the test suiteThis package uses nosetests for automated testing. Just run python setup.py nosetests to run the tests. No setup or any other prep needed.ContributingPull requests are greatly appreciated!Product's homepage


Flask-MongoRest Related Software