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