Welcome to day 40
of your 100 Days of Python Challenge.
Today we'll build a mini weather API
using flask and Rest principles.
You'll learn how to create, handle, and serve a RESTful endpoint
that deliver weather data in Json format.
This project will also cover basic API routing,
error handling, and query parameters.
Let's look at what you learn today.
First, what are the Rest APIs?
Now you've used some of it, but we've actually did not know what we were doing.
So this is where we will learn it.
Setting up a flask rest API.
Next defining API routes and endpoints.
Returning Json responses.
And finally De 40 project which is a mini weather API.
So let's get started and let's look at what are Rest APIs Rest.
Which stands for Representational State
Transfer, is an architecture style for building APIs.
Now Rest APIs use Http methods to interact with resources.
There are four different types of methods that are basic, which are get
which retrieve data.
Your post which adds new data you have put,
which updates existing data, and we have delete which removes data.
Now why build APIs?
First, for data access APIs enable
sharing data across applications.
Scalability.
APIs can scale to handle many requests and finally flexibility.
Clients can request only the data they need.
So that brings us to setting up a flask rest API.
Now for that,
we need to install some required packages which include flask and flask RESTful.
So you already have flask.
I'm going to open my terminal here or command prompt.
And you can go ahead and say it
install flask.
You should have that flask dash restful.
This is another one which is new which will be using this time.
And it's going to download if I don't have anything.
As I said,
it already has
flask, but I didn't have flask restful and it installed successfully right here.
So I'm going to clear this out.
So that will install all the required packages.
Next let's go ahead and create a basic flask API example.
I'm going to go here create a new file.
I'm going to save this as app.py.
And here first I'm going to say from flask
import flask and Jason if flask you know what it is Jason ify
is something that converts Python dictionaries into Json responses.
And that's why I am importing Json if I here.
Next I'm going to create the app by saying flask underscore underscore name
underscore underscore.
And then let's create a root endpoint.
Now this is something similar to creating the drought say app dot root
slash create a function home.
And here instead of returning text or HTML or template
I'm going to say return Json ify.
And I'm going to give a dictionary of message.
Welcome to Mini Weather API.
So this will return back.
So if I call this API which I call the URL slash,
it will give me a Json data instead of HTML or anything else.
So it's nothing.
But instead of getting text or HTML, I'm getting Json in this particular case.
Finally, to run the application, if name equal to main
approach, run debug equal to true.
Now if I run this application on my.
So I'm going to go here.
Make sure I'm the right folder.
And here I can say Python app.py.
It's running on this particular URL.
I open this URL in my Chrome browser here.
And if you see when I call this URL, it's saying I can simply print message.
Welcome to mini weather API.
It looks like a Json data.
Then I got Json data again. Looks like dictionary.
So it says message welcome to mini weather API.
So this is a basic API that you have built.
And this is your first API that you build
and is available to users to use now at that particular URL.
Next let's talk about defining
API routes and endpoints.
So first structure your API endpoints like for example will have for weather
will have slash weather where we get weather details for a default city.
Then we can have slash weather with the city name
which gets weather for a specific city.
Let's look at an example weather data.
So what I'm going to do is I am going to create here an example.
Just to show you here weather data for New York is temperature is 22.
Conditions is sunny London temperature is 15.
Condition is cloudy Tokyo temperature is 28.
Condition is clear.
Now, I don't know how we get this data,
but this is what we get and this is what we can reply back again.
Now, if I copy this and add it here
to my data set, I save it.
Go back to my browser, refresh it.
You have the weather data based for London, New
York and Tokyo coming from the API.
There you go.
Now let's go ahead and implement this entire application here.
So I'm going to delete everything here and start with importing flask Json.
If I add request from flask next I'm going to say app equal to flask name.
Then I'm going to create some mock weather data that we just had where
I'm going to create a weather data variable with weather of New York.
Temperature 22.
Condition is sunny.
London temperature is 15.
Condition is cloudy.
Tokyo temperature is 28 and condition is clear.
Close this.
Let's create a root endpoint app dot root slash
home function equal to return Json is my message.
Welcome to many weather API that we just created.
Next let's get weather for all the cities.
Now if you call a route slash weather with the method of the type get,
this is the get API that I'm calling because I'm requesting some data.
This will call this function get all weather and it will return.
Jason.
If I the weather data, it will just unify this weather data and return back.
And this will look something like this.
Next I'm going to say get weather for a specific city.
For this I'm going to create app dot root weather and in angular brackets city.
This will be depending on what user selects either
New York, London or Tokyo, the URL, whatever it is based on these cities,
I'm going to say get weather by city, city equal to city or lower.
Now if you say capital London, ello and capital N capital
Tokyo, I want to convert everything into lowercase.
And I'm going to say if city in weather data, I'm going to check if this city
key exists in the weather data.
Is it either New York, London or Tokyo?
Then I'm going to say return I city weather data for that city.
So I'm going to just return this particular data back next.
If it doesn't exist then I'm going to say error city not found.
And then finally run the app by saying
name equal to main app dot run debug equal to true.
So this is my entire code for this API.
Now if I want to run this code I can just go here save it.
Go back.
It's already restarted I'm going to open my browser.
And here first it goes to the basic road.
Here it says welcome message is welcome to mini weather app.
Now if I want to get weather for all of them, I can just to slash weather.
And I get weather for all the three cities.
If I say weather slash London.
If you see I'm putting capital L for London, it will give me London
because I convert it into lowercase right here.
So if I say Tokyo,
it will give me Tokyo with.
And again New York will give me New York weather. Perfect.
So that kind of gets me the data out from here.
So this brings us to returning Json responses, which we just saw
of getting Json responses back on or depending on the URL that we call,
depending on which URL you call, you'll get that data back.
So now let's go ahead to the next section,
which is day 40 project Mini weather API.
Here we will have multiple endpoints like weather and weather.
City will have proper Json responses and will have error handling
for invalid cities.
So I have already created this API here.
Let's go ahead
and just to add some more functionality, let's add an additional city.
So I'm going to go ahead here and add
Sydney to my list of cities.
So this will give me weather data for four different cities.
I have my basic route here I'm going to call it.
And when it's already called Mini weather API.
So that's perfect.
This will give me weather for all the cities.
That's perfect.
And for a particular city.
If you specify any of the city, I get weather
for that particular city by saying get weather by city.
It will give.
I think I got rid of my city equal to.
Let's make sure we put that back.
City equal to city dot lower
so that converts whatever the user passes the city name into lowercase.
And I have that data back if or else there's an error.
Now I also want to give user the functionality
to add new weather data for a new city.
So for that I am going to go ahead and here
create add.
Let me just move all this to the top.
And here I'm going to specify that I want to add
new weather data.
So this will happen through post.
Like all the things that we did request that we did will get.
This time I want to create a road which is app dot road.
And in this particular case we'll call the same URL slash weather.
But the method that will be called
in this particular case is.
So what happens is when you make a Post request, this will be called
when you make a Get request for the same URL.
This will be called so that's how this methods work.
So here I'm going to say add underscore
city underscore weather.
That's the function.
And inside here I'm going to say data equal
to request dot Json.
So this is the data that is provided to me and Json.
And then I'm going to say city equal to which city is this.
Let's see. Did not get
from the one that user has passed.
I'm getting the name of the city
and I'm converting it into lowercase because all my city names are lowercase.
Next, I want to get the temperature for that city.
That will be data dot get,
and I am looking for
temperature.
Next I need condition like is it cloudy?
Is it sunny?
Data dot get and I'll specify
condition perfect.
So I have all the three parameters that I need.
And here next I'm going to check.
If not, city's user did not pass anything
or not temperature
or not condition because I need all these three parameters.
If not, then I'm going to return saying Json ify
and here I will pass error
saying
missing city
temperature or condition.
So one of these things is missing.
Please provide me the right data.
And also this is an error of the day 400.
So every time you call a particular URL it will give you a status code.
So for example if I inspect here and if I look at my network and move this
a little bit below, and when I call this you'll probably see that called Tokyo.
But here it mentioned again let me run it again Tokyo.
This is status is 200.
But what if I enter something like this.
It says 404 because this city is not found.
And that's what, 4 or 4 days and if I give some other URL again not found.
And this is for of.
Also in this particular case I'm saying the error is of the type 4 or 4,
because for 400 because that city is you'll have some error in your system.
Next I'm going to say weather underscore data
for the city which means.
So for the city that user pass.
That means the data is available.
So I'm going to set the temperature
is equal to temperature and
condition.
So this will run when everything is successful.
Pretty much condition.
And then I'll return here Json.
If I in this particular case I will give a message to the user
saying f of f
string weather for city.
Whatever city user passed added success fully.
And this time I will say this is of the type two of one,
which means it was all successful and I was able to add the data.
Next, let's go ahead and I think I already wrote the code to run the app.
I'm going to close this here.
Save it, go back
here and see that it's running okay.
Perfect.
So this time, instead of using Chrome, what I'm going to do is I'm going to use
something called as postman.
So you can download postman from postman.com.
This helps you test APIs.
So I already have it installed and I have an account.
So you can go ahead and sign in and download this
UI based on which computer you have.
If I have Mac, I'm going to say
depending on Apple chip or Mac chip, I can download it.
I'm going to download this for here.
And once it's downloaded I can open it and just call the APIs that are available.
My URLs are right there.
Is it URL here.
This is the API endpoint that I have and I have slash method is where I can get
all the information.
So again once postman is installed or downloaded I'm going to open it
and it looks something like this.
It says unlock postman with an account.
I have an account, but I'm not going to login right now.
But what I'm going to do is I'm just going to go ahead, close this.
And here I'm going to say I'm going to create a new you are somewhere close.
Everything here, whatever is open.
And here I can
just give that URL that I have, which was let's go back to my terminal.
This was the URL.
If I just run this and you'll see it says Welcome to Mini Weather API
because I got I'm passing that, data when I called slash.
What if I called slash weather here you can go ahead and say slash weather.
Run it.
We get the weather for London, New York, Sydney and Tokyo again
if I say weather for a particular city.
So in this case, let's get weather for Sydney.
Send it.
And there you go. We get Sydney.
Now if I want to add noise, you see it says get here, I'm making a get call.
But if I do post and in this case
I am going to say slash weather because that's the post URL here.
Read post this thing and I am going to pass a weather
for a particular city so I can just pick this here
if you see the things that it needs.
If you check here I said data dot request, Json
data, dot get city temperature and condition.
So it needs all these different parameters.
So I'm going to go here select body and I'll say raw.
But I'm passing Json data here.
I'm just going to do this temperature
condition.
Oops.
That's not
do any mistake.
Let's increase the size I think this too big.
So this I hope it's visible to you.
And here I'm going to say city is
Atlanta
and the weather is 22.
And it's say rainy
send it's going to say weather for Atlanta added successfully
which means I'm going to set here get weather for Atlanta send
and I'll get the weather for Atlanta that is available.
And as I can call weather for then all of them and I will see
Atlanta, London, New York, Sydney and Tokyo.
So Atlanta weather got added here.
And that happened because we added we call this and new weather
data, functionality to our application.
So we were able to create dynamic endpoints here, handle multiple routes.
We did error handling will validate city names and input data.
We got Json responses, proper API response formatting.
We did some extensibility where we easily add more data points.
And bonus challenge for today is
add a put to update existing city weather.
Try that add delete to remove a city from the weather data,
and finally try to implement API authentication example
using API keys if you know what I'm talking about.
If not, that's okay.
We will be covering that in future.
So if you have any questions or had any issues implementing these or
understanding any of these, let me know in the messages and I'll get back to you.