WEBVTT

00:05.340 --> 00:06.940
Everyone welcome back.

00:06.950 --> 00:13.910
So in this second part the last part of this video we're going to be implementing the last three resources

00:14.360 --> 00:20.840
which are subtract multiply and divide it now subtract multiply are very similar to.

00:20.890 --> 00:24.620
And in fact the only difference there are very few differences.

00:24.680 --> 00:31.380
But if you remember that divide we discussed at that great divide we should be careful about that there's

00:31.400 --> 00:36.370
an extra layer of code which is three hundred and two which is when y is zero.

00:36.530 --> 00:38.720
We have to be a bit careful here.

00:38.720 --> 00:43.900
When when when we're doing when we're doing the checking for it.

00:44.280 --> 00:44.650
OK.

00:44.660 --> 00:48.830
So let's go back to our app here.

00:48.890 --> 00:49.430
OK.

00:49.520 --> 00:52.510
So let's start implementing subtract.

00:52.730 --> 00:59.030
Well we don't have to really you know usually we don't like to copy code and we can we can you know

00:59.060 --> 01:01.540
instead do this process much much easier.

01:01.550 --> 01:10.310
But for now was just copy the code and see how we would change that to suit our need here in this case.

01:10.310 --> 01:12.100
Let's start with the premise.

01:12.240 --> 01:15.040
Ok so you do this and then paste it.

01:15.310 --> 01:15.930
OK.

01:16.100 --> 01:25.730
So if I'm here then the resource subtract Shipley's and we should replace this one with subtract was

01:25.730 --> 01:31.510
requested using the method post and then the step one is to get the posted to.

01:32.040 --> 01:37.390
Step 1 be to verify the validity of the posted data.

01:37.400 --> 01:39.510
So let's go back to the validity data.

01:39.520 --> 01:45.270
But this time we're going to be passing this conscience contract because we're in the subtract resource

01:45.370 --> 01:46.930
not the and resource.

01:46.940 --> 01:49.220
So let's go back and see.

01:49.610 --> 01:50.630
Whoops.

01:50.660 --> 01:56.180
So right so we need to I think we need to do.

01:56.410 --> 01:59.790
So let's give me a second here.

02:00.200 --> 02:08.790
So pseudo atom right this atom needs to be in Suder.

02:08.900 --> 02:09.890
We're developing it

02:13.510 --> 02:18.100
OK so let's first case is going to happen to them.

02:18.370 --> 02:25.690
Let's first copy the code from the other one because we can't even accept unless we're in Suta right.

02:25.720 --> 02:34.000
So we're gonna copy this code here and then we're going to switch to the other one here.

02:34.810 --> 02:42.180
And then you just paste everything and say you should take with no problem let's close this one.

02:42.190 --> 02:48.970
This one is without pseudo axes don't save and then OK let's let's keep going here.

02:48.970 --> 02:52.110
So it's just a pseudo problem it's nothing too serious.

02:52.180 --> 02:56.590
So we're going to have you Gotcha the posted data for the track method.

02:56.650 --> 03:01.300
So let's go back and see how the subject method is used.

03:01.300 --> 03:08.020
So if you go back here and then which you check the posted data if the function name is that then we

03:08.020 --> 03:11.750
have to check if x is not in the posted data or Y.

03:12.070 --> 03:16.860
But actually this is the same conditions that we're checking for in these Tracht functions.

03:16.870 --> 03:23.070
If we look here subtract function has exactly the same error code status codes 200 and 300 and they're

03:23.080 --> 03:29.320
exactly relating to the same exact missing parameters or OK's.

03:29.320 --> 03:31.360
Or you're missing parameters are OK.

03:31.390 --> 03:39.570
So in fact we can we couldn't really avoid writing this whole thing again if we just say four function

03:40.690 --> 03:47.580
is equal to subtract that is tracked and then that's it.

03:47.570 --> 03:54.180
If x is not in there then we return 3 0 1 code to indicate a missing parameter.

03:55.240 --> 03:59.330
And if this is all everything's fine then returned 200.

03:59.750 --> 04:00.130
OK.

04:00.130 --> 04:06.370
So let's go back to our subtract resource now once we've made sure the status code is the status quo

04:06.420 --> 04:08.880
is not 200 meaning is 300.

04:09.010 --> 04:12.240
Hear them return message and there happened.

04:12.280 --> 04:15.990
And where we turned the state code we got from the Hanshin.

04:16.090 --> 04:19.250
Next we would get the value of x y.

04:19.390 --> 04:29.140
Change it to an integer and then we would do X minus Y's here and b subtract the posted data and then

04:29.180 --> 04:35.490
return map as a message returned and status code 200.

04:35.620 --> 04:38.160
So the message we're getting traction.

04:38.390 --> 04:41.980
Man is and the state has good success.

04:42.220 --> 04:44.640
Finally let's go to the ER.

04:44.650 --> 04:49.840
Finally please let's do the same thing for us.

04:50.350 --> 04:52.600
So let's do the same thing for multiply.

04:52.600 --> 04:54.960
Let's copy this code every year.

05:02.840 --> 05:08.310
Embraced it here and then and so this resource is now multiply.

05:08.480 --> 05:15.090
Now if you go back to multiply this here we see it's also two hundred and three oh wow.

05:15.140 --> 05:16.580
So what was your place.

05:16.590 --> 05:18.380
Two hundred three answers.

05:18.380 --> 05:18.910
Two hundred.

05:18.920 --> 05:19.400
OK.

05:19.500 --> 05:21.640
Three are One missing parameter.

05:21.800 --> 05:28.950
So we're going to do first get the posted data then we're going to check the validity of this in that

05:29.020 --> 05:31.100
show posted data and that's it.

05:31.190 --> 05:33.760
But then we have to account for the function.

05:33.890 --> 05:40.980
So instead of subtract is going to be multiply your hopes as is the substract one.

05:41.030 --> 05:43.880
So it's going to be a multiply one here

05:48.860 --> 05:59.150
and then we got caucuses and then we're going to check here in the check posted function or the function

05:59.150 --> 06:03.980
name is equal to multiply.

06:04.070 --> 06:08.550
Then we do that again the same check check if X or Y are not there.

06:08.600 --> 06:10.640
Else return 200.

06:11.350 --> 06:20.360
OK so now we go here and then instead of subtraction multiplication multiply and then return map is

06:20.360 --> 06:21.940
equal to this.

06:21.950 --> 06:27.560
Now you can notice here that the only difference we're doing is we're doing this tracked here and this

06:27.560 --> 06:29.700
multiply here so you can actually do.

06:29.900 --> 06:31.840
You can be a bit smarter about this.

06:31.840 --> 06:38.720
You can do like a map and then for example map every name with a function to an operator to perform

06:38.780 --> 06:39.380
a function.

06:39.380 --> 06:39.650
Right.

06:39.680 --> 06:42.920
To perform the operator here.

06:43.040 --> 06:46.540
But you know let's let's just go for the minimum viable product right.

06:46.550 --> 06:52.790
Let's just go for the most you know worst code we can write just to make sure it's working and to get

06:52.850 --> 06:59.930
the idea of the whole workflow and how things really work and usually things you know resources are

07:00.080 --> 07:06.220
very that related if they're related then we usually encapsulate them all in one class.

07:06.500 --> 07:11.840
So don't worry about the the the copy pasting for now.

07:11.930 --> 07:14.990
Usually we don't really copy paste the code.

07:15.230 --> 07:21.320
I think I said this should be so tracked because this is the subtract method.

07:21.800 --> 07:22.820
And the other one.

07:22.820 --> 07:26.450
And again this is just the problems of copy pasting code.

07:26.450 --> 07:31.200
That's why we usually copy paste code that you know.

07:31.280 --> 07:36.280
Let's let's just focus on the ideas for now and later on we need to optimize this.

07:36.710 --> 07:42.870
OK so once we have multiplication The last thing to do is to check for division and division.

07:42.870 --> 07:44.610
Isn't that a bit different.

07:44.620 --> 07:45.060
Right.

07:45.140 --> 07:48.380
Let's copy this one last time.

07:52.610 --> 07:55.450
And then paste it here.

07:55.550 --> 07:59.890
Are we going to finally resource divide.

07:59.960 --> 08:05.410
We're going to get decent data and we're going to pass it as we're in the subject the posted data in

08:05.450 --> 08:07.420
case of the division.

08:07.940 --> 08:11.570
And then Les mentioned let's just fix the function here first.

08:11.630 --> 08:17.630
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

08:17.630 --> 08:18.390
3 2.

08:18.410 --> 08:25.390
So here we have 3 1 and 3 2 3 or 1 is missing parameter 3 and 2 is why was zero.

08:25.400 --> 08:28.080
So we can't divide by zero.

08:29.360 --> 08:37.850
So we get X Y and then the response is X Y and return the response and then we can do this.

08:38.270 --> 08:44.020
Since this is my this this is an integer and this is an integer so which do times one point.

08:44.970 --> 08:51.950
It was done on point out over why just to make sure that it's a float.

08:51.950 --> 08:54.930
And then we turned the response back.

08:55.160 --> 08:55.840
OK.

08:55.910 --> 09:05.220
So let's go now that she posted Daytona function and then this one here is going to have two cases or

09:05.300 --> 09:11.300
else is a function name whose

09:13.940 --> 09:18.480
name is equal to division.

09:18.890 --> 09:19.160
Right.

09:19.160 --> 09:22.250
That's what you called it from here.

09:22.540 --> 09:22.880
Yes.

09:22.880 --> 09:24.000
Division.

09:24.720 --> 09:27.910
Let's make sure we spell that correctly.

09:28.460 --> 09:30.690
So is the function name is division.

09:30.740 --> 09:32.840
We're going to check for two codes right.

09:32.940 --> 09:39.520
If so the first one is if x not in Kostin data.

09:40.040 --> 09:45.590
Or why not in person.

09:46.560 --> 09:51.560
Then we simply return three or what else.

09:51.890 --> 10:03.330
Yes or Ellis is host and data why is equal to zero.

10:03.720 --> 10:06.230
In that case we're would return 3 0 2.

10:06.450 --> 10:10.200
Because that was cast into an integer.

10:10.200 --> 10:13.170
Make sure everything's fine.

10:13.280 --> 10:22.220
So if integer is zero then you tend to go to else where we turn 200.

10:22.230 --> 10:26.050
So success.

10:26.370 --> 10:34.050
OK so we have here 3 1 denoting that one of the parameters was not there 3 or 2 meaning why was zero

10:34.290 --> 10:37.170
or 200 which is a success.

10:37.170 --> 10:41.460
So now we really finished building the API or the resources.

10:41.550 --> 10:45.000
Now the only thing left is to add these resources here right.

10:45.010 --> 10:47.700
So so far we haven't added it to our API.

10:47.710 --> 10:57.860
So there's one last step which is to add the resources and then add subtract to slash unless you get

10:57.900 --> 11:13.110
our API was slash subtract slash subtract should do AP either and resource multiply and slash multiply.

11:13.550 --> 11:16.560
And finally API and resource

11:19.620 --> 11:26.420
divide divide and slash division right.

11:26.660 --> 11:30.250
And now with initial basically build our API.

11:30.420 --> 11:41.520
So let's start testing lets make sure that lets try first exporting flask code to and up P-ROC flask

11:42.320 --> 11:46.040
this flask from a flask.

11:46.610 --> 11:49.830
Okay OK so I've done as else if.

11:49.840 --> 11:50.130
Right.

11:50.200 --> 11:53.770
This it's that's in C++ 9.

11:54.020 --> 11:59.580
So it's l s l f this.

12:00.160 --> 12:00.640
OK.

12:00.780 --> 12:02.660
Let's see if we have any other problems.

12:02.730 --> 12:03.910
And we don't.

12:04.410 --> 12:09.660
OK so let's now open post them and try and test our application.

12:09.660 --> 12:12.020
Make sure everything's working fine.

12:24.910 --> 12:32.690
OK so once we have once we have an open can go to slash multiply Let's try multiply first.

12:33.430 --> 12:45.260
And then in the row data with us and x 10 and y 12 to that should return us 120.

12:45.430 --> 12:56.650
So let's put this year and send the request and we get indeed Under-20 is the message and 200 is escrowed.

12:56.680 --> 12:58.590
Now let's try.

12:59.020 --> 13:01.600
Tracked 10 and 12.

13:01.600 --> 13:06.470
That should give us negative 2 so negative 2 and their sponsors do.

13:06.820 --> 13:08.340
Now let's check divide.

13:08.380 --> 13:08.930
Right.

13:09.130 --> 13:11.190
So that's one divide.

13:11.190 --> 13:13.660
For example 10 by 5.

13:14.330 --> 13:16.930
When we said that we have an error.

13:16.960 --> 13:19.020
Let's check the air first here.

13:19.360 --> 13:21.710
So it says here that 0.

13:21.820 --> 13:25.900
So I I my routed to the wrong place.

13:25.900 --> 13:26.790
OK.

13:27.290 --> 13:27.620
Good.

13:27.650 --> 13:28.100
Yeah.

13:28.300 --> 13:34.300
So it's slash divisions if you look at last year it tells me post-blast divide four or four.

13:34.300 --> 13:36.150
It doesn't Nowhere's less divide.

13:36.160 --> 13:38.640
So it should be slash division.

13:38.770 --> 13:45.420
So let's change that to slash division and then send the request and we get the response to.

13:45.460 --> 13:45.700
Right.

13:45.700 --> 13:48.180
This is the response 10 to about it.

13:48.190 --> 13:51.820
My 5 is 2 and the status goes 200.

13:51.820 --> 13:57.150
Now what happens if it's 10 divided by three and it should be three points or anything.

13:57.150 --> 13:57.890
So these are three.

13:57.890 --> 14:01.660
And so what about if we put 8 or 0.

14:01.660 --> 14:04.470
Now that's that's what we should have cared about.

14:04.480 --> 14:05.590
Right.

14:05.650 --> 14:11.010
So once we have that we get this message and there have been statis go to 302.

14:11.020 --> 14:13.960
So our checking worked.

14:14.290 --> 14:16.430
What if we don't have one of the parameters

14:19.390 --> 14:24.280
and then well that one should should have must check are posted.

14:24.290 --> 14:25.590
Message me me me

14:38.920 --> 14:43.880
OK so I think the problem was to come over here that we should delete this summer.

14:43.970 --> 14:50.800
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

14:53.110 --> 14:55.060
parameter in the sense data.

14:55.540 --> 14:59.670
So we now have a fully working API that is documented.

14:59.710 --> 15:02.420
You can easily change this.

15:03.100 --> 15:09.500
This diagram over here to the documentation for the user to see.

15:09.670 --> 15:14.860
Usually you can use something like swagger which is an online Web site to help you write the documentation

15:14.860 --> 15:15.540
for the API.

15:15.550 --> 15:22.120
But yeah we now have a working API that supports four functions and subtract divide and multiply with

15:22.150 --> 15:29.860
error checking we're returning correct responses to the users and it won't crash on any given input.

15:29.860 --> 15:36.760
Now of course you could argue that you know I can crash it of course if if if I give my application

15:36.760 --> 15:42.020
something like you know X as you know ABC.

15:42.060 --> 15:42.320
Right.

15:42.350 --> 15:46.610
And instead of a number you can be in a string right.

15:46.960 --> 15:50.170
Or for example why five right.

15:50.190 --> 15:51.030
So what would happen.

15:51.030 --> 15:54.870
Of course it would crash the you know an internal server.

15:54.880 --> 15:55.680
Right.

15:55.720 --> 16:01.030
So you know it's up to you for how much validation you want to do you know.

16:01.030 --> 16:06.880
Are you trusting that the user always send x and y are you trusting that the user assigned always x

16:06.880 --> 16:10.870
and y but are the always numbers or are they always in the years.

16:11.050 --> 16:13.000
Are they always not strings.

16:13.000 --> 16:18.340
You know you have to know this function could really you could spend a lot of time on just to make sure

16:18.340 --> 16:21.560
that nothing basically crashes your server.

16:21.640 --> 16:25.430
For example here you get a value error invalid literal for it.

16:25.570 --> 16:26.130
Right.

16:26.170 --> 16:28.220
Because a b c is not integer.

16:28.360 --> 16:37.060
So it's really up to you to to to to you know work on this error checking point here and make your error

16:37.060 --> 16:43.390
codes very very specific to the user and make sure what you can assume and what you can't assume on

16:43.390 --> 16:44.030
the data.

16:44.080 --> 16:49.660
But it's generally better if you you know anticipate everything can't trust that the user will always

16:49.660 --> 16:53.610
send you good information or are valid parameters.

16:53.620 --> 16:55.410
You have to always check it internally.

16:55.410 --> 16:58.170
Don't let anything crash your server.

16:58.220 --> 17:03.080
So that's basically the bare minimum in this for this API.

17:03.200 --> 17:05.030
And yeah I'll see you in the next video.

17:05.080 --> 17:06.500
So until that happens Tony.
