Structure of our REST API
app.py
In app.py
we'll initialize and configure our Flask application. We'll also set up our API resources.
This file is the entry point to our REST API.
db.py
In this file we'll create our database Python object, so that other files can import it. All other files import the database variable from this file.
The reason for creating a separate file containing just this is precisely so it's easier to import, and to avoid Python's circular imports.
In Python applications, if many files will import the same thing it's often worthwhile separating that into another file!
This is what the code looks like for this file:
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
Because this is a Flask extension, it needs to communicate with our Flask app. That's why in app.py
we have a line that registers this db
object with our app
:
# Most of the file...
if __name__ == "__main__":
db.init_app(app)
app.run()
Models
models/item.py
The ItemModel contains a definition of what data our application deals with, and ways to interact with that data. Essentially it is a class with four properties:
id
;name
;price
; andstore_id
.
Methods in the class can be used to find items by name, save them to the database, or retrieve them. Covered in more depth in this page.
models/store.py
The StoreModel is another definition of data our application deals with. It contains two properties:
id
; andname
.
In addition, because every ItemModel has a store_id
property, StoreModels are able to use SQLAlchemy to find the ones that have a store_id
equal to the StoreModel's id
. It can do that by using SQLAlchemy's db.relationship()
.
Covered in more depth in this page.
models/user.py
The UserModel is the final data definition in our API. They contain:
id
;username
; andpassword
.
Covered in more depth in this page.
Resources
resources/item.py
Finally, the resource is what defines how clients will interact with our REST API. In the resource we can define the endpoints where clients will have to send requests, as well as any data they have to send in that request.
For example, we could define that when clients send a GET
request to /item/chair
, our API will respond with data of an item called chair
. That data could be loaded from our database.
In Flask-RESTful we define a class
for each Resource
, and each Resource
can have one Python method for each HTTP method that it should be able to respond to.
For example, our Item
Resource might define how to respond to GET
, POST
, DELETE
, and PUT
requests in different ways.
If in doubt, read When to define a Resource and when to define a Model.
In addition, resources/item.py
also defines an ItemList
resource which can be used to retrieve multiple items at once via the API. Covered in more depth in this page.
resources/store.py
In a similar way to the Item
resource, the Store
resource defines how users interact with our API.
Users will be able to get stores, create them, and delete them. Similarly a StoreList
resource is defined to retrieve all stores in our API.
Covered in more depth in this page.
resources/user.py
These resources are quite different from the other two because they do not only deal with creating and updating data in our application, they also deal with various user flows like authentication, token refresh, log outs, and more.
For an in-depth review of these resources, check out this page.