WEBVTT

00:05.240 --> 00:06.760
Hi everyone welcome back.

00:07.070 --> 00:12.200
So in this video we're going to be starting to implement the API of the actual API that will do the

00:12.200 --> 00:14.040
text comparison.

00:14.090 --> 00:17.140
And we're going to be dividing this into a couple of videos.

00:17.140 --> 00:22.520
So first of all we're going to implement the registration features so how do we make it to generate

00:22.520 --> 00:29.540
the user where we can you know check whether he exists in our database or not whether or how we can

00:29.540 --> 00:35.670
communicate for like his username and password and store it into our database and so on.

00:35.810 --> 00:40.650
So this should be fairly straightforward as we have done that previously.

00:41.030 --> 00:46.660
We're just going to be on a couple of extra things so previously we would just add a user into the database.

00:46.670 --> 00:52.490
But now we're going to be improving this feature and we're going to check first if the user exists.

00:52.490 --> 00:58.700
If it doesn't exist because if you remember from the chart protocol if the user does exist already I

00:58.700 --> 01:01.270
should return 3 of one invalid username.

01:01.340 --> 01:01.960
Right.

01:02.270 --> 01:02.860
OK.

01:02.990 --> 01:08.120
So you're going to add a couple of extra things but hopefully should be similar logic to what we did

01:08.120 --> 01:09.160
in the previous video.

01:09.170 --> 01:16.700
So we'll I'll try to go up in as fast as I can but also not sacrifice just explaining what I'm doing.

01:17.030 --> 01:25.700
So the first thing we always do is to import the if the necessary modules or libraries or functions

01:25.700 --> 01:32.190
from from class so I'm going to write from flask import and then I'm going to import a couple of things.

01:32.210 --> 01:35.200
First one on port the constructor flask.

01:35.240 --> 01:44.000
I'm also going to copy the G J signify function which basically changes a dictionary into a Jason replied

01:44.000 --> 01:51.020
to get the user right and we've used this to return the return adjacent to the user that sent us the

01:51.040 --> 01:52.360
requests right.

01:52.400 --> 01:58.670
And then we're also going to have requests or we have request over here that's what we need from flask.

01:58.670 --> 02:02.140
Now we also have some stuff that we need from flask restful.

02:02.170 --> 02:07.280
So from flask press photo we're going to import it and then we're going to couple it in court a couple

02:07.280 --> 02:12.380
of thing we're going to bore the API constructor and we're also going to import the resource right so

02:12.380 --> 02:16.300
resource make sure resource.

02:16.480 --> 02:16.900
OK.

02:16.970 --> 02:19.080
So make sure the spelling is correct.

02:19.190 --> 02:26.870
We also need to import the Mongo climbed from by Mungo's we're going to do it from Pine Mongo in port

02:28.470 --> 02:30.780
Mongo client.

02:30.820 --> 02:38.090
And if you remember it's and capital and C capital we're also going to import the crypt to be crypt

02:39.020 --> 02:44.150
because we want to store the password as a hashed password and not just a normal password.

02:44.450 --> 02:47.570
And then the last thing is we're going to be importing Spacey.

02:47.630 --> 02:55.400
OK so we're going to be using spacey but let's not important now but when we actually need it I'm going

02:55.400 --> 02:57.420
to come up here and add space.

02:57.440 --> 02:57.820
Right.

02:57.950 --> 03:01.670
So for now let's just leave it as it is and you don't eat pork.

03:02.170 --> 03:02.610
OK.

03:02.630 --> 03:08.660
So now we're going to do a couple of things that we always do so we're going to construct a flask application

03:08.660 --> 03:15.320
so act as equal to flask and then name you just give it the name of a square inch for name and square

03:15.320 --> 03:19.720
inch square marble and then we say API is equal to API.

03:20.090 --> 03:21.290
Right so we initialize that.

03:21.290 --> 03:23.740
This app is going to be an API.

03:24.260 --> 03:26.940
We also have to connect to Langone Baeza right.

03:26.950 --> 03:28.490
Client is equals.

03:28.500 --> 03:31.760
What's that client is equal to.

03:31.850 --> 03:33.230
Mongo client.

03:33.470 --> 03:36.870
And then inside there we're on right Mongo db.

03:37.400 --> 03:47.200
And then two dots like call us and we call and then two Dasch slashes and then NDB got 2 7 0 1 7.

03:47.240 --> 03:54.140
So that's basically connecting to that D-B that was made by this knocker file here so deep and just

03:54.200 --> 04:00.960
connecting to the default port that Mongo the Bjork's and which is 2 7 0 1 0.

04:01.160 --> 04:08.240
So now we're connected to Mongar client and then the next thing we want to do is to do D-B is equal

04:08.240 --> 04:09.550
to client.

04:09.710 --> 04:12.050
And then we want to do a new database.

04:12.050 --> 04:14.240
I want to call it simple liars.

04:14.350 --> 04:19.710
Me lariats the database OK so correct spelling.

04:19.870 --> 04:21.970
So client that similarity database.

04:22.210 --> 04:24.820
And then we're also going to be making a new table.

04:24.820 --> 04:26.970
Or if you remember and you collection.

04:27.100 --> 04:31.450
So a user is equal to D.B and then users.

04:31.600 --> 04:36.700
So this collection here is going to have a bunch of user name and hash passwords.

04:36.730 --> 04:37.720
OK.

04:38.200 --> 04:42.340
All right so now let's start with implementing the class.

04:42.580 --> 04:44.890
So we're going to implement the class register.

04:45.130 --> 04:45.810
OK.

04:45.960 --> 04:49.940
And then we're going to inherit from resource from the API.

04:50.070 --> 04:57.250
We Cirus some reason I keep writing it incorrectly and that are going to be since we discussed here

04:57.250 --> 04:59.980
that Shlash register is going to be post.

05:00.040 --> 05:07.790
So I'd like to implement this as opposed to someone to define post as post and then itself.

05:08.640 --> 05:10.720
OK so a couple of things we have to do.

05:10.720 --> 05:20.110
First of all we have to get the posted these are after say posted data is equal to request dot get underscore

05:20.380 --> 05:23.410
JCA right we get the education that was sent to us.

05:23.690 --> 05:24.150
OK.

05:24.310 --> 05:29.080
So this should be PPOs Ted data.

05:29.250 --> 05:29.910
OK.

05:30.220 --> 05:31.660
To get the request.

05:31.690 --> 05:32.800
Jason File.

05:33.190 --> 05:40.180
And then the second thing we're going to be doing is to get the username and the password that were

05:40.180 --> 05:40.630
posted.

05:40.640 --> 05:52.200
Right so I want to say a user name is equal to posted the username and password is equal to posted data.

05:52.500 --> 05:53.880
Basford right.

05:53.890 --> 05:57.270
So I just access it as a normal dictionary.

05:57.340 --> 05:58.660
Piven.

05:58.810 --> 06:01.230
Now the next thing is I'm going to do check.

06:01.270 --> 06:07.080
I want to check if this username exists already because if it does then I need to return to the users

06:07.240 --> 06:09.990
that hey this user already exists right.

06:10.000 --> 06:13.030
I need to send him 3 or 1 invalid username.

06:13.240 --> 06:14.010
OK.

06:14.230 --> 06:21.670
So I'm going to come here and then I'm not I right if user exist.

06:21.710 --> 06:23.860
So I haven't written this function yet.

06:23.870 --> 06:26.580
But if user exists user name.

06:26.690 --> 06:33.710
So if this user exists over here it's no need for this extra process the user exists then that means

06:33.710 --> 06:38.340
that I have to tell the user hey sorry three or 1 invalid user.

06:38.410 --> 06:45.080
So if the user exits with this username then I'm going to say return Jaisalmer is equal to and then

06:45.080 --> 06:57.080
I'm going to say status is 3 0 1 and message is invalid username invalid Youzhny.

06:57.710 --> 07:01.950
And then I'm going to return Jaison ify the return Jason.

07:02.000 --> 07:02.600
OK.

07:02.720 --> 07:06.130
So I want to return this dictionary Jason.

07:06.280 --> 07:06.690
OK.

07:06.770 --> 07:08.440
So that's if the user exists.

07:08.440 --> 07:11.880
So I tell him hey sorry invalid user choose another way.

07:12.380 --> 07:12.810
OK.

07:12.830 --> 07:18.590
That is not true on the other hand then I need to hash this password and then I need to store this username

07:18.620 --> 07:19.850
and hashed password.

07:20.060 --> 07:27.940
So that's a hashed password is equal to big crypt dot hash password.

07:28.190 --> 07:30.080
And then I'm going to say password.

07:30.180 --> 07:33.480
Well you remember we have to encode it in UGF 8.

07:33.610 --> 07:40.450
I'm going to encode UTF 8 OK and then I'm also going to generate a salt.

07:40.490 --> 07:47.020
So I want to say B crypt dot get generate salt from it.

07:47.070 --> 07:47.740
OK.

07:48.860 --> 07:55.690
And now finally I want to input this user with the username and password hash password into the database

07:55.700 --> 07:56.430
right.

07:56.450 --> 08:01.160
So I'm going to go users don't answer it.

08:01.770 --> 08:02.200
OK.

08:02.200 --> 08:14.910
So when I insert something into the user's database when I input username as username and password as

08:15.820 --> 08:21.260
hashed password you're going to store the hashed password and not the actual password.

08:21.370 --> 08:23.330
And then finally I'm going to start tokens.

08:23.350 --> 08:27.200
So the free tokens that any user gets is 60 seconds.

08:27.220 --> 08:31.510
You can only use six tokens for each user for free before he has to be.

08:31.600 --> 08:32.470
OK.

08:32.860 --> 08:38.590
So now that we've inserted this user then the only thing that we need to return to the user is a success

08:38.590 --> 08:41.580
message right 200 status code 200.

08:41.590 --> 08:42.520
OK.

08:42.820 --> 08:48.320
So let's go back and we're going to write returned J-Zone or return dictionary.

08:49.000 --> 08:52.630
And then we're going to say status is 200.

08:52.630 --> 08:54.190
So success.

08:54.320 --> 08:58.610
And then the message is you've successfully signed up for the API.

08:58.630 --> 09:05.790
Right so you've successfully signed up to the API.

09:06.400 --> 09:12.370
And then finally we return Jaison if I off the return Jason.

09:12.520 --> 09:13.070
OK.

09:13.220 --> 09:18.650
So this should be it for the return Jason and for the post function in general.

09:19.070 --> 09:19.630
OK.

09:19.750 --> 09:24.640
So but a couple of things that we haven't done yet is you're right the user exists.

09:24.680 --> 09:27.730
Right we haven't written this this function O'Rear.

09:27.920 --> 09:33.820
So we go back and then we're going to write define user exist.

09:33.970 --> 09:40.910
User name right we need to make a function that checks whether some user exists or not this user in

09:42.150 --> 09:45.410
so I'm going to say if users that's fine.

09:45.450 --> 09:48.210
So I'm going to try and find a user with this username.

09:48.210 --> 09:53.760
So when are you going to write it all in long lines so if you find usernames.

09:53.910 --> 09:58.840
I want a user with this username with with with this username.

09:59.210 --> 09:59.750
OK.

09:59.880 --> 10:07.320
So if I find it and then there's a useful function in Mongomery be called count so if count how many

10:07.320 --> 10:09.660
records match this find.

10:09.660 --> 10:09.930
Right.

10:09.930 --> 10:13.180
How many records have a user name is.

10:13.320 --> 10:21.350
So I am going to say if the user stuff find this user name is equal to zero then that means that there

10:21.360 --> 10:23.690
is no user with this user right.

10:23.790 --> 10:27.420
He doesn't exist because none of the records have this username.

10:27.570 --> 10:29.370
So I'm going to return false.

10:29.370 --> 10:29.970
Why.

10:29.970 --> 10:32.970
Because the user does not exist his account.

10:33.000 --> 10:38.390
None of the record none of the documents in this collection have a user name or fusion.

10:38.400 --> 10:42.860
So if it's equal to zero of the number of zero I'm going to return false.

10:43.010 --> 10:44.380
Else I'm going to return true.

10:44.430 --> 10:45.270
OK.

10:45.720 --> 10:46.880
So I'm going do.

10:47.130 --> 10:50.100
So that's basically it for the user exists.

10:50.310 --> 10:53.570
So let's go quickly over the logic of the registration.

10:53.610 --> 10:55.270
You get the posted data.

10:55.380 --> 10:56.670
We say the your name.

10:56.670 --> 11:00.630
Is this what the password is the posted data in password.

11:00.900 --> 11:07.300
And then we check if the user exists with this username that I telling users hey sorry three or one

11:07.380 --> 11:08.680
invalid username.

11:08.700 --> 11:09.980
Choose another username.

11:10.880 --> 11:12.490
And then else.

11:12.530 --> 11:15.630
So if this is not true then we don't return here.

11:15.700 --> 11:22.270
Then we first hash the password here and then I store the hashed password with the user name and they

11:22.370 --> 11:28.410
initialized the user with 60 seconds so that he can use the API at least six times to get a feel of

11:28.430 --> 11:30.140
how the API is working.

11:30.470 --> 11:36.320
And then I tell them 200 you successfully signed up to the API and I return Jason.

11:36.770 --> 11:38.950
And that should be OK.

11:38.990 --> 11:40.160
So we're going to stop here.

11:40.190 --> 11:44.990
And in the next video we're going to see how we can implement the detection right where we're going

11:44.990 --> 11:51.130
to be using this model over here the space the natural language processing library.

11:51.410 --> 11:53.550
So until the next video app decoding.
