Eve: Building RESTful APIs with MongoDB and Flask Transcripts
Chapter: Fine-tuning your REST service
Lecture: Event hooks
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
When a request is received, one or more events are filed by the framework, you can subscribe to these events with multiple callback functions.
0:10
There are essentially two kinds of event hooks. You can attach a custom function to a pre or post request event
0:17
or you can subscribe to a database event. We're now going to give a look at database events here we defined a standard Python function
0:28
it's called inject signature and it accepts the resource name and the response, which is about to be sent back to the client.
0:36
The goal of this function is simply to add a new field to the response with our signature.
0:42
Now, the way this works is that once we have the instance of our Eve app we can attach our function to the on fetched item event.
0:53
If we save and go to our Postman client and try to fetch one item, one document, this is a get request to this specific document,
1:05
we get back the document with the new field. So what we've been doing is essentially inject new fields into a document.
1:14
Now this field is returned to the client, but since we injected it right after reading it from the database, it is not in the database.
1:24
So essentially, we are transforming the document before it is sent back to the client, and we can do that because we are attaching our function
1:33
to the on fetched item, which, as the name implies, is only filed when an item has been fetched from the database
1:41
and is about to be sent back to the server. In this other example I want to show you how you can attach a functional to a post command,
1:52
so every time a post request hits the service and before the documents are sent to the db, we want this function to be invoked.
2:02
As usual, we get the resource and items is an array of documents which are about to be stored, because as you might recall
2:10
a client can submit more than one document with a single request. So what we're doing here is a simply iterate the documents
2:19
and if a born field is available in a document we add an age field that based on the day the person was born.
2:28
We then go and attach our callback function to the on insert event. Let's go and try these out. Here I am trying to post to the people endpoint
2:41
and the guy is called Gimme An Age and was born on 27 August 1970. Let's post this one, everything is fine,
2:53
now let's go back and see what happened to the person, we do a get to the person endpoint,
3:03
we have the last name, we have the born, and the default role but we also have the age field which is 48—
3:11
what happened here is pretty powerful if you think about it we are doing transformation this time, before the document hits the database
3:22
and because our previous inject signature function is still hooked to their own fetched item, when we did the get request
3:30
we also get the signature injected into the document, so while age is permanent on the database, signature is not,
3:39
it is still being injected by our callback function. There is a huge number of events to which you can attach your callback functions,
3:51
there is a table on the website you can go and look it up we have events for fetch, insert, replace, update, delete, you name it
4:00
and also every single command or request has an event before the operation is performed, and after the operation is performed.
4:11
And these are just the database events, then you have all that pre and post request events which are also equally powerful,
4:20
so you want to go and look them up. Finally, let's go back to our code and give a look at the operator we're using here
4:28
we're attaching a function to an event but the operator hits and the fact that we can actually add more than one function to the event
4:38
and this is true, we might have another function doing some fancy stuff, I don't know, performing some computation
4:45
and it will be a different one, isolated, which helps in keeping our code well organized, tidy and clean.