Skip to content

Commit 27c7ad6

Browse files
committed
copy-paste due to no includes in readme github/markup#172
1 parent 5ae8d20 commit 27c7ad6

File tree

1 file changed

+69
-8
lines changed

1 file changed

+69
-8
lines changed

README.rst

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,76 @@ Install
2323
pip install Flask-COMBO-JSONAPI
2424

2525

26-
Example?
27-
28-
.. include:: docs/minimal_api_head.rst
29-
30-
31-
Literal include minimal api example?
26+
A minimal API
27+
=============
3228

33-
.. literalinclude:: ./examples/api_minimal.py
34-
:language: python
29+
.. code-block:: python
30+
31+
from flask import Flask
32+
from flask_combo_jsonapi import Api, ResourceDetail, ResourceList
33+
from flask_sqlalchemy import SQLAlchemy
34+
from marshmallow_jsonapi.flask import Schema
35+
from marshmallow_jsonapi import fields
36+
37+
# Create the Flask application and the Flask-SQLAlchemy object.
38+
app = Flask(__name__)
39+
app.config['DEBUG'] = True
40+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
41+
db = SQLAlchemy(app)
42+
43+
# Create model
44+
class Person(db.Model):
45+
id = db.Column(db.Integer, primary_key=True)
46+
name = db.Column(db.String)
47+
48+
# Create the database.
49+
db.create_all()
50+
51+
# Create schema
52+
class PersonSchema(Schema):
53+
class Meta:
54+
type_ = 'person'
55+
self_view = 'person_detail'
56+
self_view_kwargs = {'id': '<id>'}
57+
self_view_many = 'person_list'
58+
59+
id = fields.Integer(as_string=True, dump_only=True)
60+
name = fields.Str()
61+
62+
# Create resource managers
63+
class PersonList(ResourceList):
64+
schema = PersonSchema
65+
data_layer = {'session': db.session,
66+
'model': Person}
67+
68+
class PersonDetail(ResourceDetail):
69+
schema = PersonSchema
70+
data_layer = {'session': db.session,
71+
'model': Person}
72+
73+
# Create the API object
74+
api = Api(app)
75+
api.route(PersonList, 'person_list', '/persons')
76+
api.route(PersonDetail, 'person_detail', '/persons/<int:id>')
77+
78+
# Start the flask loop
79+
if __name__ == '__main__':
80+
app.run()
81+
82+
This example provides the following API structure:
83+
84+
======================== ====== ============= ===========================
85+
URL method endpoint Usage
86+
======================== ====== ============= ===========================
87+
/persons GET person_list Get a collection of persons
88+
/persons POST person_list Create a person
89+
/persons/<int:person_id> GET person_detail Get person details
90+
/persons/<int:person_id> PATCH person_detail Update a person
91+
/persons/<int:person_id> DELETE person_detail Delete a person
92+
======================== ====== ============= ===========================
93+
94+
95+
`More detailed example in the docs <https://flask-combo-jsonapi.readthedocs.io/en/stable/minimal_api_example.html>`_
3596

3697

3798
Flask-COMBO-JSONAPI vs `Flask-RESTful <https://flask-restful.readthedocs.io/en/latest/>`_

0 commit comments

Comments
 (0)