Hello everyone and welcome back.
In this lesson you're going to learn how to use the request module to programmatically send a TTP request
using python.
This will give you the opportunity to get a deeper understanding of all the HDTV related concepts discussed
so far during the course.
Get ready to build your first API with Django.
In the next lessons LET'S GET STARTED.
OKAY.
WE ARE ON THESE STUDIO code.
As you can see I've created a file called the double zero requests doge by.
And this is in fact the first script that we're going to write during this lesson.
We're actually going to make a couple of different examples.
And I'm going to create a different script for each example so you can then have access to all the different
versions of the program that we're going to write.
As for all the codes that we're going to write during the course you will be able to download it from
both the downloadable resources here on udemy and from our github repository on my guitar.
Page One cool feature about Visual Studio code is that as soon as you save a file with a specific extension
or 2080 Taurus functionalities are going to adapt accordingly.
For example because you're using a pile on file we can see on the bottom left of the screen Visual Studio
code as a set up of Python three point seven point two sixty four B2 which is the newest version currently
installed on my system as the version of Python that it's going to use to execute our scripts.
However considering that the Requests module is not part of Python standard library it's better if we
create a virtual environment using Python 3 the end so that we can install the request module and all
the other dependencies that we're going to need for out the whole section.
So let's click on terminal new terminal and as you can see I'm currently on one level above the current
four there where we find our cell phone so I can just change directory within 0 2 where we eyes and.
I can give it a comment.
Python free that and the end the end.
Sir I'm creating a virtual environment called the NDA using the V end module from Python 3.
I can now activated by doing source we have been activate as you probably know and we can check all
the install dependencies.
My join people list and we can see that aside from set up tools and people our environment is empty.
We can now install requests by doing peep style requests which is going to download and install the
request module.
You can now give people e once again.
Here it is.
With all its dependencies we can now tell visuals to your code that we intend to use the virtual environment
we've just created to execute all the python code for our project and we can inform the program of our
choice by clicking on the interpreter on the bottom left of the screen which will show us at least we
have all the python binary in our operating system and right at the bottom we shall find a new virtual
environment we've just created.
If for any reason you can't find these available environment directory just reload visible as to your
code or open it up in the same folder of your project on the same directory level where you've created
your environment.
I can however just click on it now because it has found it.
And as you can see it asks me to install pilings alien there for python.
I can just click on install as you can see the environment gets activated and Palin gets installed.
If we give it a command Pip at least we now also have Arlington.
We can now finally start to write some code in this very first example.
We're going to make a get request to Google some page.
This is our first step towards getting familiar with all the theoretical concepts we've discussed so
far such as HBP requests status codes responses and so on.
So import requests and I'm now going to define a function that's going to call it Main you can call
it however you want.
We might actually call the client in the future.
I'm going to use the past instruction for now because first of all I want to add two more lines if name
equals Main.
In this case I want to execute our function and I'm pretty sure you've already seen this line of code
before.
It's pretty common especially when you're using modules this way we just still in Python that we want
to execute the main function basically.
So let's make our requests go going to create a variable response to perceived requests not get I need
to pass the you rail that we want to make the request to in this case Google's on page HDP double for
words Lesh
Google dot com.
We've said that we can check if our request has being successful or not.
My techie needs corresponding responses.
Status code I can therefore do parental status code here I'm going to pass response that status code.
Okay cool.
We're not ready to execute our script and as you may know if you already used visual studio code there
are a couple of ways to do so.
The fastest way is just to right click with your mouse and then go to round python file in terminal.
Excellent.
Are requests as being successful as you can see we got status code 200.
Okay so our request was successful because of course we made our get request to Google's on page and
as everybody knows Google is one of the most reliable web sites on the planet but it also must be considered
that our address was correct.
We would have got back different status code asking for a page that doesn't exist.
So let's make another example asking for a random page or getting them to comment this line here I can
add slash brand name address like these.
So let's execute this creep.
Once again and of course now we get four or four not found.
Of course we can also check the other attributes of a response object and the requests module makes
it really easy to do so.
So for example I could have checked the headers so I can just write Heather's
let's execute once again but on a proper page.
So
you see now we get all the air there is for our request.
For example date cash controlled content land content type.
You see and if we want to of course we can even get just one specific header like content type like
so and I could just ask for these specific elements like these so maybe I can't comment is one rampage
item file and then release content type text ECMO.
As we've said the Weber one of the most important parts in our response message is actually the message
body the content of the response.
And in this case because the content type is text HDMI out we can actually expect to get a whole HDMI
page right in this specific case the HDMI code for Google's on page so we can go ahead and grab it like
so.
Copy this one right below.
And I'm going to do content response not text.
Let's execute the script.
Run python file and if I now expand the terminal.
We see that we've got the whole page in the email format of course.
So going to Google same page with your browser making a get request to Google's home page we your browser
you're actually downloading all these HD email code and then gets of course interpreted by your browser.
That shows you the page 4 which was designed by Google's engineers.
So now that we're starting to understand all these different parts of the request response cycle really
work it's time to use the request module to actually communicate with a web API.
And so we're going to see how to write a small client to communicate with the API offered by exchange
rates API dot I.
Which is we can read.
It's a free service for current and historical foreign exchange rates published by the European Central
Bank.
And so if we scroll down the page we see how we can actually use the service that's offered by DARPA.
We see that we for example can get the latest foreign exchange rates by making a get request in HDP
get request to the end point EDP as API that exchange rates API daughter you slash latest we can get
historical rates for any data since 1999 and we can also get the different values for different currencies
of course.
So because rates are quoted against the euro by default.
Considering that of course the data is provided and published by the European Central Bank we can change
them by passing a different parameter in the base parameter in our request.
And so let's start by making a simple example of first a simple example making a get request towards
the latest rates.
And the point.
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
call 0 1 requests that's by I can in fact copy part of the code but of course we need to actually change
the endpoint then I'm going to copy right here and I can actually delete.
Response I can also delete this one because it's wish we would not going to need responsible text.
Instead I can comment content type.
So let's go ahead and execute our script.
I can close 0 0 requests and execute the 0 1 run by film fighting terminal.
And so you see we get status code two hundred and content type application Jason which in fact means
that the request was successful and the content type is Jason.
Let's edit editor script to take advantage of the new information we got instead of printing the content
type.
We can go ahead and define a new variable data and I'm going to pass it.
Response not Jason which is we can read Reuters The Jason encoded content of a response if any.
Then of course I can go ahead and print it so print Jason data and I'm just gonna pass it data and you
can add the whitespace and we can also let us create manage any kind of error that it might get while
communicating with the API.
So if response dot status code is different from 200 then I'm going to print the status code and then
I'm going to raise an exception so a res exception I can pass something like there was an error.
OK maybe add some space like so let's execute their script run by phone filing terminal.
Excellent.
The request was successful.
And here we got to Aunt Shay son response.
As you can see we got different key value pairs surrounded by curly braces.
So we go to base in these cases.
Era of course to date.
Because these are the latest rates.
This is today's date and then we got another Jason and nested within.
We have all the actual rates as we've seen.
We can also specify different currencies bypassing the base and symbols parameters.
So let's make another example to take advantage of these features.
I'm going to create another file.
0 2 requests by and I'm just going to copy this code and so of course I need to modify the end point
accordingly so I can pass for example base U.S. Dollar USD and symbols and maybe I can pass Great Britain
pound so I'm going to get the rates between USD and GBP execute their script and so we get 1 USD in
today's date equals zero point seventy seven.
Great Britain pounds the request module however for size a more elegant way to define parameters for
an end point so I can do something like this can go ahead and delete Bayesian symbol from here.
And instead what I want to do is to create a dictionary a Python dictionary that I'm going to call payload
and I'm going to set base.
In this case we set USD and symbols
we said Great Britain pounds I can now specify the parameters like so programs I'm going to pass payloads.
So this way the parameters are going to be added automatically to our End Point.
So let's say that I wanted to get a comparison between the U.S. dollar and maybe there is France crawling
out of the Swedish Crown.
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
nine point twenty three Swedish crowns.
So that was it for these introductory video to the request module a all the different parts that compose
in each city request response cycle as you can see the request module makes it really easy to communicate
with an API using Python and it is therefore a really important module to know it's really useful and
if you need to you can also use it we find your own Django views.
You could for example grab some data from an API end point and maybe process the data showing that the
result of that processing and maybe exposing it.
We have your own API endpoint.
Well that was it for the video.
See you in the next lecture.