Fast API Tutorial, Part 20: Path Operation Configuration

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends and welcome back to part 20 of our fast api tutorial uh in this tutorial we are going to be touching on uh path operation configuration by the way i don't know if you can tell from my voice it's probably fairly obvious um i have a little bit of a cold so it's going to sound a little different maybe i should start singing songs like about my sticky shoes or someone something like that like phoebe does also the mic is in a different place so it might sound a little different there as well but you know time will tell for that so let's jump right into it as you can see here we've got our app up and running we have no no operations in the spec yet so we're just going to go ahead and get going so let's start off by creating a class of item we're going to set the name is going to be a string the description is going to be a string or none remember we're using python 3.10 so we can do this we don't need to use the optional flag we're going to say price is a float tax is a float or none we're going to set none to default and tags is going to be a set of strings that we will just initialize as an empty set so now let's go ahead and create a route app.post items let's put the slash on the front of it just for fun why not response model is going to be equal to item and status code is going to be what let's see do we have it imported up here it does not look like oh there it is there's status okay so just as a friendly reminder if you don't remember what all of these are we can use our intellisense i'm using pycharm again you can you know i'm sure this is in vs code as well so we're going to use the 201 created status we set the actual path item is going to be an item and we'll just return the item now you'll notice we're not handling any of that any of the the configuration in the path operation we're actually putting it up in the um in the decorator instead so now we go back over here we refresh our page oh okay here we go so we can see here as we've seen before we will get a successful response of a 201 because that's what we've set as the status code and it shows the actual schema here for our response model remember if we got rid of this and we refreshed our page we would see a 200 and the response would be a string okay this is all stuff we've seen before so i'm just going to refresh this now the next thing that we're going to add to this though that will provide a little bit more organization let me actually create the routes first and then i'll explain what we're going to do with it so we'll do app.get items async def read items return and we'll just say name foo and price 42 and i deleted that by mistake okay and then we'll say another app.get will say users and we will say async def read users and we're just going to return username phoebe buffet you know in in honor of my my deeper voice right now so we go ahead and refresh and we can see we've got a whole bunch of just we've got the three routes that's all fine and good what we're going to do though next is we are going to add in tags and what we can do here is we can say this is going to be in items the tags for this one will also be in items and then down here we will say tags equals users and what this does is this allows us to just organize things a little bit better we hit refresh over here and we can see we have our our set of items routes we set it we have our set of users routes okay we can take this a step further and up here let's declare class tags we'll make this an enum and we'll say items equals items users equals users and now here instead of just being the string it will be tags dot items here this will be tags.items and here this will be tags.users now for those of you who are wondering and let's just save it and refresh now you can include multiple tags i mean we're you know we're this is a list right here so we can add in a second tag here we refresh and now our items our read items route will show up in both places in this situation obviously we don't want to do that right because this doesn't pertain to users but there very well may be situations where one route is going to you know you might have a you know get logged in user route that might associate with users might also associate with off if you want to have some auth routes or something like that okay moving right along let's go back onto our post route because we're gonna we're gonna do as much as we can in here just so that we can kind of visualize things because there's a lot that you can do in the route configuration so now here let's set up a summary create an item and we'll say description create an item with all the information name description price tax and i think this um and oh i went to the wrong place and a unique a set of unique tags and let's um can we wrap this let's just wrap it to make it look a little bit better now we're going to hit save and we're going to refresh our page again now we should only have i took the users tag out of this route so we should only be here we still have nothing here but here we have our nice little description here and you notice this says create an item if we just hide this for a second and we refresh you notice this says create item because that's what we named our path operation function it will convert it into a nice you know a nice readable format but if you want you can actually set this yourself you can say create an item type item because we're actually we're actually setting this class right here we're calling it an item class so you see we say create an item type item so that's one way that you can do it but this this description is great for just the you know quick and dirty text but what if we want something a little bit more informational if you will so i'm going to go ahead and hide this description what we're going to do is we're going to create some doc strings and for those of you who don't know what doc strings are you should still by now you should i'm pretty sure we've covered at some point in time or another in this so what we're going to do here is we are going to [Music] put our main text here create an item with all the information now you saw this param and this return were automatically generated when i you know hit enter after doing the three quotes because when you're creating doc strings you're doing it usually for um you know documentation online for users and and you might actually care about um you know you might care about the arguments and the return type in this situation we don't right now i mean i'm sure we will at some point in time but let's do name because what we want to display to the user who's using our api not the actual function itself but the api we want to display the information so each item must have a name we're going to say description a long description description price required tax and you might be wondering why i'm adding in the double stars and the the hyphen at the beginning you will see if the item doesn't have tax you can omit this and finally tags tax and tags i feel like sebastian should have probably chosen a different thing to put in there but whatever a set of unique tag strings for this item we hit save we go over here and refresh our page excuse me and you notice the reason why i used this and the double stars is because the the swagger documentation that shows up here we'll actually interpret this as markdown so if we were to do just a single star instead of a double star and we refresh it's italicized if we were to do an underscore instead of a star and again we refresh we get italicized a double underscore should be um boldfaced and that's what we get okay so you can do all that sort of fun stuff in here um yeah i don't think i want to do too much more in terms of in terms of markdown um the next thing that we can that we can add in here and i'm going to leave the doc string here like i'm going to leave all this in here as much as i can this description i'll leave commented but we can you know you can probably get rid of it if you want to we are going to call the response description the created item again we hit save we refresh our page and we go into here oh the created item you'll see it's showing up right down here the created item there we go again if we get rid of this just to see what uh what it was looking like before it just said successful response which there's nothing wrong with um if you're you know this is the sort of thing where if you're building this api for either public consumption or you're working on the back end and you've got um you know you've got front-end folks who are working on a separate client and you know you don't communicate as well as you should then you would want something like this where you can actually display this sort of information to people um the very last thing that that we're going to touch on um i think yeah i don't think i want to do too much more because again we're getting a little long and you know my voice is a little scratchy we're going to add in one more piece of functionality at get elements tags equals tags dot items async def read elements return item id foo it will update our formatting and you can see here we've got our read elements right here we try it out it's all well and good it does exactly what we anticipate it's going to do but what happens if we want to again we're building this for public consumption we want to let either the public know or our front-end users know that this is no longer going to be a valid um a valid endpoint well we can say deprecated equals true we hit save so this still shows up we can still try it out we can still use it so it's it it's still there it will still work but the concept of something that's deprecated is it's it's no longer an active use it is going to be removed in a future release you know something like that so you want it to still be there because you don't want to implement a breaking change let's say you're doing a you know a a minor release instead of a major release you know major release you'd have breaking changes people need to update their end points minor is less so so you know you don't want to break people's systems so that's what this would be for deprecated is true there are a whole bunch of other options here you can you can look into them let's go into app.get we can see a whole bunch of stuff tags dependencies summary description all this sort of stuff is in here um that we're not going to um you know we're not going to touch on all of them right now because i think i think we're good for this video we're about 15 and a half minutes so i am going to say see you later uh next video we will um we will deal with an encoder um json compatible encoder which will then get into um you know uh updating with um uh request body and stuff like that sorry i'm sick my mind is just not right there um okay i'm gonna call it time of death at 16 minutes and five seconds have a good one
Info
Channel: JVP Design
Views: 3,539
Rating: undefined out of 5
Keywords: fastapi
Id: 7d0dy6QWgNA
Channel Id: undefined
Length: 16min 10sec (970 seconds)
Published: Tue Jun 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.