WEBVTT

00:05.050 --> 00:05.840
I over.

00:05.900 --> 00:06.830
Welcome back.

00:07.130 --> 00:15.070
So in this video we're going to be installing a flask restful which is basically a library on top of

00:15.070 --> 00:15.740
flasks.

00:15.740 --> 00:20.870
That helps us build a restful API and it makes things much much easier.

00:20.870 --> 00:25.600
So let me zoom in just for this box here.

00:25.700 --> 00:26.760
And then the terminal.

00:26.840 --> 00:31.180
And then we're basically going to first install the flask restful.

00:31.180 --> 00:33.200
It's just a head package.

00:33.200 --> 00:38.850
So when we go three and stock and then flask restful.

00:39.500 --> 00:46.070
And each has already have a flask restful so it's just I can't get it from the cash that I have.

00:46.070 --> 00:51.360
Yours should download it and basically you know if you want to check that it's there.

00:51.420 --> 00:58.950
You can write eyes on or should be in three right where you install it using three.

00:59.070 --> 01:08.220
So import flask restful underscore restful as f.

01:08.870 --> 01:10.160
And there's no errors.

01:10.170 --> 01:11.580
All right good.

01:12.080 --> 01:12.710
OK.

01:12.950 --> 01:18.590
So now that we have a flask restful Let's see how this will translate or how we can build a restful

01:18.590 --> 01:22.410
API a very basic one in flask.

01:22.460 --> 01:32.540
So we're going to first imported from flask restful implored Ravana Boyd method called API which is

01:32.540 --> 01:38.750
actually a constructor and we're also going to call for now let's just leave it for API and let's import

01:38.810 --> 01:40.650
whenever we need anything.

01:41.060 --> 01:52.870
And then we're going to say API API is equal to the constructor API and this constructor takes an our

01:52.880 --> 01:53.810
app.

01:55.450 --> 01:56.120
Okay great.

01:56.150 --> 01:57.710
So far so good.

01:57.740 --> 02:06.770
Now the next step is if you remember in this here we defined here in this video here we define our resources

02:06.830 --> 02:10.430
as a plus minus divide and multiply.

02:10.430 --> 02:17.290
So the first step in in developing this is to is to do exactly that to define our resources.

02:17.450 --> 02:25.160
So to do to do that we're going to also import from flask restful resource to indicate that some class

02:25.190 --> 02:27.350
is a resource.

02:27.350 --> 02:30.330
Now we're gonna say well we have four classes right.

02:30.370 --> 02:34.980
Class add which is a resource.

02:35.760 --> 02:44.650
Let's just fast for now and then we have a class subtract which is also going to inherit from resource.

02:44.690 --> 02:47.120
And then we're going to pass.

02:47.480 --> 02:49.850
And then the same thing for multiply

02:54.230 --> 02:57.350
and the same thing for divide.

02:58.210 --> 02:58.860
OK.

02:58.930 --> 03:01.350
So so far so good.

03:01.370 --> 03:06.580
Now inside these functions we're not going to do an app to route just like we did here.

03:06.830 --> 03:13.550
So instead we're going to be defining functions called Get or post depending on whether we want this

03:13.850 --> 03:17.810
resource to support so let's say for example let's go back here.

03:17.930 --> 03:20.160
We want this to support post.

03:20.270 --> 03:29.660
Then I would come here and then I would write define post as self.

03:30.170 --> 03:32.890
And then I would assume that if I'm here.

03:32.890 --> 03:42.850
So if I'm here then the resource add was requested using the method post.

03:42.890 --> 03:45.010
It's just simple as that.

03:45.020 --> 03:49.000
Now let's say for example I can support for and either post or again.

03:49.040 --> 03:51.920
Then I would define another function called get.

03:52.700 --> 04:04.380
And again here I would be if I'm here then the resource and wasn't requested using get.

04:04.880 --> 04:07.270
Similar thing for Prid put a new lease.

04:07.280 --> 04:14.090
I can do it or I can do the same thing for

04:16.820 --> 04:20.500
delete.

04:21.890 --> 04:25.640
OK so now that we know that we don't need any get or put or delete.

04:25.640 --> 04:26.480
In this case right.

04:26.490 --> 04:30.560
Because if we look at our diagram only hand posts here.

04:30.590 --> 04:35.360
So for this case we're only going to leave it as post right now.

04:35.360 --> 04:42.330
Once I'm here then I'm assuming that I've entered this function here so what do I do.

04:42.410 --> 04:44.980
Well I want to get the two numbers right.

04:45.110 --> 04:48.170
So for example at the store there's no errors.

04:48.350 --> 04:51.050
So first of all I want to get the posted data.

04:51.050 --> 04:59.960
So first steps step 1 get the post and lose data.

05:00.650 --> 05:11.250
So we're going to send a post to data as equals you request get Jaisalmer just like we saw pre-decided

05:11.270 --> 05:12.830
you OK.

05:13.020 --> 05:24.120
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

05:24.240 --> 05:28.080
Y is equal to the posted data.

05:28.680 --> 05:34.260
Why now we're going to say that X is and x to make sure that it's in.

05:34.280 --> 05:46.590
And even though we're supposed to have received but let's just verify that their ends and then return

05:46.950 --> 05:57.330
is equal to x plus y and then turn Jaison or you return even Mao is equal to

06:00.570 --> 06:04.560
some and then the sum is returned.

06:04.800 --> 06:05.800
Right.

06:05.920 --> 06:11.360
Then finally return on a FI return now.

06:11.490 --> 06:16.890
So before that we also need to put the status code and we said that if we succeed in this then we would

06:16.890 --> 06:25.890
say status code and then put 200 right because we succeeded in this in this if we if we get here and

06:25.890 --> 06:29.810
we have added the two numbers then the status quo should be 200.

06:30.100 --> 06:37.650
There should be a comma here now instead of some what we can do is we could always put a message here

06:38.220 --> 06:41.460
to just simulate what HDB really is like.

06:41.550 --> 06:44.680
So we always put a message here and we will get the response.

06:44.680 --> 06:48.430
And we also have the status which will store the status code.

06:48.480 --> 06:52.610
So this message if if if it's a 200 then it would have the response.

06:52.770 --> 06:58.910
If it's not a 200 if it's a variable like 301 or a 302 then we would stand here.

06:59.040 --> 06:59.420
Right.

06:59.460 --> 07:01.420
And a message for example.

07:01.560 --> 07:10.700
You forgot to say you know the parameters are not there or your numbers are are not integers or so on.

07:10.740 --> 07:13.750
OK so if we're here then we're good.

07:13.760 --> 07:18.380
So let's first test this less Now before we can test this.

07:18.400 --> 07:21.270
So for defined our resource here.

07:21.300 --> 07:27.810
This is our resource but if you look here we see that map to slash and right.

07:27.840 --> 07:31.380
So we need to do this mapping over here.

07:31.950 --> 07:34.540
Now how do we do this mapping.

07:35.580 --> 07:43.870
Well you go back here and then we're going to say after we have defined all our resources we're going

07:43.880 --> 07:46.240
to say API.

07:46.350 --> 07:58.020
So if you don't remember AB AIs this API here then we're going to write API is equal to add and then

07:58.230 --> 08:06.930
underscore and then resource and then we're going to say which resource we want to do so we want to

08:06.930 --> 08:15.120
add this resource here and we're going to do add and then in here we're going to say where we want that

08:15.120 --> 08:17.690
resource to be routed or ahead.

08:17.850 --> 08:24.760
So in this example we said we aren't at slash and so let's go back here and then we do slash.

08:24.760 --> 08:28.120
And so now we've added this resource.

08:28.470 --> 08:29.830
It's listening and slashed.

08:29.850 --> 08:38.940
And if you add this resource gets a post request this function is going to start running and then we're

08:38.940 --> 08:51.120
going to get the posted data and then step 2 gone and and the posted data and then step three is we're

08:51.150 --> 08:58.810
going to return our response to the user which is the message and the status quo.

08:59.750 --> 09:00.240
OK.

09:00.240 --> 09:03.560
So let's make sure everything is running here no problems.

09:03.750 --> 09:07.580
Let's see if there's any problems clearing this.

09:07.680 --> 09:14.610
And then flask question and we have a couple of errors here.

09:14.880 --> 09:23.550
So the first thing is oh ok so it shouldn't be API does it add resource.

09:23.550 --> 09:25.840
It's not equal to and resource.

09:26.130 --> 09:28.800
So it's API and resource.

09:28.920 --> 09:31.900
So we're adding our resource to that API.

09:32.520 --> 09:37.350
And they're not saying you have any other problems and we don't.

09:37.620 --> 09:38.200
OK.

09:38.280 --> 09:44.260
So let's open up postman here let's try posting some information.

09:44.520 --> 09:47.670
So we're going to select coast from here.

09:47.910 --> 09:53.800
And then here we're going to use localhost five thousand.

09:53.820 --> 09:57.950
And then at the far southern port then we're going to listen to that right.

09:57.960 --> 10:06.160
This is how we this is the end point slash add right at it and then we're going to read in the message

10:06.160 --> 10:12.810
of the body or the body we're going to be sending a message wrong which wishes and Jaison format.

10:13.330 --> 10:23.280
And then inside this case I'm really going to have X and he's going to be five and then yours and then

10:23.410 --> 10:27.930
Y and it's going to be reasonable six.

10:28.090 --> 10:33.970
And then let's see what happens when we sent this and their response and we get their response back.

10:33.970 --> 10:40.160
So the message is that then and the status code is 200 So we got to success to use God.

10:40.390 --> 10:42.610
And this is the dish.

10:42.610 --> 10:45.560
Now what happens if x is not given it right.

10:45.580 --> 10:48.290
If we don't have X and we send it.

10:49.330 --> 10:52.450
Well this will cause internal server error right.

10:52.450 --> 10:53.860
We don't want that.

10:53.860 --> 11:01.480
We want you want to give the user an understanding of what to do when when there's an error right.

11:01.480 --> 11:04.420
We want to give him an area that makes sense.

11:04.420 --> 11:05.480
In other words.

11:06.070 --> 11:09.460
OK so let's see how we can we can change that.

11:09.460 --> 11:10.050
Right.

11:10.060 --> 11:22.670
So first of all once we get our posted data let's make a function code check posted data anti-acid post

11:23.120 --> 11:25.000
and data right.

11:25.270 --> 11:31.460
So what's so this here is step 1 be right.

11:31.630 --> 11:37.930
Verify validity of.

11:38.230 --> 11:40.560
And also we should pass in what functions.

11:40.570 --> 11:42.220
So this is and.

11:42.280 --> 11:43.770
Right.

11:43.870 --> 11:47.450
So verify the validity of posted data.

11:47.860 --> 11:48.610
Right.

11:48.780 --> 11:49.440
OK.

11:49.660 --> 11:51.660
So let's write this function over here.

11:51.670 --> 11:54.550
So we're gonna define a function called a shack.

11:54.680 --> 11:58.460
Most of the data is going to have the posted data.

11:58.840 --> 12:05.130
And we're also going to have the function name so function name.

12:05.650 --> 12:13.290
OK so we're going do is the function name is equal to add.

12:13.460 --> 12:14.730
Right.

12:14.890 --> 12:15.960
And what do we do.

12:16.340 --> 12:19.990
Well in that case we want to we have two types of errors right.

12:19.990 --> 12:21.830
We have 200 which is OK.

12:21.850 --> 12:26.330
So we don't return any errors but there is also 301.

12:26.620 --> 12:32.450
So 301 would be a problem right because we have a missing parameter.

12:32.650 --> 12:44.880
So let's go back here and then we say if x not in posted data in the posted data dictionary.

12:45.160 --> 12:50.660
Or why not an posted data.

12:51.230 --> 13:01.150
And what do we do then we would apply return 3 3 0 1 or else we simply response.

13:01.220 --> 13:03.190
We're we're with 200.

13:03.370 --> 13:03.970
Right.

13:04.300 --> 13:13.570
So in the function here we're gonna get status code is equal to check the posted data and then re going

13:13.570 --> 13:25.180
to see OK if status code is not 200 That means there are some here that are going to prepare a response

13:25.180 --> 13:29.560
to the user's server or returned Jason and then

13:32.760 --> 13:42.530
so we're going to prepare a response and say an error has happened or let's say let's make it more clear.

13:42.670 --> 13:47.610
So a message and then and there happened.

13:48.870 --> 13:59.990
And then the status quo would so let's make sure the capitalization record was put X back and see how

13:59.990 --> 14:02.110
stupid it looks like.

14:02.240 --> 14:12.970
So copy this exactly and then status code and then we put the status quo that the error status code

14:12.970 --> 14:20.650
that we got and then return on if I returned Jason.

14:21.710 --> 14:26.630
So now we're going to check the posted data if there is any here.

14:26.690 --> 14:27.310
Right.

14:27.320 --> 14:33.440
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

14:33.540 --> 14:37.090
error elsewhere when I return 200 pages.

14:37.200 --> 14:38.370
OK.

14:38.840 --> 14:41.720
Now I want to see this tax code that I returned.

14:41.900 --> 14:44.850
If it's not 200 then that means there's a failure.

14:45.020 --> 14:49.370
I'm going to say the error happened and then I'm going to return to the user status code that led to

14:49.370 --> 14:50.140
this error.

14:50.360 --> 14:55.220
In this case it means that there's a missing parameter.

14:55.220 --> 14:58.510
Now if this is 200 then that if I'm here right.

14:58.520 --> 15:07.580
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

15:07.580 --> 15:08.720
returned here.

15:08.820 --> 15:18.740
So here and then this data is 200 so we can now access easily x and y add them together and then return

15:18.770 --> 15:23.380
their response to the user as the message.

15:24.350 --> 15:30.570
OK so let's try and test this and see if there's any ears here syntax or or any other errors.

15:30.590 --> 15:37.000
So this flask can run so return some here.

15:38.180 --> 15:40.470
So this should be equal to.

15:40.500 --> 15:41.240
Right.

15:41.720 --> 15:47.720
And let's see run again and there's no problems here.

15:47.990 --> 15:51.800
Let's go first verify that the normal case is working.

15:52.310 --> 16:00.200
Then we get 11 and 200 maybe try five and fives and then you get 10 and 200 OK.

16:00.410 --> 16:05.480
So what if we get delete x now and then we see what happens what's the status.

16:06.260 --> 16:12.530
So hopefully he shouldn't tell us your message and there happened and status code 3 I want.

16:12.530 --> 16:18.430
So the user would look at the state and say oh three you won so that means that I didn't give that the

16:18.800 --> 16:22.060
the API the parameter x.

16:22.520 --> 16:23.580
Right.

16:23.690 --> 16:26.660
So you what if I send the empty.

16:27.230 --> 16:29.040
Then again get 3 0 1.

16:29.090 --> 16:34.070
So it handles all the edge cases perfectly whether or not there or why is not.

16:34.100 --> 16:36.360
There were 2 3 0 1.

16:36.380 --> 16:39.070
Now you can make this very very specific right.

16:39.080 --> 16:40.600
It doesn't have to stop it.

16:40.760 --> 16:46.730
You know you could make one very close for X one Eyrecourt for why you know you can you can really divide

16:46.730 --> 16:50.330
this chart or here in two very very detailed.

16:50.330 --> 16:53.760
And that's very good for your eyes.

16:53.850 --> 16:59.450
In fact most developers love a guy like that because they can debug their code right away.

16:59.450 --> 17:04.130
But for now we're just going to get like if any of the parameters is not there we're just going to return

17:04.130 --> 17:05.410
3 or what.

17:06.110 --> 17:13.760
OK so now that we're finished this and one in the next video room and I start completing the subtract

17:13.760 --> 17:17.810
one of the multiply 1 until we have the fully working API.

17:18.320 --> 17:18.670
OK.

17:18.680 --> 17:20.900
So until the next video recording.
