Eve: Building RESTful APIs with MongoDB and Flask Transcripts
Chapter: Your first Eve service
Lecture: Let's build and launch our first app
0:01 In this lecture, we are going to build our first Eve application. Now, before we can start, we need to make sure that we meet the pre requirements
0:10 the first one is that Eve must be installed on system, the second one is that Mongo must be installed, of course,
0:16 and the third is that Mongo needs to be running. So let's activate our virtual environment which we built a while ago, in a previous lecture.
0:27 As you can see here, the prompt has changed, so the virtual environments will be ready,
0:32 let's just make sure that the Python we are going to use is the right one, yes, as you can see, we are going to run the Python
0:40 which is stored within our virtual environment here, so we're good to go. Let's just make sure that Eve also is installed— yes it is.
0:52 Now we need to make sure that Mongo is running, let me open a new tab on my terminal window and simply launch the Mongo server.
1:03 So now everything is ready, Eve is installed and active, Mongo is running, and let's just open our code editor so we can start working on our code.
1:15 We still have our hello.py script here if you remember, we built this script when we were looking at how Flask works we can get rid of it because
1:25 we are going to build a new script for our app. First file we want to create is our launch script, let's call it app.py.
1:37 First line, from eve import the eve class second line, we create an instance of this Eve class and then we save, now that we have our app object
1:53 we want to instruct it on how the API should behave and it is always a good idea to keep behavior separated from the actual application context
2:03 so let's create a new file and call it settings.py. Now, typical Eve app will always have a settings file somewhere
2:14 this is where you basically instruct your API on how it should perform what is the API surface for example, stuff like that.
2:23 Now, most settings in Eve come with default values you use the settings file to override the defaults should you need to update them
2:32 but there is one single setting that has no default value and you have to set it otherwise your API won't even run. This is the domain setting,
2:43 it is a Python dictionary, and it is basically where you design your API surface every key in this dictionary is an endpoint
2:55 and every value is a Python dictionary itself, where you configure your endpoint behavior.
3:04 Okay, let's save while we leave the people endpoint with no definition, let's review what we've done so far:
3:13 we have an app.py script where we are importing the Eve class and creating an instance of it, and then we have a settings file
3:21 where we're configuring the behavior of our API. Right now, we are only setting the domain keyword,
3:28 and we are only defining one endpoint which is going to be called people, and it has no definition for the time being.
3:37 By the way, notice how the settings all are upper case we will see that there are exceptions when we are setting the endpoint definition
3:46 but right now we are setting the global settings like domain here and so it is all upper case.
3:54 This is typical for all Flask applications, not just Eve. Believe it or not, we have all we need in order to be able to launch our app.
4:04 Let's open the builtin terminal and activate the virtual environment, and now we can try and launch our app. If you remember from the Flask lecture
4:16 what we want to do is export a Flask app environment variable and set it to our launch script which is app.py. Then, because we are in development mode
4:30 and we want to debug our code, we also want to export the Flask debug variable so Flask will run in debug mode,
4:41 and now we're all set and we can launch our app. Here we go, as you can see, Flask is listing at local host on port 5000
4:53 and it is running in debug mode. Let's open Postman, our REST client and attempt a call to local host 5000
5:11 something is happening, it is actually sending us a response, as you can see, we get ok response status and yes, we have a payload here
5:22 because we are hitting the homepage we get basically navigation information so the links to the child node and, of course, this is an array
5:31 and we only have one endpoint and it is, of course, called people. So let's try and hit the people endpoint and here we go, as you can see we get again
5:43 an ok response status from the server and this time the payload is more complex this is because we are hitting a proper endpoint
5:52 and it is exactly the one we configured in our settings file, the identity items array is empty, the links node has a reference to the parents node
6:02 which is our homepage with the title and its URL and then there is a reference to the endpoint itself.
6:10 The meta node is telling us that we are on page 1, and we have a total of zero documents available.