WEBVTT 00:00.210 --> 00:02.340 Hello everyone and welcome back. 00:02.370 --> 00:09.090 In this lesson you're going to learn how to use the request module to programmatically send a TTP request 00:09.360 --> 00:10.530 using python. 00:10.560 --> 00:16.950 This will give you the opportunity to get a deeper understanding of all the HDTV related concepts discussed 00:16.950 --> 00:18.570 so far during the course. 00:18.570 --> 00:21.510 Get ready to build your first API with Django. 00:21.540 --> 00:24.760 In the next lessons LET'S GET STARTED. 00:24.810 --> 00:25.250 OKAY. 00:25.320 --> 00:27.320 WE ARE ON THESE STUDIO code. 00:27.360 --> 00:31.680 As you can see I've created a file called the double zero requests doge by. 00:31.770 --> 00:36.240 And this is in fact the first script that we're going to write during this lesson. 00:36.270 --> 00:38.940 We're actually going to make a couple of different examples. 00:38.970 --> 00:45.720 And I'm going to create a different script for each example so you can then have access to all the different 00:45.720 --> 00:48.170 versions of the program that we're going to write. 00:48.180 --> 00:53.550 As for all the codes that we're going to write during the course you will be able to download it from 00:53.550 --> 01:00.450 both the downloadable resources here on udemy and from our github repository on my guitar. 01:00.450 --> 01:07.770 Page One cool feature about Visual Studio code is that as soon as you save a file with a specific extension 01:08.070 --> 01:11.810 or 2080 Taurus functionalities are going to adapt accordingly. 01:11.820 --> 01:17.440 For example because you're using a pile on file we can see on the bottom left of the screen Visual Studio 01:17.460 --> 01:23.850 code as a set up of Python three point seven point two sixty four B2 which is the newest version currently 01:23.850 --> 01:30.500 installed on my system as the version of Python that it's going to use to execute our scripts. 01:30.510 --> 01:36.120 However considering that the Requests module is not part of Python standard library it's better if we 01:36.120 --> 01:42.750 create a virtual environment using Python 3 the end so that we can install the request module and all 01:42.750 --> 01:46.390 the other dependencies that we're going to need for out the whole section. 01:46.500 --> 01:54.810 So let's click on terminal new terminal and as you can see I'm currently on one level above the current 01:54.810 --> 02:02.800 four there where we find our cell phone so I can just change directory within 0 2 where we eyes and. 02:02.820 --> 02:03.860 I can give it a comment. 02:03.870 --> 02:08.110 Python free that and the end the end. 02:08.280 --> 02:15.920 Sir I'm creating a virtual environment called the NDA using the V end module from Python 3. 02:16.050 --> 02:25.750 I can now activated by doing source we have been activate as you probably know and we can check all 02:25.750 --> 02:27.240 the install dependencies. 02:27.250 --> 02:36.700 My join people list and we can see that aside from set up tools and people our environment is empty. 02:36.700 --> 02:46.390 We can now install requests by doing peep style requests which is going to download and install the 02:46.390 --> 02:48.060 request module. 02:48.110 --> 02:52.000 You can now give people e once again. 02:52.110 --> 02:52.740 Here it is. 02:52.750 --> 02:58.600 With all its dependencies we can now tell visuals to your code that we intend to use the virtual environment 02:58.630 --> 03:04.900 we've just created to execute all the python code for our project and we can inform the program of our 03:04.900 --> 03:10.380 choice by clicking on the interpreter on the bottom left of the screen which will show us at least we 03:10.380 --> 03:17.020 have all the python binary in our operating system and right at the bottom we shall find a new virtual 03:17.020 --> 03:18.870 environment we've just created. 03:18.970 --> 03:25.600 If for any reason you can't find these available environment directory just reload visible as to your 03:25.600 --> 03:32.830 code or open it up in the same folder of your project on the same directory level where you've created 03:32.830 --> 03:33.980 your environment. 03:34.000 --> 03:37.500 I can however just click on it now because it has found it. 03:37.500 --> 03:42.140 And as you can see it asks me to install pilings alien there for python. 03:42.190 --> 03:49.030 I can just click on install as you can see the environment gets activated and Palin gets installed. 03:49.060 --> 03:54.200 If we give it a command Pip at least we now also have Arlington. 03:54.670 --> 03:58.940 We can now finally start to write some code in this very first example. 03:58.940 --> 04:02.000 We're going to make a get request to Google some page. 04:02.050 --> 04:07.240 This is our first step towards getting familiar with all the theoretical concepts we've discussed so 04:07.240 --> 04:12.940 far such as HBP requests status codes responses and so on. 04:12.940 --> 04:21.700 So import requests and I'm now going to define a function that's going to call it Main you can call 04:21.700 --> 04:23.440 it however you want. 04:23.440 --> 04:26.220 We might actually call the client in the future. 04:26.310 --> 04:33.700 I'm going to use the past instruction for now because first of all I want to add two more lines if name 04:35.230 --> 04:37.970 equals Main. 04:38.090 --> 04:44.200 In this case I want to execute our function and I'm pretty sure you've already seen this line of code 04:44.230 --> 04:44.920 before. 04:44.920 --> 04:49.570 It's pretty common especially when you're using modules this way we just still in Python that we want 04:49.570 --> 04:51.900 to execute the main function basically. 04:51.910 --> 05:01.120 So let's make our requests go going to create a variable response to perceived requests not get I need 05:01.120 --> 05:08.780 to pass the you rail that we want to make the request to in this case Google's on page HDP double for 05:08.780 --> 05:09.400 words Lesh 05:12.040 --> 05:14.270 Google dot com. 05:14.320 --> 05:18.860 We've said that we can check if our request has being successful or not. 05:18.940 --> 05:21.260 My techie needs corresponding responses. 05:21.270 --> 05:33.010 Status code I can therefore do parental status code here I'm going to pass response that status code. 05:33.040 --> 05:33.750 Okay cool. 05:33.760 --> 05:38.850 We're not ready to execute our script and as you may know if you already used visual studio code there 05:38.880 --> 05:41.020 are a couple of ways to do so. 05:41.020 --> 05:47.580 The fastest way is just to right click with your mouse and then go to round python file in terminal. 05:48.070 --> 05:48.710 Excellent. 05:48.720 --> 05:53.340 Are requests as being successful as you can see we got status code 200. 05:53.350 --> 05:59.920 Okay so our request was successful because of course we made our get request to Google's on page and 06:00.280 --> 06:06.910 as everybody knows Google is one of the most reliable web sites on the planet but it also must be considered 06:06.910 --> 06:09.190 that our address was correct. 06:09.190 --> 06:14.660 We would have got back different status code asking for a page that doesn't exist. 06:14.980 --> 06:21.440 So let's make another example asking for a random page or getting them to comment this line here I can 06:21.450 --> 06:26.190 add slash brand name address like these. 06:26.260 --> 06:27.670 So let's execute this creep. 06:27.660 --> 06:33.040 Once again and of course now we get four or four not found. 06:33.040 --> 06:38.530 Of course we can also check the other attributes of a response object and the requests module makes 06:38.530 --> 06:40.840 it really easy to do so. 06:40.840 --> 06:49.450 So for example I could have checked the headers so I can just write Heather's 06:54.090 --> 06:59.040 let's execute once again but on a proper page. 06:59.250 --> 06:59.640 So 07:02.600 --> 07:05.180 you see now we get all the air there is for our request. 07:05.190 --> 07:12.070 For example date cash controlled content land content type. 07:12.130 --> 07:24.520 You see and if we want to of course we can even get just one specific header like content type like 07:24.600 --> 07:34.580 so and I could just ask for these specific elements like these so maybe I can't comment is one rampage 07:34.600 --> 07:39.470 item file and then release content type text ECMO. 07:39.650 --> 07:45.680 As we've said the Weber one of the most important parts in our response message is actually the message 07:45.680 --> 07:47.990 body the content of the response. 07:48.050 --> 07:55.350 And in this case because the content type is text HDMI out we can actually expect to get a whole HDMI 07:55.370 --> 08:02.480 page right in this specific case the HDMI code for Google's on page so we can go ahead and grab it like 08:02.480 --> 08:04.480 so. 08:04.550 --> 08:07.360 Copy this one right below. 08:07.550 --> 08:13.830 And I'm going to do content response not text. 08:13.830 --> 08:15.300 Let's execute the script. 08:16.250 --> 08:20.690 Run python file and if I now expand the terminal. 08:20.780 --> 08:25.500 We see that we've got the whole page in the email format of course. 08:25.550 --> 08:31.700 So going to Google same page with your browser making a get request to Google's home page we your browser 08:32.090 --> 08:39.290 you're actually downloading all these HD email code and then gets of course interpreted by your browser. 08:39.410 --> 08:45.220 That shows you the page 4 which was designed by Google's engineers. 08:45.320 --> 08:50.720 So now that we're starting to understand all these different parts of the request response cycle really 08:50.720 --> 08:56.840 work it's time to use the request module to actually communicate with a web API. 08:56.840 --> 09:02.510 And so we're going to see how to write a small client to communicate with the API offered by exchange 09:02.510 --> 09:04.640 rates API dot I. 09:04.790 --> 09:05.870 Which is we can read. 09:05.930 --> 09:12.680 It's a free service for current and historical foreign exchange rates published by the European Central 09:12.680 --> 09:13.640 Bank. 09:13.700 --> 09:19.990 And so if we scroll down the page we see how we can actually use the service that's offered by DARPA. 09:20.060 --> 09:27.890 We see that we for example can get the latest foreign exchange rates by making a get request in HDP 09:27.890 --> 09:36.680 get request to the end point EDP as API that exchange rates API daughter you slash latest we can get 09:36.680 --> 09:43.850 historical rates for any data since 1999 and we can also get the different values for different currencies 09:43.850 --> 09:44.630 of course. 09:44.630 --> 09:47.660 So because rates are quoted against the euro by default. 09:47.670 --> 09:53.420 Considering that of course the data is provided and published by the European Central Bank we can change 09:53.420 --> 09:59.040 them by passing a different parameter in the base parameter in our request. 09:59.140 --> 10:05.360 And so let's start by making a simple example of first a simple example making a get request towards 10:05.390 --> 10:06.620 the latest rates. 10:06.620 --> 10:07.590 And the point. 10:07.700 --> 10:14.390 Let's go back to visa rules to your code and I'm going to create a new file which I'm just going to 10:14.390 --> 10:27.210 call 0 1 requests that's by I can in fact copy part of the code but of course we need to actually change 10:28.260 --> 10:34.720 the endpoint then I'm going to copy right here and I can actually delete. 10:34.720 --> 10:42.630 Response I can also delete this one because it's wish we would not going to need responsible text. 10:43.170 --> 10:46.420 Instead I can comment content type. 10:46.490 --> 10:48.810 So let's go ahead and execute our script. 10:48.810 --> 10:55.390 I can close 0 0 requests and execute the 0 1 run by film fighting terminal. 10:55.750 --> 11:03.120 And so you see we get status code two hundred and content type application Jason which in fact means 11:03.120 --> 11:08.120 that the request was successful and the content type is Jason. 11:08.210 --> 11:13.170 Let's edit editor script to take advantage of the new information we got instead of printing the content 11:13.170 --> 11:13.700 type. 11:13.740 --> 11:18.100 We can go ahead and define a new variable data and I'm going to pass it. 11:18.130 --> 11:26.790 Response not Jason which is we can read Reuters The Jason encoded content of a response if any. 11:26.790 --> 11:35.910 Then of course I can go ahead and print it so print Jason data and I'm just gonna pass it data and you 11:35.910 --> 11:41.970 can add the whitespace and we can also let us create manage any kind of error that it might get while 11:41.970 --> 11:43.620 communicating with the API. 11:43.770 --> 11:54.840 So if response dot status code is different from 200 then I'm going to print the status code and then 11:54.840 --> 12:03.450 I'm going to raise an exception so a res exception I can pass something like there was an error. 12:03.450 --> 12:10.440 OK maybe add some space like so let's execute their script run by phone filing terminal. 12:11.340 --> 12:11.880 Excellent. 12:11.880 --> 12:13.810 The request was successful. 12:13.960 --> 12:17.240 And here we got to Aunt Shay son response. 12:17.250 --> 12:23.910 As you can see we got different key value pairs surrounded by curly braces. 12:24.030 --> 12:26.000 So we go to base in these cases. 12:26.000 --> 12:28.090 Era of course to date. 12:28.110 --> 12:30.000 Because these are the latest rates. 12:30.000 --> 12:34.490 This is today's date and then we got another Jason and nested within. 12:34.510 --> 12:39.210 We have all the actual rates as we've seen. 12:39.210 --> 12:46.790 We can also specify different currencies bypassing the base and symbols parameters. 12:46.890 --> 12:50.080 So let's make another example to take advantage of these features. 12:50.100 --> 12:52.220 I'm going to create another file. 12:52.620 --> 13:05.080 0 2 requests by and I'm just going to copy this code and so of course I need to modify the end point 13:05.170 --> 13:16.420 accordingly so I can pass for example base U.S. Dollar USD and symbols and maybe I can pass Great Britain 13:17.080 --> 13:27.130 pound so I'm going to get the rates between USD and GBP execute their script and so we get 1 USD in 13:27.130 --> 13:30.760 today's date equals zero point seventy seven. 13:30.760 --> 13:37.930 Great Britain pounds the request module however for size a more elegant way to define parameters for 13:37.930 --> 13:46.100 an end point so I can do something like this can go ahead and delete Bayesian symbol from here. 13:46.130 --> 13:54.460 And instead what I want to do is to create a dictionary a Python dictionary that I'm going to call payload 13:55.370 --> 13:57.930 and I'm going to set base. 13:58.030 --> 14:03.040 In this case we set USD and symbols 14:06.560 --> 14:19.630 we said Great Britain pounds I can now specify the parameters like so programs I'm going to pass payloads. 14:19.800 --> 14:25.080 So this way the parameters are going to be added automatically to our End Point. 14:25.080 --> 14:30.430 So let's say that I wanted to get a comparison between the U.S. dollar and maybe there is France crawling 14:30.440 --> 14:32.280 out of the Swedish Crown. 14:32.340 --> 14:43.070 So s e k sic let's execute our file from python file and if we get one U.S. dollar in today's date equals 14:43.100 --> 14:46.150 nine point twenty three Swedish crowns. 14:46.160 --> 14:52.190 So that was it for these introductory video to the request module a all the different parts that compose 14:52.460 --> 14:58.580 in each city request response cycle as you can see the request module makes it really easy to communicate 14:58.580 --> 15:04.820 with an API using Python and it is therefore a really important module to know it's really useful and 15:05.090 --> 15:08.890 if you need to you can also use it we find your own Django views. 15:08.900 --> 15:15.080 You could for example grab some data from an API end point and maybe process the data showing that the 15:15.080 --> 15:17.400 result of that processing and maybe exposing it. 15:17.410 --> 15:19.540 We have your own API endpoint. 15:19.610 --> 15:20.930 Well that was it for the video. 15:20.930 --> 15:22.280 See you in the next lecture.