1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
*******************************
Quotes API (Flask + SQLAlchemy)
*******************************
Below is a full example of a REST API for a quotes app using `Flask <http://flask.pocoo.org/>`_ and `SQLAlchemy <https://www.sqlalchemy.org/>`_ with marshmallow. It demonstrates a number of features, including:
- Custom validation
- Nesting fields
- Using ``dump_only=True`` to specify read-only fields
- Output filtering using the ``only`` parameter
- Using `@pre_load <marshmallow.decorators.pre_load>` to preprocess input data.
.. literalinclude:: ../../examples/flask_example.py
:language: python
**Using The API**
Run the app.
.. code-block:: shell-session
$ uv run examples/flask_example.py
We'll use the `httpie cli <https://httpie.io/cli>`_ to send requests
Install it with ``uv``.
.. code-block:: shell-session
$ uv tool install httpie
First we'll POST some quotes.
.. code-block:: shell-session
$ http POST :5000/quotes/ author="Tim Peters" content="Beautiful is better than ugly."
$ http POST :5000/quotes/ author="Tim Peters" content="Now is better than never."
$ http POST :5000/quotes/ author="Peter Hintjens" content="Simplicity is always better than functionality."
If we provide invalid input data, we get 400 error response. Let's omit "author" from the input data.
.. code-block:: shell-session
$ http POST :5000/quotes/ content="I have no author"
{
"author": [
"Data not provided."
]
}
Now we can GET a list of all the quotes.
.. code-block:: shell-session
$ http :5000/quotes/
{
"quotes": [
{
"content": "Beautiful is better than ugly.",
"id": 1
},
{
"content": "Now is better than never.",
"id": 2
},
{
"content": "Simplicity is always better than functionality.",
"id": 3
}
]
}
We can also GET the quotes for a single author.
.. code-block:: shell-session
$ http :5000/authors/1
{
"author": {
"first": "Tim",
"formatted_name": "Peters, Tim",
"id": 1,
"last": "Peters"
},
"quotes": [
{
"content": "Beautiful is better than ugly.",
"id": 1
},
{
"content": "Now is better than never.",
"id": 2
}
]
}
|