Hi everyone welcome back.
So in this video we're going to complete.
Finally the last word of the API and we're going to test it and see how it would work.
So let's get started.
We have lots of things to cover.
Now a couple of things from the previous video.
The first thing is in this password over here then this password the hash password function expects
UTF 8 encoded string.
So we have to do in encode here.
And then we would encourage it to UTSA it.
So there's just an additional thing to do to add to this password right here.
But other than that should be fine to.
Now the next thing is.
OK.
So we also need to look at the API for the resource store.
So we're also going to add API and resource at store slash store.
And then another thing that we missed in the previous videos that we forgot to implement the varify
password and they count tokens of function.
So let's implement them here.
So we'll start with the very password passwords so to find verifying password with a username and password
and then this is how the function is going to look like so we're going to do users so users it is the
collection with the users.
And so we're going to say hashed password and we're going to get the hash password is users not find.
And then I want the user with username to be username and I also want to act so it says this is going
to return an array of users but there's only one user with this user names on when to get the first
user zero element.
And then I'll also get the password right.
There was a field called password stored for that user.
And so now with that to verify the first from the Crypt are going to come here and then we we're going
to say if encrypt the hash password is this print it matches.
Else it doesn't match.
So I'm going to copy this line over here from from the Crypt Web site.
So it's going to say go come here.
Then we say if the crypt the hash password and then the password here again has to be coded so in code
UTF 8 UTF 8.
And then the hash here is the hashed password so hashed password.
So if the password that the user set sends me if I hash it.
And it ends up being the same as hash password.
Then I return true right.
This is return true says not C++ else.
OK.
Else we return false.
OK.
So the hash matches then indeed this user name matches this password.
But if it doesn't then we return false.
So this is for the first one the second one is to count the number of tokens a user have.
So it's defined count tokens.
OK.
And then we take a user name as an input and then we say tokens is equal to users that find.
And so we want the user with a username user name.
And then we want to access the first user of that and we want to access that tokens element.
So when we return tokens this just returns the number of tokens that is left for this user.
OK.
So hopefully this should be fairly straightforward.
OK.
So now we are ready with this store and register to simulate some news.
Let's let's try and do that.
So let's first save and then we'll go to a doctor and then we must clear this and then let's do Sudhakar
compose build first and then we do pseudo-ID combos up.
OK so up.
And then now it should be working fine there shouldn't be any problems.
So make sure there's that the server is running and let's go to postman.
So the first thing is we want to register.
Right so we're going to go to localhost 5000 slash register.
And I want to register my my user name.
Right so I'm going to send a son with the username as let's say my name's of Farouk.
And then for my password I say Allah I'm not very tech or he doesn't want to and went to 3 x y is it.
Gates That's my password and then I sent just last register and it tells me message you successfully
signed up for the API status 200 So we have successfully got into the API.
So now let's say I want to store a sentence so I go to you go back to our API.
We do it as Slash store.
So I go here and I do slash door and then this time I need to send my username password and Ascendence.
So a sentence and then that's going to be this is super secret centers again.
So this is the sentence that I'm going to be storing in my database in the API and then I send that
and then it tells me a message saved successfully status 200 so successfully saved this message two
to the database.
Now the final thing that we need to implement is as you might have guessed is the last one to retrieve
sent over here.
So we need to implement that once.
Let's go back now to the final thing is we have class restore are the same things you can call and get
resourced.
And then as we said it's only going to be a get requests we're going to do define and then get that
self and then the first thing is we get the posted data.
So how do we get the post in the you know what we always do with the same way request duck get Jason
so just copy this from here.
So we copy this and read paste it here as opposed to here is equal to request you use.
Now what does the user send me from here.
He says the user name and the password.
So I do a username is equal to posted data username and password is equal to posted data.
It's posted data and password.
Now I'm going to verify first that this is indeed the user so I'm going to.
So just like we're we're we have here verify the user name and password match so I can copy this here
right.
This also handles the verification of the user name and password.
So that's.
So this should go backwards a little bit.
OK.
So let's shift tap and then use the correct password is verified password username and password so the
user name matches.
Then I.
It doesn't match.
And I returned 3 0 2 and I returned the the Jason.
If however it doesn't match then I also check that he has enough tokens right so I copy this to.
So I have to verify that he has enough tokens.
Right.
So I get all error checking and you can add this into one function if you like if that makes it better
for you.
So we shift out this backwards of number of tokens miscount tokens for this your name and the number
of tokens less than zero then hey you need to buy some extra tokens.
So three on one.
And then if this is not the case.
And finally I can retrieve his sentence and the sentence is equal to users do not find.
So I want to find the user with the username username and then that's it.
So that's the criteria.
And then I want to get the first user and I want to get the sentence so sentence.
So now I got his sentence and then now I can prepare his sentence for him so I can say returned Jaison
is equal to a dictionary.
So first of all the status is 200.
So congratulations you actually did it.
You know you succeeded and the message.
So we're going to go.
Message is the sentence or less say even the sentence is sentence.
So the South is that we got from the database so we save that and it should be return return.
Jason if I sign if I return Jason.
OK.
So we see that finally and we want to send it to you to test it to see if if if it will return my feel
we'll return my sentence that I stored or not.
OK.
So I'll say this and then now we closed AUKer and we opened it again.
So behold and then Doc up
OK.
So no problems Me no syntax errors at least.
And so you can go here into.
And of course we forgot the important thing is to register the resource right so API and resource right
so we forgot about that get and then slashed get.
If you remember in the original one we said that slashed it.
So let's go back to the dot slash get.
And that should be it.
So let's rerun Dharker again rebuild and rerun.
So build and up with there should be no problems no syntax errors.
If you notice here the database prints a lot of debug messages.
By the way the main one here is Web.
Right.
This is the main flask application and then it's running at this and it's running and no problem.
So now I go here and then I want to log in so it's Faruq and 2:59 x y Zed right.
So that's my username and password and I want to get my password back.
So I said
go and of course.
OK.
So we've actually missed that totally because here this shouldn't be actually get.
This should be post right.
So this should be post and not get because the user is sending us the username and password right.
So he's posting to us something so we totally missed that in the chart so this should be and this is
wrong.
This should be postholes because the user is sending us some information.
OK.
So let's correct this.
So this correction shouldn't be very hard.
We just changed this get to a post and we save that.
We rebuild everything.
So build and up and so now there should be no problem.
Hopefully their server is running.
And then we we get the sentence.
OK so the problem was actually in this come over here so there was an additional coming year which was
ruining lots of stuff.
So once we delete this come over here so this code should work as it is there is no any problems with
the code it's just there was this common problem here.
So if I if I try a user name as Faruq and my password is 23 x y is dead.
And then it should work now.
So there's some debugging and it turns out it was just the comma that was the problem.
So there should be no more problems from now on hopefully.
So if we try and first let's close darker and then we build them up again and then.
So it should be working now.
So make sure that the server is running and it's running.
You need to be Gohier and re go and slash get.
And then this time we get that the sentence is a status go it is 200.
And this is actually the sentence that I stored.
Right so if you remember I store the sentence This is supersecret sentence.
This is a supersecret sentence.
So actually the service works though.
I can now get and rebind.
So we have three three features right.
I can register I can store and I can get some information on the database and I paid.
OK.
But if you notice you know we forgot to do a very important thing and it is to take away some money
from the from the user once he's actually done the service right.
So we check.
So we say the number of tokens is this and that the number of tokens is less than or equal to zero than
return air.
But if it's not then we never do anything right.
We just give him the sentence right without making him pay.
So a very important step to add here that we forgot is to make the user pay.
Right and that's why you're probably making the FBI or you might be just making it to help other people
but you should make the user pay.
If you in this case read because we make these You're pay you into to.
Right.
So how can we do it here well if you remember we just did.
Replaced this year.
So we're going to copy this update and that and then we're going to come down here.
And so what we're going to do is first line this correctly.
So shift it out and then we're going to search for the user with this username and we're going to set
the number of tokens as minus is or is not we're not going to replace this sentence right we're going
to leave the sentence as it is.
So we're going to leave this one but then we're going to set the number of tokens as tokens minus one.
So now we took away from him one too.
So if I say this for years now I can save this in and set his token to be this and that if I use the
service many times it should stop me from accessing it.
So let's let's actually test.
So we will we all closed on.
Again we build them up.
There should be no problems because we only added this line here.
The server is actually running.
OK.
So let's try to abuse the system so I'll try to keep get get get get get get.
So this is my sentence.
Let's actually you know what let's actually try to input the wrong password.
So one two three exercise.
And then on the space.
Right.
So if we do this then we get status 3 0 to 1 status 3 or 2.
Well that means invalid username and password.
So it does work.
OK.
So let's go back here and then we put the correct password.
And then we keep abusing the system.
We want to keep getting my supersecret Sundin's right.
So I'll keep doing this and if you remember we gave each person only 10 tokens and that's it.
So now once I've used my 10:02 I got a 301 code.
So what is 301.
Well that means I'm out of tokens and I need to buy some extra tokens so that will be it for this API
and you've seen how we've you know started from nothing and we've built basically been building an API
I won our US resource at a time and how we can really do this.
If you add just a few extra checks of course we mentioned a lot of checks here.
So this area needs a lot of checking actually.
And most of these areas just to make sure that the user are sending you.
Perfect.
You have basically been built a real world API that you can deploy on the server and you can start making
people pay you for for giving you money for it for just using your API calls.
So nobody can access your API unless he pays for something right for it for your API.
And yeah it wasn't a very it was very short it was just hundred fifty five lines of go to her probably
can make it much much shorter but we've copied a bunch of code right.
You can you can you can do a lot of improvements here we've been just lazy in copying lots of gold which
is not usually a good thing to do.
But for the sake of this very small simple API it shouldn't be too bad for it.
And another thing is as we said lots of things you can improve on it.
And as it's standing You might also want to link somehow a payment to gates so that once a user pays
his number of tokens is automatically Cree's dries and you can do like a gate that once it's successful
then it calls some function here and then that that makes its tokens higher.
Right.
So actually we gave him six tokens not 10.
So that's why trend very quickly in Postman.
But that will be it for this API.
So this can be good.
You know there's actually versions of this API out there that people are charging money for Ensco database
service.
So you're basically paying per day and access.
So if you try to store something on their database or access your data and you have to pay some amount
of money.
So that's exactly what we did.
But on a much smaller scale.
So yeah I hope that you found this project code and we'll move onto the next project.
So until the next venue I become an.