Hi everyone welcome back.
So in this video we're going to be starting to implement the API of the actual API that will do the
text comparison.
And we're going to be dividing this into a couple of videos.
So first of all we're going to implement the registration features so how do we make it to generate
the user where we can you know check whether he exists in our database or not whether or how we can
communicate for like his username and password and store it into our database and so on.
So this should be fairly straightforward as we have done that previously.
We're just going to be on a couple of extra things so previously we would just add a user into the database.
But now we're going to be improving this feature and we're going to check first if the user exists.
If it doesn't exist because if you remember from the chart protocol if the user does exist already I
should return 3 of one invalid username.
Right.
OK.
So you're going to add a couple of extra things but hopefully should be similar logic to what we did
in the previous video.
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.
So the first thing we always do is to import the if the necessary modules or libraries or functions
from from class so I'm going to write from flask import and then I'm going to import a couple of things.
First one on port the constructor flask.
I'm also going to copy the G J signify function which basically changes a dictionary into a Jason replied
to get the user right and we've used this to return the return adjacent to the user that sent us the
requests right.
And then we're also going to have requests or we have request over here that's what we need from flask.
Now we also have some stuff that we need from flask restful.
So from flask press photo we're going to import it and then we're going to couple it in court a couple
of thing we're going to bore the API constructor and we're also going to import the resource right so
resource make sure resource.
OK.
So make sure the spelling is correct.
We also need to import the Mongo climbed from by Mungo's we're going to do it from Pine Mongo in port
Mongo client.
And if you remember it's and capital and C capital we're also going to import the crypt to be crypt
because we want to store the password as a hashed password and not just a normal password.
And then the last thing is we're going to be importing Spacey.
OK so we're going to be using spacey but let's not important now but when we actually need it I'm going
to come up here and add space.
Right.
So for now let's just leave it as it is and you don't eat pork.
OK.
So now we're going to do a couple of things that we always do so we're going to construct a flask application
so act as equal to flask and then name you just give it the name of a square inch for name and square
inch square marble and then we say API is equal to API.
Right so we initialize that.
This app is going to be an API.
We also have to connect to Langone Baeza right.
Client is equals.
What's that client is equal to.
Mongo client.
And then inside there we're on right Mongo db.
And then two dots like call us and we call and then two Dasch slashes and then NDB got 2 7 0 1 7.
So that's basically connecting to that D-B that was made by this knocker file here so deep and just
connecting to the default port that Mongo the Bjork's and which is 2 7 0 1 0.
So now we're connected to Mongar client and then the next thing we want to do is to do D-B is equal
to client.
And then we want to do a new database.
I want to call it simple liars.
Me lariats the database OK so correct spelling.
So client that similarity database.
And then we're also going to be making a new table.
Or if you remember and you collection.
So a user is equal to D.B and then users.
So this collection here is going to have a bunch of user name and hash passwords.
OK.
All right so now let's start with implementing the class.
So we're going to implement the class register.
OK.
And then we're going to inherit from resource from the API.
We Cirus some reason I keep writing it incorrectly and that are going to be since we discussed here
that Shlash register is going to be post.
So I'd like to implement this as opposed to someone to define post as post and then itself.
OK so a couple of things we have to do.
First of all we have to get the posted these are after say posted data is equal to request dot get underscore
JCA right we get the education that was sent to us.
OK.
So this should be PPOs Ted data.
OK.
To get the request.
Jason File.
And then the second thing we're going to be doing is to get the username and the password that were
posted.
Right so I want to say a user name is equal to posted the username and password is equal to posted data.
Basford right.
So I just access it as a normal dictionary.
Piven.
Now the next thing is I'm going to do check.
I want to check if this username exists already because if it does then I need to return to the users
that hey this user already exists right.
I need to send him 3 or 1 invalid username.
OK.
So I'm going to come here and then I'm not I right if user exist.
So I haven't written this function yet.
But if user exists user name.
So if this user exists over here it's no need for this extra process the user exists then that means
that I have to tell the user hey sorry three or 1 invalid user.
So if the user exits with this username then I'm going to say return Jaisalmer is equal to and then
I'm going to say status is 3 0 1 and message is invalid username invalid Youzhny.
And then I'm going to return Jaison ify the return Jason.
OK.
So I want to return this dictionary Jason.
OK.
So that's if the user exists.
So I tell him hey sorry invalid user choose another way.
OK.
That is not true on the other hand then I need to hash this password and then I need to store this username
and hashed password.
So that's a hashed password is equal to big crypt dot hash password.
And then I'm going to say password.
Well you remember we have to encode it in UGF 8.
I'm going to encode UTF 8 OK and then I'm also going to generate a salt.
So I want to say B crypt dot get generate salt from it.
OK.
And now finally I want to input this user with the username and password hash password into the database
right.
So I'm going to go users don't answer it.
OK.
So when I insert something into the user's database when I input username as username and password as
hashed password you're going to store the hashed password and not the actual password.
And then finally I'm going to start tokens.
So the free tokens that any user gets is 60 seconds.
You can only use six tokens for each user for free before he has to be.
OK.
So now that we've inserted this user then the only thing that we need to return to the user is a success
message right 200 status code 200.
OK.
So let's go back and we're going to write returned J-Zone or return dictionary.
And then we're going to say status is 200.
So success.
And then the message is you've successfully signed up for the API.
Right so you've successfully signed up to the API.
And then finally we return Jaison if I off the return Jason.
OK.
So this should be it for the return Jason and for the post function in general.
OK.
So but a couple of things that we haven't done yet is you're right the user exists.
Right we haven't written this this function O'Rear.
So we go back and then we're going to write define user exist.
User name right we need to make a function that checks whether some user exists or not this user in
so I'm going to say if users that's fine.
So I'm going to try and find a user with this username.
So when are you going to write it all in long lines so if you find usernames.
I want a user with this username with with with this username.
OK.
So if I find it and then there's a useful function in Mongomery be called count so if count how many
records match this find.
Right.
How many records have a user name is.
So I am going to say if the user stuff find this user name is equal to zero then that means that there
is no user with this user right.
He doesn't exist because none of the records have this username.
So I'm going to return false.
Why.
Because the user does not exist his account.
None of the record none of the documents in this collection have a user name or fusion.
So if it's equal to zero of the number of zero I'm going to return false.
Else I'm going to return true.
OK.
So I'm going do.
So that's basically it for the user exists.
So let's go quickly over the logic of the registration.
You get the posted data.
We say the your name.
Is this what the password is the posted data in password.
And then we check if the user exists with this username that I telling users hey sorry three or one
invalid username.
Choose another username.
And then else.
So if this is not true then we don't return here.
Then we first hash the password here and then I store the hashed password with the user name and they
initialized the user with 60 seconds so that he can use the API at least six times to get a feel of
how the API is working.
And then I tell them 200 you successfully signed up to the API and I return Jason.
And that should be OK.
So we're going to stop here.
And in the next video we're going to see how we can implement the detection right where we're going
to be using this model over here the space the natural language processing library.
So until the next video app decoding.