Eve: Building RESTful APIs with MongoDB and Flask Transcripts
Chapter: Consuming the service
Lecture: Python
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
In this lecture, we're going to build a Python client and we will be using it to consume our RestFul service.
0:07
Now, we've already built a Javascript client, actually a web page in the previous lecture, now we're going to build a Python client,
0:16
so it is probably a good idea to create a folder in which we will be storing our clients, let's move the web page htm file within the new folder,
0:26
and then add our client.py file and this is where we will be writing our Python code. Now, whenever we're writing some Python code
0:40
which is supposed to be launched from the command line, we usually start with the same code which is basically a test
0:45
if the module name is equal to main and then to something, we could type that code, but we could also
0:52
leverage one of the many Visual Studio Code features which is code snippets. If you've used other editors and ides
1:00
you've already probably been using them, in Visual Studio Code you hit control and space and then you use intellisense
1:06
to get where you want to be, so for example, in this case I am writing if main code snippet for a main function,
1:13
let me just press enter and here we are with our code ready to go. So, when this module is launched from the command line,
1:22
as the main module that main function would be invoked, right now the main function is empty, which is fine and we're good to go.
1:29
We now need a couple of imports, here they are, so we're importing Json from the standard library and the request.
1:35
I don't know if you know about request already, if you don't, you can go here there is a URL to the homepage,
1:43
it's very cool and basically the go to tool in the Python world for whenever you need to do some requests over the internet
1:52
just go and pick request and download it, everybody is using it, so if you aren't, hurry up and join the flock.
2:00
Now, let me add a helper method, this will come in handy later, this is a very simple function, it will simply return the endpoint URL,
2:11
we provide the kind of a point we want to hit, and it will just return a string, with the complete URL to our endpoint.
2:20
Now, if we go back to Postman, and check our people endpoint, we see that there are a number of documents there,
2:29
so an interesting experiment might be write a function which will delete all the documents at the endpoint.
2:37
Yes, it can be dangerous, but we're bold, so let's try that. Here it is, delete all people, so as you can see on the first line,
2:45
this is basically a one liner, we're using the delete method of the requests library,
2:49
delete takes an URL to the endpoint where the delete should happen and here our helper method is very useful,
2:58
just ask for the full URL to the people endpoint. If in the future our client will be hitting our remote instance instead of local host,
3:07
we won't need to modify our function here, we only need to update our helper method by changing the http address of our restful service.
3:19
As you can see, delete returns a response object which has many properties, one of them being the status code,
3:26
so in line 11, we are just printing the server response to make sure that everything works as expected.
3:33
Alright, we can now go to line 15 and replace this pass with something more useful like maybe calling delete all people,
3:44
so now our script is ready, when we launch it, the main function here will be called
3:51
and it will launch the delete all people function for us, let's save, now we need to launch our script.
3:58
Now, if we look at the terminal here, we have our app running, so what we really need is a new terminal,
4:05
we also want to activate our virtual environment in this new window. If you haven't, and you probably haven't, you should also run pip install request,
4:19
because it won't be installed in your environment, it is in mine, if you launch it, I get an already satisfied requirement message,
4:28
this is because I wanted to show you intellisense for request so I went ahead and installed it ahead of registering these screencasts,
4:36
but yes, you should go and pip install request otherwise your script won't be running.
4:42
Now, let's try Python clients, client.py, let's see what happens. Great, as you can see, people deleted server response was 204, which is good,
4:58
this is what we were expecting. Let's go back to Postman and see if we actually deleted our documents, yes, the response is ok and as you can see,
5:09
the items array now is empty, so we actually deleted the people from our Mongo db instance. Great, let's switch back to our fist terminal window
5:23
where the server is running and we can see the sequence of our commands, this one is the delete,
5:29
which was used by our client with the response of course, and then, the get we did with postman is nice, isn't it? Alright, now that we can delete,
5:41
how about inserting some documents? Let me paste some more code here, specifically the post people function.
5:51
Now, as you can imagine, this is slightly more complex than the one liner we wrote before, but not too much, actually.
5:58
So we have an array with two documents because if I didn't tell you already, Eve supports bulk inserts by default,
6:06
which means that with a single request, you can store more than one document, actually, an unlimited number of documents.
6:13
So here we are going to send a post request for two documents, the actual command as you can see is very similar
6:21
to the delete one we did before, request.post post requires a URL again to the endpoint data to be posted, and some more options.
6:35
Our helper method gives me the URL for the people endpoint, and our data array is simply dumped as Json,
6:42
and then we need to provide a header and specifically, we need to tell request or I should say we need to tell our server
6:51
that the data coming in is Json. And that's it, then we're printing the response status code as we did before with the delete command.
7:01
Let's see what happens, if we after deleting all the people at the endpoint, post some people to the same endpoint.
7:10
You see this way every time we launch our script, we clean our endpoint and then we post,
7:16
let's go back to our terminal window and launch our client again, alright, first comes the delete with 204,
7:30
which is no content, it is what we expected, and then, people had been posted and the response was 201, which is created, so it looks good.
7:41
Let's go back to Postman and check the people endpoint, it was an empty endpoint, now it should have two documents,
7:48
here they are, this is an array with two documents, looking good. Back to our code, how about we now get data from the server,
7:57
let's do that, get people function, one liner again, request.get url for people so get at the people endpoint and as you guess already
8:11
the response status code is printed on the second line. Get people, so first, delete everything, then post people and then download,
8:22
now if we download these guys here we probably also want to see them to make sure we downloaded the right data, so we might do something like this,
8:31
so if the status code is actually ok, we get the Json from the response here, and of that Json, we get the items array,
8:40
we print the number of people in this line and then we iterate over the people array so for person in people and for every person we print first name
8:51
and the unique id of the person, it should be good, let's save, and again, launch our client. So we got delete in, then we got a post in and finally,
9:07
we downloaded from the server with a ok response. Two people were downloaded, one was John with this id and then it was Mike with this id. Great.
9:18
So to wrap it up, I will say that all in all writing Rest clients in Python is really easy, and this is mostly thanks to the request library
9:28
which does most of the work for us. If you think about it, our functions here are just one or two liners,
9:35
and if the get people which is the most complex has only one meaningful line which is this one, the Rest is just parsing.