Fast API Tutorial, Part 30: Bigger Applications - Multiple Files

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends and welcome back to our fast api tutorial in this video which is number 30 in the series we're going to touch on bigger applications dealing with multiple files so what we have here we have our app up this is still part 28 from two videos ago we still have the sql app in here we still have the front end in here that's all like it's all going to stay in there but now what we're going to do is we're going to have kind of a sub app if you will um we're going to set up a certain structure that's what is showing up right here we're going to set up a certain structure and we're going to you know kind of handle a whole bunch of stuff we i mean we kind of saw it in the in the sql app before but we're going to kind of just take it a little bit further so we've got our sub app with our dunder in it file right here and we're going to have i'm going to start to um uh how do i put it i'm just going to start to add the files i'm not really going to discuss the structure ahead of time because it would be just a whole bunch of me talking and you looking at the screen so what we're going to do first thing we're going to set up we're going to create a new python package call it routers and then within routers i'm going to create a new python file called users.pi now from fast api we're going to import api router and then we're going to set up we're going to instantiate our api router now we're going to just create some routes we can do this just like you would with an app that we've done before so we're going to do tags equals users for our router.getusers and then async def read users return and then we'll just return an array uh username will be rick and user name will be morty okay now we're going to do router dot get user slash me not may me tags equals users and then async def read user me return username current user and then we'll just [Music] we'll do one where you're passing any username specifically router.get users username tags equals users the tags functionality here is really just for organization in the api and we'll see it in just a little bit async def read user username is a string return [Music] username username okay good that's reformatted now what we're going to do within our sub app we're going to create a dependencies module dependencies let's get rid of this from fast api import header http exception async def get token header x token is a string which is a header type if x token is not equal to fake super secret token then we're going to raise http exception status code equals 400 detail equals we'll just say x token header invalid valid not invalid invalid um what is this yelling at me for default unfulfilled there we go okay next async def we're going to get the query token token token is a string if token is not equal to jessica raise http exception status code equals 400 detail equals no jessica token provided okay so now we've got our oh not what i wanted there we go now we've got our two dependencies set up and now we are going to um we're going to create another another router okay so we've got our user's router this time we're going to set up an items router from fast api import api router depends an http exception from dependencies import get token header so now you can see why we did the dependencies after users and didn't then set up items first so now we're going to say router equals api router so prefix is going to equal slash items and we need to so just as as a reminder i ran into this little issue um a while back um and you know what i'm going to expand this out for now because we don't have the app actually open right now um i ran into this issue a while back where i didn't include that leading slash we have to have this on the prefix but since we're going to be so what's going to happen with this prefix is we're not going to have to add in slash items slash to each route that we create on this router by including this prefix that will automatically do that for us so we don't want to put the slash at the very end of it because we're going to put the slash at the beginning of each route so you'll see what i mean tags equals items dependencies equals depends on get token header and responses equals 404 is going to be description not found and you'll see what we're going to do with that in just a little bit now let's create a fake itemsdb uh we'll do no i don't i'm not i'm not going to do this one um let's see let's think of another database um whatever i'll just go with this one name is plumbus and then gun nope colon name portal gun see i'm not as big of a gamer so it doesn't whatever it's fine it doesn't really matter now at router.get so this is going to be our base route but this is going to be slash items and then that's it because we're setting that prefix async def read items return fake items db and the the beauty part about all of this is because we've declared our tags up here we've declared our dependencies up here we don't have to worry about adding them to our um our decorator at all it just comes along for the ride okay at router dot get slash item id async def read item item id as a string if item id not in fake items db raise http exception status code equals 404 detail equals item not found okay sorry about that i think i just hit the microphone um and then otherwise we're going to return name fake itemsdb item id and we're going to get the name attribute and then we'll return item id item id now we're going to do a put request as well router.put item id and we will do tags equal custom and responses equals 403 description operation forbidden okay async def update item item id is a string if item id is not equal to plumbus raise http exception status code equals 403 detail equals you can only update the item plumbus otherwise return item id is our item id with a name the great plumbus i'm going off documentation on this i have no idea what this refers to so um if anyone wants to throw that in the comments you know great um okay so let's see let's see let's see what do i want to talk about right now so um in this one we have um so there are a couple things that are going on here so we have our our general stuff that's showing up here and then what we can do we can add extra tags to certain routes we can add extra stuff to uh to these routes the tags responses things like that that will just that will show some extra information for 403 which we're returning or we're raising right here i don't want to i don't want to say too much about this just yet because i'm probably going to end up saying something that's incorrect uh because kind of my i don't i don't work with a script in case in case you can't tell so i'm just going to keep on going and then we'll kind of see it as as things unfold a bit so let's create our um our main python file main.pi so from here from fast api import depends fast api now we're going to import from dependencies um get query token head get token header and get query token okay and we will we're going to import a couple other things in just a minute um something with the routers and i'm going to put this to do import routers just because i want to show you something else so now we have app equals fast api dependency so we're going to have a global dependency is going to be get query token so we had a dependency for our items which was get token header and now we're going to have a global dependency that's going to show up everywhere which is get query token okay so we're importing get query token and now what we're going to do is we're just going to add the routers but first let's just create a app.get slash async def root return message hello bigger applications we'll save this and let's just go ahead and start where did my mouse go okay there we go uh let's just go ahead and run it and let's just see what it looks like let me open up the window localhost eight thousand not three thousand okay we have no routes and that's because i'm not using the correct one so let's stop this we are going to do uvicorn nope not you lock well i don't know how to pronounce that you've acorn um what is that sub app main app reload oh it's not a slash it's a dot there we go refresh we have a route it's the root route try it out a no jessica token provided so let's look at our get query token token needs to be literally the name jessica hello bigger applications okay there we go we're good now what we want to do is we want to import the routers so from routers dot users import router and then what we can do app.include router router now we save and we refresh our page and look we have our root route again but we also have all of these routes as well try it out this will not work because this needs to be jessica and you can see we get rick and morty if we go down here a little bit again jessica i think i'm probably going to you know what i'm going to do get query token dependencies um equals um jessica let's see if this works refresh there we go just because i don't have to type it out in each one but you can see the point of having that in there okay so now and we can just type in our username blah blah and we get it great fantastic we're good now though we get into a little bit of an issue because if we look at items we call this router and if we look at users we call this router so if we try to import from dot routers dot items import router now if we refresh the page let's do this save refresh the page we're still only going to get our items because we're we're overwriting this so there are a couple of ways that we can handle this i'm going to get rid of this so we want this one to be our user's router and we want this to this one to be our items router now how do we do this well there are a couple of ways we can just first do from routers import users and from routers import items or we can just do items right up there and then this would be users.router and this would be items.router and now we save and we reload and there we go now we have our users we have our items and we have this custom this this route the items slash item id shows up in both of them what did we require for the x token i'm just going to make this fake super secret token because i don't want to have again i don't want to have to type it each time there we go and we notice if we go back to our items router for this one with the item id with the put request not to get the put request you notice we got the 403 with the detail of that but we can see the the description for it is operation forbidden which is what we've i don't know if that's how you spell it in german or not i don't speak german i speak french but i do know a few words so there we go we can see um we can see that that's updated there now you notice we also have the not found that we set up before operation forbidden not found we also should have not found on this one and there's a bunch more information that you can include in the um the error like you can see up here for the responses the 404 we kind of only have this description here there's a bunch more information that you can that you can include that we've seen before but we're not going to get into too much detail about it okay now let's go back to the main app now this is one way we can do it um we can do from routers.users import you import router as user router from.routers.items import router as item router and if we hit save and we refresh the page you can see everything still works we're good um there's another way we can do this that i will just touch on very quickly if you have a lot of stuff going on um you can from dot items import router as item items router and from dot users import router as users router and then we can get rid of this from dot routers import users router and items router so if we do this nope stop that please there now we hit refresh there we go so we can import this and if you're using type checking you would need to do i think it's all equals items router and user's router otherwise my pi will yell at you okay so so now we're importing that we've got these two different routers it doesn't really matter which way you do it um you know just kind of keep it in mind so so you don't lose track of things um and then i think i think that's kind of it like anything that you can do on the app that we've seen before um let me pull up uh fast api let's open this a little bit um or zoom in a little bit i should say i don't think it's request example data so yeah i mean we can we can do something like what we've done in the past here where we describe um you know we describe what these things would look like um you can do that for you know for your examples but with like in the uh in the dependencies no not in the dependencies in where is it where is it where is it items so you can add more information to uh to this sort of this sort of stuff right there uh but that's kind of it um there's you know you can you can include api routers in other api routers it doesn't have to just be into the main app um you know you can nest this sort of functionality you can get very creative but it can also get very um it can get very confusing very quickly so i would caution you against that start you know start kind of high level and then work your way into more detail as you need it okay so i think that's it for this video we've got it's looking like about four or five videos left i think in the next one we're going to touch on background tasks okay i will see you there
Info
Channel: JVP Design
Views: 7,714
Rating: undefined out of 5
Keywords: fastapi
Id: OzFyOC90v6U
Channel Id: undefined
Length: 22min 39sec (1359 seconds)
Published: Wed Aug 17 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.