Flask restful API framework for MongoDB/MongoEngine
Project description
Flask-CuddlyRest
================
A framework for manipulating mongoengine collections via a CUD-ly API
It has taken inspiration from:
- https://github.com/brettlangdon/mongorest
- https://github.com/elasticsales/flask-mongorest
- https://github.com/mozilla-services/cornice (for sphinx integration)
All credit goes to those projects! :)
[![Build Status](https://travis-ci.org/wuurrd/Flask-CuddlyRest.png)](https://travis-ci.org/wuurrd/Flask-CuddlyRest)
Setup
=====
``` python
from flask import Flask
from flask.ext.mongoengine import MongoEngine
from flask.ext.cuddlyrest import CuddlyRest
from flask.ext.cuddlyrest.views import Resource
app = Flask(__name__)
app.config.update(
MONGODB_HOST = 'localhost',
MONGODB_PORT = '27017',
MONGODB_DB = 'mongorest_example_app',
)
db = MongoEngine(app)
api = CuddlyRest(app)
class User(db.Document):
email = db.EmailField(unique=True, required=True)
class Content(db.EmbeddedDocument):
text = db.StringField()
class Post(db.Document):
title = db.StringField(max_length=120, required=True)
author = db.ReferenceField(User)
content = db.EmbeddedDocumentField(Content)
api.register(Post, '/posts')
```
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": "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%20post
{
"data": [
{
"id": "1",
"title": "First post!",
"author_id": "author_id_from_a_previous_api_call",
"content": {
"text": "this is our test post content"
}
},
... other posts
]
}
```
Delete a Post:
```
curl -X DELETE http://0.0.0.0:5000/posts/1/
```
Request Params
==============
**skip** and **limit** => utilize the built-in functions of mongodb.
**order_by** => order results if this string is present in the Resource.allowed_ordering list.
Sphinx doc generation
=====================
There are facilities to generate sphinx documentation that document the REST
APIs as handled by Flask-CuddlyREST.
Setup
-----
Edit `conf.py` and add the following:
```
import flask_cuddlyrest
```
Make sure `flask_cuddlyrest.ext.sphinxext` is added to the list of extensions, for instance:
```
extensions = ['sphinx.ext.autodoc',
'flask_cuddlyrest.ext.sphinxext']
```
Usage
-----
To add a description of the operations that Flask-CuddlyREST handles:
```
.. cuddlyrest::
:document: Sock # The name of the class where the mongo document
binding is defined.
:url: /socks # the url on which the document object is served.
```
To add a description of the JSON objects Flask-CuddlyREST handles:
```
.. cuddlyobject::
:module: the.module.in.which.the.document.is.defined
:document: Sock # The name of the class where the mongo document
binding is defined.
:id: sock_class # the named hyperlink reference id which the
rest of the API doc can use to reference this generated
documentation block. This is optional: when not provided, the
default value will be set to the document name, lower cased.
```
================
A framework for manipulating mongoengine collections via a CUD-ly API
It has taken inspiration from:
- https://github.com/brettlangdon/mongorest
- https://github.com/elasticsales/flask-mongorest
- https://github.com/mozilla-services/cornice (for sphinx integration)
All credit goes to those projects! :)
[![Build Status](https://travis-ci.org/wuurrd/Flask-CuddlyRest.png)](https://travis-ci.org/wuurrd/Flask-CuddlyRest)
Setup
=====
``` python
from flask import Flask
from flask.ext.mongoengine import MongoEngine
from flask.ext.cuddlyrest import CuddlyRest
from flask.ext.cuddlyrest.views import Resource
app = Flask(__name__)
app.config.update(
MONGODB_HOST = 'localhost',
MONGODB_PORT = '27017',
MONGODB_DB = 'mongorest_example_app',
)
db = MongoEngine(app)
api = CuddlyRest(app)
class User(db.Document):
email = db.EmailField(unique=True, required=True)
class Content(db.EmbeddedDocument):
text = db.StringField()
class Post(db.Document):
title = db.StringField(max_length=120, required=True)
author = db.ReferenceField(User)
content = db.EmbeddedDocumentField(Content)
api.register(Post, '/posts')
```
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": "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%20post
{
"data": [
{
"id": "1",
"title": "First post!",
"author_id": "author_id_from_a_previous_api_call",
"content": {
"text": "this is our test post content"
}
},
... other posts
]
}
```
Delete a Post:
```
curl -X DELETE http://0.0.0.0:5000/posts/1/
```
Request Params
==============
**skip** and **limit** => utilize the built-in functions of mongodb.
**order_by** => order results if this string is present in the Resource.allowed_ordering list.
Sphinx doc generation
=====================
There are facilities to generate sphinx documentation that document the REST
APIs as handled by Flask-CuddlyREST.
Setup
-----
Edit `conf.py` and add the following:
```
import flask_cuddlyrest
```
Make sure `flask_cuddlyrest.ext.sphinxext` is added to the list of extensions, for instance:
```
extensions = ['sphinx.ext.autodoc',
'flask_cuddlyrest.ext.sphinxext']
```
Usage
-----
To add a description of the operations that Flask-CuddlyREST handles:
```
.. cuddlyrest::
:document: Sock # The name of the class where the mongo document
binding is defined.
:url: /socks # the url on which the document object is served.
```
To add a description of the JSON objects Flask-CuddlyREST handles:
```
.. cuddlyobject::
:module: the.module.in.which.the.document.is.defined
:document: Sock # The name of the class where the mongo document
binding is defined.
:id: sock_class # the named hyperlink reference id which the
rest of the API doc can use to reference this generated
documentation block. This is optional: when not provided, the
default value will be set to the document name, lower cased.
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Flask-CuddlyRest-0.1.15.tar.gz
(11.1 kB
view details)
File details
Details for the file Flask-CuddlyRest-0.1.15.tar.gz
.
File metadata
- Download URL: Flask-CuddlyRest-0.1.15.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7da20a407defe257c08c336e23b49476f1056ef534fa46f66446dc744478b09 |
|
MD5 | faa19b990d36a9c013d8682d88cf392f |
|
BLAKE2b-256 | 158c436acabbde7c7e202d77a957ea2ca41f875975b9981c4e7c1747f147b2e5 |