Build a Pizza delivery API with FastAPI #8 | GET ONE ORDER (HTTP GET REQUEST)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone and welcome back in this video we're going to go ahead and implement our app that's going to help us to retrieve one order by its id so if i look into our mini documentation right here that order that order route is the slash orders slash or that's slash or the id route so i'm going to do is to implement this route so i'm going to head over to our other routes and the first thing i'm going to do is to create this route so i'll say at auth router so this is going to be auth router then we shall say this is going to have a method of get and we're going to specify that our route is going to be slash order slash the id of that specific order so what i'm going to do is pass in the id so also link for my view function so our fee function is going to be a sync dev and then this is going to be uh get uh order so i mean say get order by id so i'll call this get order by id then we shall pass in the id which is going to be an integer so our id is going to be an integer and the next thing we're going to do is protect this route so we shall provide the authorization dependency so i'll say it's going to be authorized actually this is going to be authorized and then it's going to be of type of jwt and then we shall say that this is equal to depends so right after doing this i'm going to come and i'm going to come right here and what i'll do is to to try to uh predict this route so that we can only access it using adjectivity so to do that i'm going to come and say tray and then what i'll do is to say this is going to be authorized dot jwt it's going to say required and right after doing this we're going to also accept any exception as e so we can say accept and you're going to accept any exception so we are going to come right here and say accept exception as e and what we shall do is to raise so we're going to raise an http exception so this is going to be http exception so this is going to have a status code so we're going to specify a status code asset as code is going to be status dot http in this case it's going to be 401 uh and authorized so we're going to say this is going to be unauthorized and now the next thing we're going to also provide here is going to be our details so in this case our detail is going to be equal to invalid token so in this case a user has provided an invalid token or they have been providing the token and they're trying to access that route so i'll say in this case we're going to have our so this is going to be your valid token as the error that you're going to produce so the next thing we're going to do is to get the current user who is making this request so right now since we're using jwt we can access the current user who has sent this request by uh getting that identity from the jwt so to do that i'm going to create a variable called user and then this user is going to have the identity of that jwt so we're going to say jwt dot get and in this case we're going to get the jwt uh subject so right after getting the jwt subject to a great query for that user with a specific username that is that of that jwt subject so we're going to call this the current user and this current user is going to be uh an object of type user so what you're going to do is to query so we shall say session dot query and then we're going to pass in our user obj user model then we read filter so say filter and then we shall filter for the username that is equal to that ability subject so we shall say it's going to be useless the username equal to so in this case we're going to call this user the jwt subject then you want to get the first object that has that specific uh that specific username so what you want to check for is that if this current user is a super user because within our documentation here we have that route being executed by only a super user so i'm going to do is to come right here and i'll say if current user so in this case we're going to say if current user dodge is so we have that dot is staff attribute that specifies whether this is a super user unknown so what we shall do is to actually uh make a request for that or that we're going to pray for this order from that database so we are going to say all that is going to be equal to session dot query dot so in this case we're going to specify our order model and then we're going to find the specific order with our id so we shall say filter and then right in here we're going to say uh order dot id equals to the id that we've specified then we want to get the first object so shall say it's going to be dot first and right after doing this we are going to simply return this with adjacent bone encoder so what i'll do is say return in this case we having say a json abroad encoder uh we shall pass in the order so i'll say it's going to be that order so if in case we do not uh return uh that order or the user is not a staff member what we want to do is to raise an exception that's going to show the user that this is not a super user and therefore they cannot carry out that request so i'll do is to come right here we need to copy this and simply paste it here so we're going to change a few things so we're going to leave the status code that's 401 and authorized and in the detail in this case we are going to say that user not allowed to carry out the request since they do not have administrative privileges so i'm going to save this and when i save this we're going to try this out so i have my installment right here i'm going to go ahead and create this request so i'm going to create a new request i'm going to say get order by id so this is going to be a get request i'm just simply going to come here and say create and right here what i'll do is to provide the url so this is going to be the host 8 000 slash or dash slash or dash and you're going to pass in uh the id of that order so in this case you're going to query for the one with an id of 1 which is an integer so the first thing i'll do is to provide the header so i'll provide the authorization header so in this case i mean say it's going to be authorization and then i'm going to come right here and then i provide bearer i'm going to go to the login route and generate a new token so i'm going to go to login i'm going to make a request so this is taking long let's see what's up hoping that the server is running so if i head over to so right now we have uh an error right here so let me try to actually fix it so i need to first close this so the area we are having is this so if i fix it a server is going to reload actually let me restart the server let's stop let's have a control c and then try to run it again so right now we see that auth router actually this is supposed to be order router sorry for that so i'm going to come right here and this is going to be our order router so if i save our server is going to reload and now we don't have any errors so if i head over to our insomnia and try to make this request one more time so right now we see that we have got an access token so i'm going to copy this access token and then go to the get order by id route so i'll paste it in with a bearer and then what we'll do is send the request so in this case we see that we've got a order with that specific id so in this video we've been able to see how to retrieve that order with a specific id thank you guys for watching and see you in the next video bye
Info
Channel: Ssali Jonathan
Views: 55
Rating: 5 out of 5
Keywords: fastapi, fastapi tutorial, fastapi python tutorial, fastapi python, using request body in fastapi, request, request body, fastapi restapi crud, fastapi example, fastapi authentication, http get request in python, fastapi crud, requests, restapi, python fastapi swagger, fastapi create project, fastapi framework, fastapi на русском, request api, sqlalchemy, postgresql, fastapi-project
Id: Z9oZ6xzKIVc
Channel Id: undefined
Length: 8min 43sec (523 seconds)
Published: Fri Sep 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.