I over.
Welcome back.
So in this video we're going to be installing a flask restful which is basically a library on top of
flasks.
That helps us build a restful API and it makes things much much easier.
So let me zoom in just for this box here.
And then the terminal.
And then we're basically going to first install the flask restful.
It's just a head package.
So when we go three and stock and then flask restful.
And each has already have a flask restful so it's just I can't get it from the cash that I have.
Yours should download it and basically you know if you want to check that it's there.
You can write eyes on or should be in three right where you install it using three.
So import flask restful underscore restful as f.
And there's no errors.
All right good.
OK.
So now that we have a flask restful Let's see how this will translate or how we can build a restful
API a very basic one in flask.
So we're going to first imported from flask restful implored Ravana Boyd method called API which is
actually a constructor and we're also going to call for now let's just leave it for API and let's import
whenever we need anything.
And then we're going to say API API is equal to the constructor API and this constructor takes an our
app.
Okay great.
So far so good.
Now the next step is if you remember in this here we defined here in this video here we define our resources
as a plus minus divide and multiply.
So the first step in in developing this is to is to do exactly that to define our resources.
So to do to do that we're going to also import from flask restful resource to indicate that some class
is a resource.
Now we're gonna say well we have four classes right.
Class add which is a resource.
Let's just fast for now and then we have a class subtract which is also going to inherit from resource.
And then we're going to pass.
And then the same thing for multiply
and the same thing for divide.
OK.
So so far so good.
Now inside these functions we're not going to do an app to route just like we did here.
So instead we're going to be defining functions called Get or post depending on whether we want this
resource to support so let's say for example let's go back here.
We want this to support post.
Then I would come here and then I would write define post as self.
And then I would assume that if I'm here.
So if I'm here then the resource add was requested using the method post.
It's just simple as that.
Now let's say for example I can support for and either post or again.
Then I would define another function called get.
And again here I would be if I'm here then the resource and wasn't requested using get.
Similar thing for Prid put a new lease.
I can do it or I can do the same thing for
delete.
OK so now that we know that we don't need any get or put or delete.
In this case right.
Because if we look at our diagram only hand posts here.
So for this case we're only going to leave it as post right now.
Once I'm here then I'm assuming that I've entered this function here so what do I do.
Well I want to get the two numbers right.
So for example at the store there's no errors.
So first of all I want to get the posted data.
So first steps step 1 get the post and lose data.
So we're going to send a post to data as equals you request get Jaisalmer just like we saw pre-decided
you OK.
And now we're going to say x is equal to the posted data and then we're going to access X. And I say
Y is equal to the posted data.
Why now we're going to say that X is and x to make sure that it's in.
And even though we're supposed to have received but let's just verify that their ends and then return
is equal to x plus y and then turn Jaison or you return even Mao is equal to
some and then the sum is returned.
Right.
Then finally return on a FI return now.
So before that we also need to put the status code and we said that if we succeed in this then we would
say status code and then put 200 right because we succeeded in this in this if we if we get here and
we have added the two numbers then the status quo should be 200.
There should be a comma here now instead of some what we can do is we could always put a message here
to just simulate what HDB really is like.
So we always put a message here and we will get the response.
And we also have the status which will store the status code.
So this message if if if it's a 200 then it would have the response.
If it's not a 200 if it's a variable like 301 or a 302 then we would stand here.
Right.
And a message for example.
You forgot to say you know the parameters are not there or your numbers are are not integers or so on.
OK so if we're here then we're good.
So let's first test this less Now before we can test this.
So for defined our resource here.
This is our resource but if you look here we see that map to slash and right.
So we need to do this mapping over here.
Now how do we do this mapping.
Well you go back here and then we're going to say after we have defined all our resources we're going
to say API.
So if you don't remember AB AIs this API here then we're going to write API is equal to add and then
underscore and then resource and then we're going to say which resource we want to do so we want to
add this resource here and we're going to do add and then in here we're going to say where we want that
resource to be routed or ahead.
So in this example we said we aren't at slash and so let's go back here and then we do slash.
And so now we've added this resource.
It's listening and slashed.
And if you add this resource gets a post request this function is going to start running and then we're
going to get the posted data and then step 2 gone and and the posted data and then step three is we're
going to return our response to the user which is the message and the status quo.
OK.
So let's make sure everything is running here no problems.
Let's see if there's any problems clearing this.
And then flask question and we have a couple of errors here.
So the first thing is oh ok so it shouldn't be API does it add resource.
It's not equal to and resource.
So it's API and resource.
So we're adding our resource to that API.
And they're not saying you have any other problems and we don't.
OK.
So let's open up postman here let's try posting some information.
So we're going to select coast from here.
And then here we're going to use localhost five thousand.
And then at the far southern port then we're going to listen to that right.
This is how we this is the end point slash add right at it and then we're going to read in the message
of the body or the body we're going to be sending a message wrong which wishes and Jaison format.
And then inside this case I'm really going to have X and he's going to be five and then yours and then
Y and it's going to be reasonable six.
And then let's see what happens when we sent this and their response and we get their response back.
So the message is that then and the status code is 200 So we got to success to use God.
And this is the dish.
Now what happens if x is not given it right.
If we don't have X and we send it.
Well this will cause internal server error right.
We don't want that.
We want you want to give the user an understanding of what to do when when there's an error right.
We want to give him an area that makes sense.
In other words.
OK so let's see how we can we can change that.
Right.
So first of all once we get our posted data let's make a function code check posted data anti-acid post
and data right.
So what's so this here is step 1 be right.
Verify validity of.
And also we should pass in what functions.
So this is and.
Right.
So verify the validity of posted data.
Right.
OK.
So let's write this function over here.
So we're gonna define a function called a shack.
Most of the data is going to have the posted data.
And we're also going to have the function name so function name.
OK so we're going do is the function name is equal to add.
Right.
And what do we do.
Well in that case we want to we have two types of errors right.
We have 200 which is OK.
So we don't return any errors but there is also 301.
So 301 would be a problem right because we have a missing parameter.
So let's go back here and then we say if x not in posted data in the posted data dictionary.
Or why not an posted data.
And what do we do then we would apply return 3 3 0 1 or else we simply response.
We're we're with 200.
Right.
So in the function here we're gonna get status code is equal to check the posted data and then re going
to see OK if status code is not 200 That means there are some here that are going to prepare a response
to the user's server or returned Jason and then
so we're going to prepare a response and say an error has happened or let's say let's make it more clear.
So a message and then and there happened.
And then the status quo would so let's make sure the capitalization record was put X back and see how
stupid it looks like.
So copy this exactly and then status code and then we put the status quo that the error status code
that we got and then return on if I returned Jason.
So now we're going to check the posted data if there is any here.
Right.
If x is not in the post data or why is not the posted data then we're going to return 3 or what is the
error elsewhere when I return 200 pages.
OK.
Now I want to see this tax code that I returned.
If it's not 200 then that means there's a failure.
I'm going to say the error happened and then I'm going to return to the user status code that led to
this error.
In this case it means that there's a missing parameter.
Now if this is 200 then that if I'm here right.
So is if I am here then status code is got to do it right because if it wasn't 200 then we would have
returned here.
So here and then this data is 200 so we can now access easily x and y add them together and then return
their response to the user as the message.
OK so let's try and test this and see if there's any ears here syntax or or any other errors.
So this flask can run so return some here.
So this should be equal to.
Right.
And let's see run again and there's no problems here.
Let's go first verify that the normal case is working.
Then we get 11 and 200 maybe try five and fives and then you get 10 and 200 OK.
So what if we get delete x now and then we see what happens what's the status.
So hopefully he shouldn't tell us your message and there happened and status code 3 I want.
So the user would look at the state and say oh three you won so that means that I didn't give that the
the API the parameter x.
Right.
So you what if I send the empty.
Then again get 3 0 1.
So it handles all the edge cases perfectly whether or not there or why is not.
There were 2 3 0 1.
Now you can make this very very specific right.
It doesn't have to stop it.
You know you could make one very close for X one Eyrecourt for why you know you can you can really divide
this chart or here in two very very detailed.
And that's very good for your eyes.
In fact most developers love a guy like that because they can debug their code right away.
But for now we're just going to get like if any of the parameters is not there we're just going to return
3 or what.
OK so now that we're finished this and one in the next video room and I start completing the subtract
one of the multiply 1 until we have the fully working API.
OK.
So until the next video recording.