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