Everyone welcome back.
So in this second part the last part of this video we're going to be implementing the last three resources
which are subtract multiply and divide it now subtract multiply are very similar to.
And in fact the only difference there are very few differences.
But if you remember that divide we discussed at that great divide we should be careful about that there's
an extra layer of code which is three hundred and two which is when y is zero.
We have to be a bit careful here.
When when when we're doing when we're doing the checking for it.
OK.
So let's go back to our app here.
OK.
So let's start implementing subtract.
Well we don't have to really you know usually we don't like to copy code and we can we can you know
instead do this process much much easier.
But for now was just copy the code and see how we would change that to suit our need here in this case.
Let's start with the premise.
Ok so you do this and then paste it.
OK.
So if I'm here then the resource subtract Shipley's and we should replace this one with subtract was
requested using the method post and then the step one is to get the posted to.
Step 1 be to verify the validity of the posted data.
So let's go back to the validity data.
But this time we're going to be passing this conscience contract because we're in the subtract resource
not the and resource.
So let's go back and see.
Whoops.
So right so we need to I think we need to do.
So let's give me a second here.
So pseudo atom right this atom needs to be in Suder.
We're developing it
OK so let's first case is going to happen to them.
Let's first copy the code from the other one because we can't even accept unless we're in Suta right.
So we're gonna copy this code here and then we're going to switch to the other one here.
And then you just paste everything and say you should take with no problem let's close this one.
This one is without pseudo axes don't save and then OK let's let's keep going here.
So it's just a pseudo problem it's nothing too serious.
So we're going to have you Gotcha the posted data for the track method.
So let's go back and see how the subject method is used.
So if you go back here and then which you check the posted data if the function name is that then we
have to check if x is not in the posted data or Y.
But actually this is the same conditions that we're checking for in these Tracht functions.
If we look here subtract function has exactly the same error code status codes 200 and 300 and they're
exactly relating to the same exact missing parameters or OK's.
Or you're missing parameters are OK.
So in fact we can we couldn't really avoid writing this whole thing again if we just say four function
is equal to subtract that is tracked and then that's it.
If x is not in there then we return 3 0 1 code to indicate a missing parameter.
And if this is all everything's fine then returned 200.
OK.
So let's go back to our subtract resource now once we've made sure the status code is the status quo
is not 200 meaning is 300.
Hear them return message and there happened.
And where we turned the state code we got from the Hanshin.
Next we would get the value of x y.
Change it to an integer and then we would do X minus Y's here and b subtract the posted data and then
return map as a message returned and status code 200.
So the message we're getting traction.
Man is and the state has good success.
Finally let's go to the ER.
Finally please let's do the same thing for us.
So let's do the same thing for multiply.
Let's copy this code every year.
Embraced it here and then and so this resource is now multiply.
Now if you go back to multiply this here we see it's also two hundred and three oh wow.
So what was your place.
Two hundred three answers.
Two hundred.
OK.
Three are One missing parameter.
So we're going to do first get the posted data then we're going to check the validity of this in that
show posted data and that's it.
But then we have to account for the function.
So instead of subtract is going to be multiply your hopes as is the substract one.
So it's going to be a multiply one here
and then we got caucuses and then we're going to check here in the check posted function or the function
name is equal to multiply.
Then we do that again the same check check if X or Y are not there.
Else return 200.
OK so now we go here and then instead of subtraction multiplication multiply and then return map is
equal to this.
Now you can notice here that the only difference we're doing is we're doing this tracked here and this
multiply here so you can actually do.
You can be a bit smarter about this.
You can do like a map and then for example map every name with a function to an operator to perform
a function.
Right.
To perform the operator here.
But you know let's let's just go for the minimum viable product right.
Let's just go for the most you know worst code we can write just to make sure it's working and to get
the idea of the whole workflow and how things really work and usually things you know resources are
very that related if they're related then we usually encapsulate them all in one class.
So don't worry about the the the copy pasting for now.
Usually we don't really copy paste the code.
I think I said this should be so tracked because this is the subtract method.
And the other one.
And again this is just the problems of copy pasting code.
That's why we usually copy paste code that you know.
Let's let's just focus on the ideas for now and later on we need to optimize this.
OK so once we have multiplication The last thing to do is to check for division and division.
Isn't that a bit different.
Right.
Let's copy this one last time.
And then paste it here.
Are we going to finally resource divide.
We're going to get decent data and we're going to pass it as we're in the subject the posted data in
case of the division.
And then Les mentioned let's just fix the function here first.
Once we get to see this code we're going to return an error if there's any errors whether it's 3 1 or
3 2.
So here we have 3 1 and 3 2 3 or 1 is missing parameter 3 and 2 is why was zero.
So we can't divide by zero.
So we get X Y and then the response is X Y and return the response and then we can do this.
Since this is my this this is an integer and this is an integer so which do times one point.
It was done on point out over why just to make sure that it's a float.
And then we turned the response back.
OK.
So let's go now that she posted Daytona function and then this one here is going to have two cases or
else is a function name whose
name is equal to division.
Right.
That's what you called it from here.
Yes.
Division.
Let's make sure we spell that correctly.
So is the function name is division.
We're going to check for two codes right.
If so the first one is if x not in Kostin data.
Or why not in person.
Then we simply return three or what else.
Yes or Ellis is host and data why is equal to zero.
In that case we're would return 3 0 2.
Because that was cast into an integer.
Make sure everything's fine.
So if integer is zero then you tend to go to else where we turn 200.
So success.
OK so we have here 3 1 denoting that one of the parameters was not there 3 or 2 meaning why was zero
or 200 which is a success.
So now we really finished building the API or the resources.
Now the only thing left is to add these resources here right.
So so far we haven't added it to our API.
So there's one last step which is to add the resources and then add subtract to slash unless you get
our API was slash subtract slash subtract should do AP either and resource multiply and slash multiply.
And finally API and resource
divide divide and slash division right.
And now with initial basically build our API.
So let's start testing lets make sure that lets try first exporting flask code to and up P-ROC flask
this flask from a flask.
Okay OK so I've done as else if.
Right.
This it's that's in C++ 9.
So it's l s l f this.
OK.
Let's see if we have any other problems.
And we don't.
OK so let's now open post them and try and test our application.
Make sure everything's working fine.
OK so once we have once we have an open can go to slash multiply Let's try multiply first.
And then in the row data with us and x 10 and y 12 to that should return us 120.
So let's put this year and send the request and we get indeed Under-20 is the message and 200 is escrowed.
Now let's try.
Tracked 10 and 12.
That should give us negative 2 so negative 2 and their sponsors do.
Now let's check divide.
Right.
So that's one divide.
For example 10 by 5.
When we said that we have an error.
Let's check the air first here.
So it says here that 0.
So I I my routed to the wrong place.
OK.
Good.
Yeah.
So it's slash divisions if you look at last year it tells me post-blast divide four or four.
It doesn't Nowhere's less divide.
So it should be slash division.
So let's change that to slash division and then send the request and we get the response to.
Right.
This is the response 10 to about it.
My 5 is 2 and the status goes 200.
Now what happens if it's 10 divided by three and it should be three points or anything.
So these are three.
And so what about if we put 8 or 0.
Now that's that's what we should have cared about.
Right.
So once we have that we get this message and there have been statis go to 302.
So our checking worked.
What if we don't have one of the parameters
and then well that one should should have must check are posted.
Message me me me
OK so I think the problem was to come over here that we should delete this summer.
So let's try again in the GSA and then we get it on the air happens 3 or 1 which means there is a missing
parameter in the sense data.
So we now have a fully working API that is documented.
You can easily change this.
This diagram over here to the documentation for the user to see.
Usually you can use something like swagger which is an online Web site to help you write the documentation
for the API.
But yeah we now have a working API that supports four functions and subtract divide and multiply with
error checking we're returning correct responses to the users and it won't crash on any given input.
Now of course you could argue that you know I can crash it of course if if if I give my application
something like you know X as you know ABC.
Right.
And instead of a number you can be in a string right.
Or for example why five right.
So what would happen.
Of course it would crash the you know an internal server.
Right.
So you know it's up to you for how much validation you want to do you know.
Are you trusting that the user always send x and y are you trusting that the user assigned always x
and y but are the always numbers or are they always in the years.
Are they always not strings.
You know you have to know this function could really you could spend a lot of time on just to make sure
that nothing basically crashes your server.
For example here you get a value error invalid literal for it.
Right.
Because a b c is not integer.
So it's really up to you to to to to you know work on this error checking point here and make your error
codes very very specific to the user and make sure what you can assume and what you can't assume on
the data.
But it's generally better if you you know anticipate everything can't trust that the user will always
send you good information or are valid parameters.
You have to always check it internally.
Don't let anything crash your server.
So that's basically the bare minimum in this for this API.
And yeah I'll see you in the next video.
So until that happens Tony.