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