In database terminology primary key refers to the column in a table that's intended to be the primary way of identifying rows. Each table must have exactly one, and it needs to be unique. This is usually some kind of a unique identifier associated with objects presented by the table, or if such an identifier doesn't exist simply a running ID number (which is incremented automatically).
Deadline¶
Deadline for delivering this exercise is 2024-02-19 23:59
Learning outcomes and material¶
During this exercise students will learn how to implement a RESTful API utilizing Flask Web Framework. Students will learn also how to test the API by reading the testing tutorial. We expect that you follow the same process to complete the Deliverable 3.
When following through this material, keep your local copy of the sensorhub app updated. You will need it for the last task. You can download what we had at the end of exercise 1 below. Filling in the other parts is considered part of the last task.
You can also use this simple script to generate a few sensors and locations for testing purposes. Run it after setting the FLASK_APP environment variable.
Introduction Lecture¶
This is an optional introduction lecture that adds some depth and visuality to the material in this exercise. As usual it's not necessary for completing the exercise but can be interesting if you want to know a little bit more.
Lay Your Database to REST¶
Exposing your data through a (REST) API is a good method to achieve decoupling between the database server itself and client applications that use the data. As long as the resources exposed through the API do not change in any way, the database server's internal workings can be altered without any risk of unwanted side effects across the larger ecosystem. Need to change your entire database engine? Not a problem. Change relationships between tables? Again, not a problem. Need to implement a reasonable backup behavior for the database being unavailable? Perfectly achievable.
From Models to Resources¶
In REST the basic unit of data is called a resource. A resource is a data representation that is deemed to be interesting enough to clients that it is exposed through the API. It's worthy of note that while it may sometimes be the case, there doesn't have to be a 1:1 correspondence between model classes and resources. In fact, more often it's more like 1:2 ratio between model classes and resources.
Resources can be roughly categorised into two types: collection type resources and item type resources. A collection type resource contains data from multiple database rows. Typically it's either a representation of all data in a specific table, or a specified subset of that data. If there's more than one interesting subset, we will often end up with even more than one collection resources per database table. Item type resources on the other hand contain everything about one item of interest. Once again, while often this equals a single row of data in a table, it is entirely possible that the resource is a representation that contains data from multiple tables.
It is also worth reiterating that a resource can be any thing that might be of interest to clients. One interesting example is a connector type resource which doesn't have any content, but its existence means there's link between other resources. Clients can then manipulate these connector resources when they wish to link or unlink objects. They're useful for APIs that expose many-to-many relationships because inherently we do not have a link verb in the basic RESTful vocabulary.
Addressing Important Things¶
The first key REST principle is called addressability. This principle states that everything of interest needs to be addressable with a uniform resource identifier, URI. When a client needs something from your API, it will send a request to the URI that belongs to the thing it needs, and it will always get the same resource as the response. One key thing to remember in REST: addresses are only assigned to resources, and they are essentially nouns. Actions, i.e. verbs, do not have addresses. They are represented through the uniform interface, i.e. using different HTTP methods on the same address. In other words, the URI is always the target of an action.
URIs are often designed as a tree structure that follows the data's inherent hierarchy. For instance, in our sensor example, measurements would be underneath the sensor that produced them. The URI template would then take a form along the lines of
/api/sensors/<sensor>/measurements/<measurement>/
Not only does this make the hierarchy of things visible to a human viewer, it also has implementation benefits that we will get into later. An interesting note about this particular example is that while it's very unlikely that a client will ever be interested in fetching an individual measurement, there might be a data cleaning client that is interested in deleting or modifying measurements. In these cases the delete/modify action would be targeted at the URI of the individual measurement.
This way of structuring URIs also naturally adds filtering by ownership: through the URI we're indicating we only want measurements from one particular sensor. Of course there are a lot of domains where a single thing can have multiple "owners". Luckily there is no limitation on how many URIs a particular endpoint can have. For instance, it's quite common for video games to have a publisher that's separate from the developer. This would give us two perfectly reasonable ways to list games: by publisher, and by developer.
/api/publishers/{publisher}/games/ /api/developers/{developer}/games/
While not a rule, you will also notice that quite often the URIs are patterned as
/collection/item/collection/item
and so on. This just follows naturally from the way data is usually organized. The selection of URIs gets more complicated as relationships between models increase in amount and variety. Ultimately it's then up to the API designer to think which ways of accessing the data are relevant to potential client applications. URI parts generally point to things that exist, and usually don't point to attribute values. If your API needs to offer filtering via attribute values, it is better to implement this as a search using
query parameters
. A suitable URI for such a search type could be a separate collection type, or just the master collection that by default includes every resource of the associated type. So in our sensor example we could just have /api/measurements/
where filtering can be applied.Handling Important Things¶
The second REST principle is called uniform interface. This principle states that actions are based on HTTP methods, and their implementation must follow the HTTP standard. There's a clear advantage here: as long as this principle is followed, actions on resources always work in the same way in every API. Of course there's a downside: it also limits us to a rather small set of verbs. The HTTP methods that are commonly used in REST APIs are: GET, PUT, POST, and DELETE. In addition, PATCH is also sometimes used but unlike the other four, it lacks a clear standard and thus its implementation will always be API specific.
GET fetches a document from the API. This document will be the resource description of whatever is pointed at by the URI. Generally speaking, if there haven't been any changes in the data, then GET will always return the same document. The client can specify what kind of a document it wishes to receive through headers. While we're not going to dive too deeply into this topic, one such commonly used header is Accept. This header lists the document types that the client is ready to accept. There are also condition headers that will tell the API server to not bother sending the response body if those conditions are not met. GET is always
idempotent
because it doesn't perform any changes.PUT replaces a resource with a new one, i.e. it quite literally puts a resource into the addressed URI. It is used mostly for modify operations, even though it technically also enables the creation of new resources (see POST below). It's important to realize that PUT is a complete replace. The request body must contain all attributes of the resource regardless of whether they are changed or not, and if an attribute is omitted, its previous value will be overwritten with a suitable empty value. In other words, PUT is not supposed to implement partial updates. Due to its nature as a replace, PUT is idempotent - no matter how many times it's performed, the result will always be whichever content was put in last.
POST is not as standardized in the HTTP specification itself, but in the context of REST it has an explicit meaning: it will create a new child resource that belongs to the addressed resource. It is most often linked to collection type resources, and is the preferred method of creating new resources. Addressing create actions to the collection type has a clear advantage: the client does not need to know what the final address of the resource is going to be. This is particularly relevant in contexts where the API resolves name conflicts when new resources are created - the client woud have no way of knowing whether it's about to cause a name conflict, or how to resolve it. It is typical for POST responses to include a Location header to inform the client about the final URI its newly created resource was placed into. POST is usually not idempotent - if a POST request is sent twice, it will create two identical resources.
DELETE is perhaps the most simple one of the bunch. It deletes the addressed resource. There really isn't much else to say about it. DELETE is idempotent because once deleted, the addressed resource simply isn't there anymore.
Finally we have PATCH which applies some kind of a modification to the addressed resource. However, as already stated, there is no one standard for this method. The request body is supposed to describe the operation, but the syntax is not standardized. If you use PATCH, you need to specify your API's PATCH syntax somewhere in the API documentation. It should not be used as a partial PUT. If all you are doing is replacing attribute values with new ones, PUT should always be used because there isn't anything vague about it.
What about actions that do not match any of the above HTTP methods? If at all possible, you should always consider a resource-based solution instead of implementing custom actions to your existing resources. Instead of coming up with a new verb, think whether you can come up with a noun that does what you want with the four basic verbs instead. If you are absolutely stumped, the last option is to overload POST. This means specifying your own POST format that allows performing multiple different actions on a resource using POST. However, doing so will mean your API is no longer strictly RESTful.
Implementing REST APIs with Flask¶
This exercise material covers how to implement REST APIs using Flask-RESTful, a Flask extensions that exists specifically for this purpose (in case you didn't figure that out from the name). The material has examples for both single file applications, and applications that use the the project layout we proposed. For exercise tasks you need to submit single file applications. However for your course project we recommend following the more elaborate project structure.
Introduction to Flask-Restful¶
In the first part of the exercise we'll cover how to use the RESTful extension. In the examples we are going back to the sensorhub example from the first material. As a very brief recap, we had four key concepts: measurements, sensors, sensor locations and deployment configurations. We'll implement some of these into
resources
as this example goes on.Learning goals: Learn the basics of Flask-RESTful: how to define
resource classes
and implement the HTTP methods
of resources
. Learn how to define routes
for resources, and about reverse routing for building URIs
.Installing¶
Some new modules are needed for this exercise. Fire up your
virtual environment
and cast the following spells (the last one is not needed for this section but it will be for the next one):pip install flask-restful pip install Flask-SQLAlchemy pip install flask-caching pip install jsonschema
A Resourceful Class¶
Flask-RESTful defines a class called Resource. Much like Model was the base class for all
models
in our database, Resource is the base class for all our resources
. A resource class
should have a method for each HTTP method
it supports. These methods must be named the same as the corresponding HTTP method, in lowercase. For instance a collection type resource will usually have two methods: get and post. These methods are very similar in implementation to view functions
. However they do not have a route decorator - their route
is based on the resource's route instead. Let's say we want to have two resource classes for sensors: the list of sensors, and then individual sensor where we can also see its measurements. The resource class skeletons would look like this:from flask_restful import Resource
class SensorCollection(Resource):
def get(self):
pass
def post(self):
pass
class SensorItem(Resource):
def get(self, sensor):
pass
def put(self, sensor):
pass
def delete(self, sensor):
pass
We're using SensorItem for individual sensors instead of just Sensor, mostly because we already used Sensor for the
model
and this would cause conflicts if everything was placed in a single file. If you want to pursue that path, simply place these classes inside your application module that has the models. However, if you followed the project layout material, these classes should be placed in a new module (e.g. sensor.py) inside the resources subfolder (also make sure there's a file called __init__.py
in the folder - it can be empty, but must exist for Python to recognize the folder as a package).The methods themselves are just like
views
. For example, here's a post method for SensorCollection that looks very similar to the last version of the add_measurement view in exercise 1. def post(self):
if not request.json:
abort(415)
try:
sensor = Sensor(
name=request.json["name"],
model=request.json["model"],
)
db.session.add(sensor)
db.session.commit()
except KeyError:
abort(400)
except IntegrityError:
abort(409)
return "", 201
Do note that all methods must have the same parameters because they all are served from the same resource
URI
! You can, however, have different query parameters
between these methods. For example, this would be typical for resources that have some filtering or sorting support in their get method using query parameters.Resourceful Routing¶
In order for anything to work in Flask-RESTful we need to initialize an API object. This object will handle things like
routing
for us. To proceed with our example, we'll show you how to create this object, and how to use it to register routes to the two resource classes
. In a single file app the process is very simple: import Api from flask_restul, and create an instance of it.from flask import Flask, request
from flask_restful import Api, Resource
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///test.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
api = Api(app)
Assuming your resource classes are in the same file, you can now add routes to them by dropping these two lines at the end of the file.
api.add_resource(SensorCollection, "/api/sensors/")
api.add_resource(SensorItem, "/api/sensors/<sensor>/")
Now you could send GET and POST to
/api/sensors/
, and likewise GET, PUT and DELETE to e.g. /api/sensors/uo-donkeysensor-4451/
. Not that they'd do much (except for POST to sensors collection which we just implemented). Extra note: When using a more elaborate project structure, resources should be routed in the
api.py
file which in turn imports the resources from their individual files. Here's the sample api.py
file, which assumes the resource classes were saved to sensor.py
in the resources folder.from flask import Blueprint
from flask_restful import Resource, Api
api_bp = Blueprint("api", __name__, url_prefix="/api")
api = Api(api_bp)
# this import must be placed after we create api to avoid issues with
# circular imports
from sensorhub.resources.sensor import SensorCollection, SensorItem
api.add_resource(SensorCollection, "/sensors/")
api.add_resource(SensorItem, "/sensors/<sensor>/")
@api_bp.route("/"):
def index():
return ""
Even More Resourceful Routing¶
When it comes to
addressability
, it only states that each resource
must be uniquely identifiable by its address. It doesn't say it can't have more than one address. Sometimes it makes sense that the same resource can be found in multiple locations in the URI
hierarchy. Consider the video game example from earlier. In that case both of these URI templates
would make equal sense:/api/publishers/{publisher}/games/{game}/
/api/developers/{developer}/games/{game}/
Both are different ways to identify the same resource. Luckily Flask-RESTful allows the definition of multiple
routes
for each resource. These would be routed as follows:api.add_resource(GameItem,
"/api/publishers/<publisher>/games/<game>/",
"/api/developers/<developer>/games/<game>/"
)
Do note that if you route like this, the resource's methods must now take into account the fact that they do not always receive the same keyword arguments: they will receive either publisher or developer. In this scenario, the GameItem resource could have a get method that starts like this:
class GameItem(Resource):
def get(game, publisher=None, developer=None):
if publisher is not None:
game_obj = Game.query.join(Publisher).filter(
Game.title == game, Publisher.name == publisher
).first()
elif developer is not None:
game_obj = Game.query.join(Developer).filter(
Game.title == game, Developer.name == developer
).first()
You can also utilize multiple routes to implement several similar resources using the same
resource class
. Reverse Routing¶
One more feature that we will soon be using a lot is the ability to generate a
URI
from the routing
. Hardcoding URIs into your API code is an update disaster waiting to happen. It's much better to do reverse routing with api.url_for
. Going back to our sensorhub example, this is how you should always retrieve the URI of a) sensors collection and b) specific sensor item:collection_uri = api.url_for(SensorCollection)
sensor_uri = api.url_for(Sensor, sensor="uo-donkeysensor-4451")
The function finds the first route that matches the
resource class
and given variables, or raises BuildError if no matching route is found. If found, the URI is returned as a string. Our two examples would generate:/api/sensors/ /api/sensors/uo-donkeysensor-4451/
Please bear in mind that if you separate resources into multiple modules, using Flask's basic routing function makes life much easier as explained here.
Even More Resourceful Routing¶
One thing that might get a little bit tiresome quite fast is having to fetch the corresponding model instances for resources that are present in the URI variables. I.e., we have to do something like this at the beginning of most methods:
class SensorItem(Resource):
def get(self, sensor):
db_sensor = Sensor.query.filter_by(name=sensor).first()
if db_sensor is None:
raise NotFound
While it's not the worst thing in the world, it's still a few lines of boilerplate that gets repeated often. In larger projects this can also lead into inconsistencies between parts of the system. Back in exercise 1 we introduced a way to convert numbers in the URI to floats automatically so that by the time they arrived to the view function as arguments, their type is already float. This was done in routing by adding type specifiers:
@app.route("/add/<float:number_1>/<float:number_2>/")
def add(number_1, number_2):
...
The route make use of
converters
. They are small classes that convert strings to Python types when resolving a route, and Python types to strings when constructing a URI. There's a few basic types available, but most importantly, Flask allows us to register custom ones. In other words we can make converters that turn unique identifiers used in URIs to the corresponding model instance - and the other way around too. Converters are relatively simple classes that inherit Werkzeug's BaseConverter class, and define two methods: to_python
and to_url
. The former is used in routing, and the latter is used in reverse routing. Here's an example for sensors:from werkzeug.exceptions import NotFound
from werkzeug.routing import BaseConverter
class SensorConverter(BaseConverter):
def to_python(self, sensor_name):
db_sensor = Sensor.query.filter_by(name=sensor_name).first()
if db_sensor is None:
raise NotFound
return db_sensor
def to_url(self, db_sensor):
return db_sensor.name
Once we have this converter, it needs to be registered, and then we can use it in routing. When registering we define its name that is used to indicate when to convert a string to a sensor instance. The registration is done to the app's URL map. After that the converter is ready to be used in routing, also shown below:
app.url_map.converters["sensor"] = SensorConverter
api.add_resource(SensorItem, "/api/sensors/<sensor:sensor>/")
With this all view methods in the SensorItem resource will have the corresponding sensor instance right from the start. Also when using
api.url_for
to get the URL of a freshly created sensor, you can simply pass a model instance as the sensor keyword argument, and it will automatically use its name in the URI. Although converters are very neat, there's some caveats to using them. An obvious one is overhead, especially with long URIs that have a lot of intermediate steps in the hierarchy. On the one hand, a model instance of every resource on the way might not be needed, but on the other hand it would be more consistent to use converters for all of them. So will you pass just the identifier string to some parameters, but an actual converted model instance to others? Or will you stay uniform and introduce some overhead from getting model instances that are not needed? Choose your own adventure, as there is no correct choice here.
Converters are also not very useful with resources that do not have a globally unique identifier (other than database ID which should generally not be used) of their own. The problem arises from lack of context: a converter does not see other parts of the URI - it simply converts one part of it. So even if
/<owner_name>/pets/<pet_name>/
would identify a unique pet resource, it's very unlikely that the API would make pet names by themself unique. In this case a converter would not know how to retrieve a pet if it only has the name. This is another problem with no clear-cut solution. You could accept that not all parts of the URI are converted, with the tradeoff of making your implementation less consistent. Or you could decide that everything gets its own globally unique identifier that is derived from a locally unique identifier. For instance, by adding a running counter when trying to use an identifier that is already taken. This way the first model instance to get a name would simply get the name (e.g. "doge"), but the next would get a counter added to it (e.g. "doge-1").
Overall, converters are a good tool for increasing consistency in your code, but their use is not always free. As with many other things, whether to make use of them or not depends on what you want to achieve.
Serial Modeling¶
Actually serializing models, but close enough.
Serialization
is a process of turning an arbitrary object in program memory into a format that can be stored onto the disk, or transmitted over a network - often as a string. The latter is of particular importance for APIs. A very simple example of serialization would be turning a Python dictionary into JSON
string using json.dumps
. For APIs serialization is most often needed when model instances need to be sent to the client as (part of) a resource. Since model instances are Python objects they cannot be serialized directly with just json.dumps
, and typically some code is needed to turn a model instance into serializable dictionary.As this is a very commonly occurring problem, there's a number of tools available to automate the process. One such tool is Marshmallow, which you can look into if you're interested. In this material we are going to implement serialization from scratch, as it is not that huge of an endeavor for our simple usecases. When going this route, the biggest question to start with is: where to put serialization code.
We have basically two reasonable options. View method is where it's needed (to produce the response), while model class method would put it closer to data, and make it easier to serialize the same type of model in the same way every time. Which option to pick depends mostly on how many different ways of serialization are needed per model class. If it's one or two, then model class method makes more sense. In a simple case that's usually how it is: we have the full details individual resource version, and then maybe a limited details collection resource version for each model class in the database.
For a simple example, a serialize method for sensors could look like this:
def serialize(self):
return {
"name": self.name,
"model": self.model
}
There are also some details that need to be considered when dealing with columns that do not have an obvious serialized equivalent. The two most common ones would be datetime columns, and then foreign key columns. The former is a bit simpler because it's only one value, so let's talk about that first. Datetimes are generally stored with the database engine's own datetime type, and on the Python side they are accesible as Python's datetime type. This type cannot be serialized automatically, and
json.dumps
will raise TypeError for a data structure that contains datetime types. At this point you need to decide how your API serializes datetimes. If you don't have any particular format in mind, a good default is the ISO 8601 format, which can be obtained with datetime's isoformat method (Python 3.8 and upwards). Serialize method for measurements: def serialize(self):
return {
"time": self.time.isoformat(),
"value": self.value
}
Embedded Serial Modeling¶
When it comes to representing foreign key columns, it's often relevant to consider what details should be shown in which resource. For instance, the location model in our example contains several details about the location, like its geolocation coordinates. Obviously all of these should be shown when the location resource itself is requested, but how much should be shown in the sensor resource? As usual, it all depends on what you want from the API. For our example, let's assume the only thing we want to show about the sensor's location in the sensor resource is the location's name. The name is also the location's unique identifier, so clients can use that to fetch more information about the location if it's relevant. With this, the sensor's serialize method could become
def serialize(self):
return {
"name": self.name,
"model": self.model,
"location": self.location and self.location.name
}
Implementation detail: because location is not mandatory, it is possible it will be None instead of a Location model instance. Using the and operator here will get rid of potential errors that would arise from trying to access the name attribute of None. Instead, the location value will simply become None.
Another approach would be to embed the serialize results of the location model while including a new optional argument to the serialize method. With this argument views and other serialize methods can control whether they want the long or short form of the referenced resource. The location's serialize would then be:
def serialize(self, short_form=False):
doc = {
"name": self.name
}
if not short_form:
doc["longitude"] = self.longitude
doc["latitude"] = self.latitude
doc["altitude"] = self.altitude
doc["description"] = self.description
return doc
and sensor's serialize (with the same keyword argumment added to keep the methods consistent with each other):
def serialize(self, short_form=False):
return {
"name": self.name,
"model": self.model,
"location": self.location and self.location.serialize(short_form=True)
}
This same keyword argument is handy when serializing relationships for collection type resources as well.
With serializers and converters in use, the get method for sensors will become very simple:
class SensorItem(Resource):
def get(self, sensor):
return sensor.serialize()
Deserial Modeling¶
On the other side of the equation, you may also want to make a method for creating a model instance from a JSON document. This is something that is essentially needed whenever processing POST and PUT requests. It makes sense to put it in the same place as the serialize method. A deserialize method would take a Python dictionary, and construct a model instance from it. For this purpose, it's good to keep in mind that model instances can be initialized as empty, and then filled in later - checking for required fields is done on commit. This means it's better to make our deserialize method for the update case with PUT, and then apply it to an empty model when creating new instances with POST. Here's an example for locations:
def deserialize(self, doc):
self.name = doc["name"]
self.latitude = doc.get("latitude")
self.longitude = doc.get("longitude")
self.altitude = doc.get("altitude")
self.description = doc.get("description")
Note the two different ways of reading from the dictionary. A key lookup is used for mandatory columns, and get is used for optional columns. This way the intended nature of PUT is implemented correctly, i.e. if an optional field is not set in the request body, its existing value should be replaced with the appropriate empty value (None for all of the columns here). Meanwhile if a required column is missing, this method will raise a KeyError. However, we do not intend for that to happen, since we are going to validate requests with schemas as described next.
Dynamic Schemas, Static Methods¶
JSON schema is a JSON document that's used for defining the valid structure of another particular JSON document. It defines what attributes the document can have, what are their types, and what kinds of values they can take. It can also define which attributes are required. For instance, the following schema defines what a valid sensor document looks like:
{
"type": "object",
"required": ["name", "model"],
"properties": {
"name": {
"description": "Sensor's unique name",
"type": "string"
},
"model": {
"description": "Name of the sensor's model",
"type": "string"
}
}
}
JSON schemas are useful for validating incoming POST and PUT requests. However, they do have a nasty drawback: they are awfully verbose. A schema that's easily over ten lines of code is definitely something that must be written in only one place. The same schema is often referenced at least twice (in corresponding POST and PUT methods) so it should not be hardcoded into any single resource method. It's also hard to attach to a resource class because the POST method to create a resource and the PUT method to modify it are in separate places (collection and seprate item respectively).
One of the more logical places for a method that produces the schema is the model class. This way it will be physically close to the code that defines the corresponding database structure. Furthermore, considering that we do not have a model instance in hand when validating an incoming POST request, it would be best to be able to call this method without one. In other words, making a static method serves this purpose rather elegantly. This example shows the method that we're adding to the Sensor model class.
@staticmethod
def json_schema():
schema = {
"type": "object",
"required": ["name", "model"]
}
props = schema["properties"] = {}
props["name"] = {
"description": "Sensor's unique name",
"type": "string"
}
props["model"] = {
"description": "Name of the sensor's model",
"type": "string"
}
return schema
Implementation detail: a static method is a method that can be called without an instance of the class and it also doesn't usually refer to any of the class attributes (that's what class methods are for). In other words it's actually a function that's just been slapped on a class to keep things more organized. It can be called as
self.json_schema()
from normal methods within the same class. From the outside it's called as Sensor.json_schema()
.If you want to go even further, you can even generate the schema from the model class itself. Here's a starting point if you want to look into it, or you could just write your own.
On the view side these schemas can be used in validation. In order to do this, two names are imported from the jsonschema module:
from jsonschema import validate, ValidationError
. The former being the function that performs validation against a schema, and the latter being an exception with details of why validation failed. Example of use can be found under the next heading.PUTting It All Together¶
Below is an example of what a PUT method for sensors could look like after we have added all these convenience methods to model classes.
class SensorItem(Resource):
def put(self, sensor):
if not request.json:
raise UnsupportedMediaType
try:
validate(request.json, Sensor.json_schema())
except ValidationError as e:
raise BadRequest(description=str(e))
sensor.deserialize(request.json)
try:
db.session.add(sensor)
db.session.commit()
except IntegrityError:
raise Conflict(
409,
description="Sensor with name '{name}' already exists.".format(
**request.json
)
)
return Response(status=204)
Caching¶
Caching is present in computing on many levels, starting from your computer's processor where it caches the results of operations. In the web world, caching also happens on many levels. Browsers tend to cache web content - static content in particular - to avoid unnecessary network transfers. Generally content that is not expected to change often is cached. A good example would be CSS files and images that are part of a website's layout. This type of content can go unchanged for years so there is very little reason to fetch a fresh version every single time the page is loaded.
Servers can use cache control headers to instruct browsers of how long it would be appropriate to cache each type of content, and as long the cache is considered valid, your browser will simply use the cached version that it has stored somewhere on your computer. In some cases this can cause sites to bug out temporarily if the server code has been updated but your browser is holding an older version of a script file that is no longer compatible with the backend. To work around this, browsers have a "force reload" mechanism that is harder than the normal refresh: triggering it will immediately invalidate all cached content and refetch them.
Server Side Caching¶
In this course we are primarily interested in server side caching. The primary purpose of server side caching is to reduce the frequency of hitting performance bottlenecks. In simple scenarios this usually translates to reducing database access as much as possible. Complex database queries with joins across multiple tables will quickly become very expensive performance wise, and if there is any possibility that the same result will be needed again, the benefits of caching that result should be rather obvious.
Lovelace content pages are a good real life example that you should be quite familiar with by now. A lot of things are embedded into the content page and the database structure underneath consists of a lot of tables - most of which are needed when rendering a content page. Most of this content is also quite static in nature: only very few things on the page depend on who is viewing them. Furthermore materials are not edited particularly frequently - a few times per year at most. In other words, unless edited, about 95% of what you see is the same HTML document every time you open it, and also the same document that everyone else sees.
Before server side caching support was implemented there was a notable loading time in the order of seconds every time a material page was loaded. After caching support, that 95% of the HTML document is now stored in cache indefinitely, and the loading time is barely noticeable when its retrieved from there. Because the content is valid for a long period of time and edits are relatively infrequent, cache in Lovelace is only invalidated when something that affects the cached document is changed. Term data is also cached separately. Meanwhile, user specific data like answer pages and progress bars are not cached because they change frequently, and are not shared between multiple users.
What to cache, when to cache, and when to invalidate are all critical decisions in the light of server performance. Cache writes and deletes do add overhead, so performing them on data that is unlikely to be requested again as exactly the same should be avoided. Another consideration that is largely application specific is whether it's ok to return stale data, and for how long. Obviously users will want to see the results of changes they have committed immediately, but is it critical for others to see them immediately as well?
This also brings to another use of caching: depending on how critical it is for your API to return fresh data, it's possible to use cache as a failsafe when parts of the API is down or too busy to respond. Dealing with temporary failure is a much larger concept and not something we'll tackle in this material, but it's worth mentioning as one of the advantages of caching.
Cache Implementation¶
Since caching is such a central component of web development, frameworks generally come with a built-in caching solution. They usually also allow you to choose between a number of different caching backends, each suitable for different use cases. Essentially all you need for caching is some way to store data, a simple lookup system, and some way to purge invalidated data - so it would not be a big deal to implement one yourself from scratch either.
For storage, one of the simpler solutions is to use the file system, and store cached documents as files. Not particularly elegant, and overall slowest of all the solutions even with solid state drives, but still a reasonable solution. Especially if there's a lot of content to cache. The opposite end is caching in the server's memory which is the fastest but has the obvious downside of being much more limited in storage space. As an in-between solution, document databases are quite ideal for storing cached results as well. While it's still essentially disk storage, database engines have better tools and optimizations than simple file system storage.
Flask has a caching extension available, called simply Flask-Caching. We will be using this extension in the upcoming examples. Similarly to Flask-SQLAlchemy, this extension gives us the freedom to choose our cache storage solution by configuring it to use one of the supported backeneds. Some of these backends work out of the box and are sufficient for learning purposes - others require installation of additional libraries, but are much more suitable for production deployments.
Flask-Caching lists four separate use cases:
- caching view function results
- caching other function results regardless of arguments
- caching function results for different sets of arguments (memoize)
- caching arbitrary data manually
The first and third use cases form the cache key automatically, with an option to add a prefix. In the other two cache keys are defined by the program code. In the first three cases, when the function is called, the cache framework will check whether there's a valid cached result available. If a result is available, it will be returned from cache instead of actually calling the function. If it's not available, then the function will be called, and the result will be cached before it is returned to the original caller. All three are available as
decorators
. The last use case has the least automation, but also is not limited in any way beyond normal rules for what can be cached.Cache Configuration¶
Cache is configured similarly to the database backend, by setting specific keys in the configuration dictionary. The full list of configuration keys for Flask-Caching can be found in its documentation. A lot of the configuration keys are cache type specific. Since we don't want to set up a separate service for caching, we have three choices for the cache type:
- NullCache, which doesn't actually store anything
- SimpleCache, which stores into a runtime dictionary and isn't persistent
- FileSystemCache, which stores into a specified directory as files.
The former two aren't really useful besides allowing your code to run when there is no actual cache backend available. SimpleCache would also be suitable for testing whether something goes into your cache the way it's supposed to. For now let's configure FileSystemCache because it allows full exploration of how caching works. In order to do so, at least two keys need to be set:
app.config["CACHE_TYPE"] = "FileSystemCache"
app.config["CACHE_DIR"] = "cache"
This would place cache files into the cache subfolder of the directory where your application file is. If using more elaborate project structure, it would be better to put the cache folder into your application's instance folder, similarly to how the database file is placed there:
os.path.join(app.instance_path, "cache")
. Another key that can be considered here is "CACHE_DEFAULT_TIMEOUT"
. This determines how long a cached entry will be valid by default, expressed in seconds. This value can be overridden for each entry separately as well. Choosing this value is extremely context-dependent. Leaving it unset defaults to caching things forever unless manually cleared.Overall, cache expiration is a very complex topic, and while an interesting problem, it's not something we're going to dig too deeply into. If you don't set a timeout at all, you'll only pay the regeneration price when things actually change. Sometimes this is what you want, but then you have to be very careful to actually regenerate the cache on every change - otherwise you'll be offering stale data indefinitely. Using a long default timeout will guarantee that even when cache clearing is incomplete, your data will eventually update. On the other end, using a very short timeout can be beneficial for data that changes frequently so that you don't even need to worry about manual clearing. Ultimately everything depends on what you need from your caching plan.
After configuration the cache needs to be initialized. This is very much like the API initialization.
from flask import Flask
from flask_restful import Api
from flask_caching import Cache
app = Flask(__name__)
app.config["CACHE_TYPE"] = "FileSystemCache"
app.config["CACHE_DIR"] = "cache"
api = Api(app)
cache = Cache(app)
After this we can access the caching funcionality through the
cache
object. In the full project structure version cache is initialized without app at first (just like the database), and then the app object is bound to it with cache.init_app(app)
in the app constructor function.As a final note, in some deployments
"CACHE_KEY_PREFIX"
will also be an important option to set in configuration. When sharing a cache backend between multiple apps, giving a key prefix for each app will make it possible to do app-wide cache clearing without incidentally deleting cached data that belongs to other apps. Caching Views¶
The simplest use case is to cache views with the cached
decorator
. When using the decorator, the Response object returned from the view function is cached using request.path
(e.g. "/api/sensors/sensor-0001/"
) value as the cache key. Thanks to the addressability principle, this approach is a very good fit for REST APIs. When every URI matches exactly one resource, there is no ambiguity as to what should be cached. With this in mind, we can start adding the cached decorator to get methods for our resource classes. Like this:class SensorItem(Resource):
@cache.cached()
def get(self, sensor):
db_sensor = Sensor.query.filter_by(name=sensor).first()
if db_sensor is None:
raise NotFound
body = {
"name": db_sensor.name,
"model": db_sensor.model,
"location": db_sensor.location.description
}
return Response(json.dumps(body), 200, mimetype=JSON)
Every call to this view function is now cached so that each separate sensor item gets its own cache key. In other words, as long as the sensor parameter is different, a new response will be generated and then cached for that particular sensor. However, if the same parameter is requested twice, the decorator will simply return the cached response instead of ever actually calling the function.
Normally this is a perfect use case because every set of URI variables matches exactly one response. However, if the resource uses
query parameters
to modify the result, this is no longer the case. This approach uses the request's path as the cache key, and this path does not include query parameters. This means that if you were to cache the result with one set of query parameters, then all further responses would use that cached result instead of calling the view function. In short, you would get the first response with every request regardless of what query parameters are used later. Now the first big question is whether you even want to cache such resources. Let's assume we want to
paginate
the measurements collection from a sensor because there's quite a few measurements. In order to do that, our measurement collection takes a query parameter, page, an integer that shows which chunk of the data the client is interested in. For purposes like this, the cached decorator
has an optional keyword argument, make_cache_key. This argument takes a function that will construct and return the cache key.A simple option would be a function that returns
request.full_path
instead of request.path
. Like this:def query_key(*args, **kwargs):
return request.full_path
However, keys provided by this would not be very reliable because the query string is not limited in any way. It can have extra parameters that are simply ignored by the rest of our code, but this function would form a different key for every different set. Also, if the view supports more than one parameter, their order would also result in different keys. A better solution is to actually extract the page and use that together with the URI as the key.
def page_key(*args, **kwargs):
page = request.args.get("page", 0)
return request.path + f"[page_{page}]"
NOTE: although not mentioned in the documentation, the function for generating the cache key gets passed all of the arguments that were given to the view function. Since we don't have any use for those, all arguments and keyword arguments are simply made optional.
Using it in the measurement collection resource:
class MeasurementCollection(Resource):
PAGE_SIZE = 50
@cache.cached(timeout=None, make_cache_key=page_key)
def get(self, sensor):
db_sensor = Sensor.query.filter_by(name=sensor).first()
if db_sensor is None:
raise NotFound
page = request.args.get("page", 0)
remaining = Measurement.query.filter_by(
sensor=db_sensor
).order_by("time").offset(page * self.PAGE_SIZE)
body = {
"sensor": db_sensor.name,
"measurements": []
}
for meas in remaining.limit(self.PAGE_SIZE):
body["measurements"].append(
{
"value": meas.value,
"time": meas.time.isoformat()
}
)
return Response(json.dumps(body), 200, mimetype=JSON)
As a side note we've made the decision to never expire this cache. The reasoning is simple: once a page is complete, it will never be changed as all new measurements are added to the latest page. There is another keyword argument that could be used here to prevent caching of incomplete pages: response_filter which is a function that gets one argument - the response object - and makes a decision whether to cache it or not. It's mostly ideal for bypassing caching when the response code is not 200. For this particular use case it's a bit cumbersome because we would need to parse our own response to see whether the page is full or not. Managing this might actually end up being easier with manual caching.
Manual Caching¶
In the previous example we have introduced a new problem: we now need to know when a page is incomplete, and forgo caching if that is the case. While this can be done with view function caching, it will be a rather messy solution - mainly because the decorator cannot see inside the function, and needs to read the response in order to find out what was returned from the view. In this case caching manually inside the function code could be a better solution. This allows the view function itself to have finer control of what gets cached. The caching process itself is quite straightforward: just use the
cache.set
method to store a value into a key. Since we want to cache the whole response, all modifications go to the end of the code, changing the return statement essentially to: response = Response(json.dumps(body), 200, mimetype=JSON)
if len(body["measurements"]) == self.PAGE_SIZE:
cache.set(page_key(), response, timeout=None)
return response
We can use the same page_key function to form the cache key. This is important for the next part since now we need to rethink how to read the cache. Previously the
decorator
took care of both checking the cache, and saving into it too. Now that we have implemented manual caching we want to get rid of the latter part. However, if possible, we would still like the decorator to do the cache checking. This can be achieved with the response_filter keyword argument that was mentioned in the previous section. If this argument is given a function that returns False, then the decorator will not perform any caching. The simplest way to do this is a lambda function. The end result is then:class MeasurementCollection(Resource):
PAGE_SIZE = 50
@cache.cached(timeout=None, make_cache_key=page_key, response_filter=lambda r: False)
def get(self, sensor):
db_sensor = Sensor.query.filter_by(name=sensor).first()
if db_sensor is None:
raise NotFound
page = request.args.get("page", 0)
remaining = Measurement.query.filter_by(
sensor=db_sensor
).order_by("time").offset(page * self.PAGE_SIZE)
body = {
"sensor": db_sensor.name,
"measurements": []
}
for meas in remaining.limit(self.PAGE_SIZE):
body["measurements"].append(
{
"value": meas.value,
"time": meas.time.isoformat()
}
)
response = Response(json.dumps(body), 200, mimetype=JSON)
if len(body["measurements"]) == self.PAGE_SIZE:
cache.set(page_key(), response, timeout=None)
return response
Since we are using the same function to generate the key at both ends, this works out very nicely. If you want proof of it working, you can put any print statement to the beginning of the method while running flask in development mode. If you see a print pop up, the cache was not hit, and the view method was called. If you do the same request again, this time you should not see a print as the result comes from cache, and is picked up from there by the decorator. If you request another page of measurements, then you should see a print pop up again.
Note that this is extremely permanent caching. There is absolutely nothing that will clear the cache. Then again, there is no interface to modify or remove existing measurements either, so it's exactly what we wanted. The only caveat being that if something is changed in the implementation (or there's bugs) then we would need a way to forcibly clear the cache, otherwise we're holding broken data indefinitely.
Cache Clearing¶
Unless you are only using extremely short cache lifetime (order of seconds), there will likely be times when you need to clear cache inside your API's view functions. This can be done one key at a time, or the entire cache can be nuked out of existence. The latter is something you might want to add as a command line tool for clearing the cache in situations where a bug is causing your API to offer stale data. To do this from Python console:
In [1]: from flask_caching import Cache
In [2]: from app import app
In [3]: cache = Cache(app)
In [4]: with app.app_context():
...: cache.clear()
We'll turn this into a command line tool later. Note: if there are multiple apps using the same cache backend, this can nuke the entire cache if key prefixes are not used. While wiping everything is sometimes needed, the more common use case is clearing data that we know has gone stale. Implementation wise this is quite simple:
cache.delete
can be used to delete one key, or cache.delete_many
can be used to delete a list of keys. Since we know how the keys are formed, we can also delete them quite easily. Keys for view caching can be obtained with api.url_for
, whereas in manual caching we can simply use the same function to form the key when storing and deleting.The bigger question lies in taking care to delete cache for all affected resources. For instance, when a sensor is created, we need to take care to refresh the cache for the sensor collection. Likewise when a sensor's details are changed, we may also need to refresh cache for the sensor collection, and also the sensor's location. Since cache refreshes can essentially be triggered from three different view methods (POST, PUT, DELETE) there are two places in our code base where they would make sense: as methods in model classes. or in resource classes.
For this example, we will implement cache clearing as resource class methods. After all, it is the resources themselves that we are caching - usually whatever comes out of its view functions. We're going to add an internal method
_clear_cache
to each resource. This internal method can then be called from view methods that modify the resource, and it will then take care of clearing everything that needs to be cleared. For example, to refresh both the sensor itself and its collection when a sensor is updated, the following method would be appropriate: def _clear_cache(self):
collection_path = api.url_for(SensorCollection)
cache.delete_many((
collection_path,
request.path,
))
Since we know that the keys are the same as the views' URIs, we can obtain the keys in a consistent manner by using
api.url_for
and request.path
, where the latter will take care of refreshing the resource that is directly being affected, and the former can be used to refresh any connected resources - such as the collection this item belongs to.API Authentication¶
Authentication is yet another huge topic in the web world. In this small section we'll take a very brief look at API authentication. Our primary focus here is how to include authentication into your API code. While we do provide some basic pointers about security implementation with Python, this is by no means a security tutorial. Likewise the measures shown here are mere examples that show you how to include security into your API, we do not propose them as sufficient by any account.
API Keys vs Session Keys¶
Web sites typically use session keys to identify when a user is logged in. When you type in your username and password for a web site, it will send you a
cookie
that contains a newly generated session key. As long as this session key is valid, you are considered logged in from the browser that holds the cookie. This way your user credentials
will only be sent with the initial login and are not exposed in later network traffic - or in the cookie. Session keys can have limited validity, and a multitude of other security mechanisms to prevent attackers from capturing your cookies etc. However, from RESTful API point of view, sessions are out of consideration. REST APIs are expected to be stateless - no state is held on the server side - and sessions are essentially state that the server uses to track when a user is logged in. When authentication is needed, API keys are used instead. Conceptually they are similar to session keys: the key is a generated authentication token that is used instead of username and password to authenticate transactions. Like session keys, API keys are also sent along with every request, and similarly a registry of API keys is held somewhere in the server. Unlike session keys, API keys are generally permanent, and independent of the client application. In this sense they are more akin to your user credentials: you can access the server from any client as long as it has your API key.
When using an API key it is usually inserted into the client's configuration. When the client accesses the server, there is no login phase because it already holds the authentication token. Therefore there is also no server-side state, and every request sent by the client is entirely independent from other requests. It probably goes without saying that API keys should be treated equally to credentials when storing them. Unlike a stolen session key, a stolen API key can be used by anyone, at any time, from anywhere.
Implementing API Key Authentication¶
Implementing authentication with API keys has two parts to it. First of all we need to have some registry for existing API keys; second, we need to apply authentication where it's required. We will be showing both of these steps from scratch to give you a more transparent view into what is going on. However, for real life uses an existing, properly tested solutions should be used.
Key Registry¶
Key registry in our simple example will just be another table in the database - or in other words, a new model class. What goes into this table is once again up to what you need. Generally though, it should have the API key, and some information about what privileges the key grants. If it's a key linked to user credentials, then the table would indicate which user it was generated for. Instead of individual users, your API could also have different groups that have access to different resources etc. Likewise if you want to control what kinds of rights the key grants, there can be a field for that.
For our sensorhub API we could have two different client groups: sensors that are allowed to post measurements, and admin clients that are allowed to change all information. In this case we should make three columns: key, sensor name (nullable), and a boolean column to indicate admin privileges. Also, for minimum basic security, we should not store API keys in plaintext. The key field will therefore contain a
hash
of the key. This way even if our database gets leaked, the keys still need to be cracked. class ApiKey(db.Model):
key = db.Column(db.String(32), nullable=False, unique=True)
sensor_id = db.Column(db.Integer, db.ForeignKey("sensor.id"), nullable=True)
admin = db.Column(db.Boolean, default=False)
sensor = db.relationship("Sensor", back_populates="api_key", uselist=False)
@staticmethod
def key_hash(key):
return hashlib.sha256(key.encode()).digest()
The added helper method will take care of encryption for us when keys are created and compared. Once again we place the method close to where it is relevant. Having one place where encryption is defined also makes it easier to move to a more secure solution - simply modify this method to fit your encryption needs.
The admin key should probably by created only once, and locally on the API server during installation. Since we haven't talked about CLI commands yet, we'll just create it from the Python console.
In [1]: import secrets
In [2]: from app import ApiKey, db
In [3]: token = secrets.token_urlsafe()
In [4]: db_key = ApiKey(
...: key=ApiKey.key_hash(token),
...: admin=True
...: )
In [5]: db.session.add(db_key)
In [6]: db.session.commit()
In [7]: print(token)
When generating keys, Python's secrets module should be used to provide cryptographically strong randomness, which the random module does not do (because it is intended for different use cases). The print at the end is the only time you will be able to obtain the plain text token, and from here it should be copied to your admin client's configuration.
For sensor keys we need to think about the logistics a bit more. One relatively simple way would be to simply generate a key when a sensor is created and return the key to the client somehow. Probably in the headers since responses to POST should not have a body. Another option would be to include API key as a required field when creating a sensor. This way the client that registers sensors will be responsible for generating keys. We'll leave the logistics as an exercise for the reader at this point.
Validating Keys¶
Once we have our keys, we need some way to require them for certain views. Our friends,
decorators
, will be here to help us out. We have two levels of privilege, and a simple way to go about is to make one decorator for each. The first decorator will be called require_admin
that will block the view unless the authentication headers contain a valid admin token. For this example we're going to use a custom HTTP header
to carry the API key. This is mostly because the standard Authorization header has a syntax that's more complex than what we need. The authentication key will be in "Sensorhub-Api-Key"
header.def require_admin(func):
def wrapper(*args, **kwargs):
key_hash = ApiKey.key_hash(request.headers.get("Sensorhub-Api-Key").strip())
db_key = ApiKey.query.filter_by(admin=True).first()
if secrets.compare_digest(key_hash, db_key.key):
return func(*args, **kwargs)
raise Forbidden
return wrapper
Once we have this decorator, we can decorate any resource class method with
@require_admin
, and it can no longer be accessed unless the correct admin key is provided in the request headers. Now we need another decorator for authenticating individual sensors. This decorator will be somewhat similar. The only difference is that we do need the view method's sensor parameter inside the decorator too.def require_sensor_key(func):
def wrapper(self, sensor, *args, **kwargs):
key_hash = ApiKey.key_hash(request.headers.get("Sensorhub-Api-Key").strip())
db_key = ApiKey.query.filter_by(sensor=sensor).first()
if db_key is not None and secrets.compare_digest(key_hash, db_key.key):
return func(*args, **kwargs)
raise Forbidden
return wrapper
If you put this decorator on a resource method that has sensor as a parameter, then that particular sensor's API key will be required to access the method. We didn't actually implement a way to distribute the sensor keys though, so doing that will make the method entirely inaccessible.
Security Caution¶
There's one security consideration that needs to be stated here: regardless of how fancy encryption you use for storing your keys, they are only as safe as your transport layer security. At the end of the day, the key is included in request headers. If you are not using an encrypted protocol, these headers will be readable in plain text for any party that can intercept the message. Always, always remember to do any and all transmissions involving secret keys over a secure connection, i.e. HTTPS. However, setting up HTTPS for Flask falls out of this material's scope as it is almost entirely a deployment issue rather than implementation.
Anna palautetta
Kommentteja materiaalista?