Fast API Tutorial, Part 2: Path Parameters

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends and welcome to part two of our fast api tutorial uh in this tutorial we're going to be talking about path parameters first let's go in and we're going to make sure that our app is running i have this i had it running before now i hit refresh and it's not going to work because the app is not running we have our virtual environment activated so we're going to go in here and type in uvicorn main app dash dash reload and we can see now it's running on 8000 and it reloaded because i guess it hadn't finished uh reloading from before so a path parameter if you're at all curious localhost 8000 and if we were to go somewhere like i don't know hello we would get detail not found or one two three detail not found this value that's in the url that i don't know if i can actually zoom that in or not that is our path parameter so if we want to add in functionality where we can pass a path parameter in this is the sort of thing where if you were to do app.get items async def get or let's say list items return we will just say message list items route okay now if we go into here and we type in items we're gonna see we're in the list items route which is fine this is your standard get request um if you wanted to search for a specific item your path parameter in this sort of situation would return the id say five or something like that so what we want to be able to do is we want to be able to do app.get and we're gonna pass in items and then here we're going to pass in our item id and here we will redefine this get item and we're going to pass in item id as our parameter and then we can actually just return item id and then we'll just return the item id that shows up there so now we don't have to do anything else we can refresh the page because we already had localhost slash items slash five we return and we get a value of five but this is somewhat strange this is showing as a string this is where pidentic can come into play and pidentic can shine a little bit what we can do is we can tell fast api that we want our item id to be an integer now if we hit save and we reload we will reload this and you notice we get five as our actual result here the thing that you need to you need to think about though and that you need to realize is that if we were to pass in something like hello in this in this situation it wouldn't work you can see we get this error value is not a valid integer but if we were to remove this save it and reload it would work again so there are some ways that you can you can get around that if you can um if you can have typing in here pretty much anywhere you can that's generally the way you want it you want to do it you you i mean python 3.6 i think and above especially 3.10 they're getting really good with their uh with their you know type safety it's not a static type statically type language it's not a compiled language but you know this is it's very very good we should definitely make use of it where we can we can go ahead and refresh this we can return one two three here we're still going to get this string let's put this back to an integer though there we refresh the page and we're good okay so now what we're going to do is we're going to take a look at the actual documentation here to see if we refresh this page now we have a couple of these these new items here these new routes i should say we can execute this and we get the message that we received before we can come down here this is where um pedentics sort of built-in help with this can can help if we go into here and we try and type in hello fast api's swagger documentation is not going to let us pass in this hello string because it's telling us it needs to be an integer if we pass in 6789 then it will work and we will return that item id of 6789 if there is a let's change this to a string just so i can show you we're going to refresh this page we're going to have to hit try it out again again we can do 5 6 4 2 i don't know why i'm picking these random numbers this returns the string okay perfect now though let's let's add in a little a little caveat here so i'm going to change this to users i'm going to change this to users and we're going to say user id and this can be a string that's fine it doesn't really matter right now list i should have probably just done this right from the beginning okay now if we want to set it up like this we can do this it's really no different than it was before we still get this and we're good now let's add a route where we can do app.get slash users slash me you know typically when you have an app that you're building um [Music] get current user return this here will ledoo message this is the current user so a lot of times when you're building an app like this you know you're going to want the user to be able to get information about themselves you generally don't want to have unless they're an admin or something like that they shouldn't be able to just fetch a user by user id unless you want to you know you have a public profile sort of situation something like that but you're also going to have you know a setup like this now if we run this we try it out we execute and we're good we get this is the current user because we have explicitly hit this route right here if we go into here and we type in um josh allen you can see we get the user id is josh allen i don't know why i'm not a bills fan but whatever it doesn't matter the difference here though is this will work if we're using swagger but if we go into users slash me this is not actually returning if we remember here oh lord i hate that part um this is not actually returning the this is the current user route what this is doing is this is hitting the first route that qualifies we're telling our our app here that the user id is a string well we're passing in this string it happens to match this route down here but it's not actually getting here what fast api will do is it will just go through the list of routes if it matches this as a get request then it returns this as a post it returns this as a put it returns this here we're going to users slash something that's a string here we're doing user and this should be users let's refresh the page let's just make sure that this still works unless i did something stupid oh here we go there so it doesn't even work in swagger i sit corrected um so what this is going to do is this is just going to to cycle through and it's going to um find the first matching route so if you have something that you want to be specific where the structure is going to look the same and you want to allow a specific endpoint first before a dynamic endpoint you have to put it before the dynamic endpoint now if we refresh the page we can see this is the current user any other string that we put in here it will return bob this will let's see if this works joe smith yeah it converted it okay so that's that's key here that's important the the order that you're putting these routes matters uh last thing we'll touch on is um uh we're going to include uh specific types so from enum we're going to import the enum package and we're going to declare uh food enum so let's see we're going to say fruits equals fruits vegetables equals vegetables and dairy equals dairy and now what we're going to do is we're going to actually add in app.get foods food name and then we're going to declare the function get food and we're going to say food name is going to be part of our food enum now there are a few things that we can do with this the first thing that we can do is we can check to see based on the actual the the model name what uh what what the the value is that we get so if we can do um if um food name equals food enum dot vegetables then we're going to return food name food name message you are healthy there next we're going to go in here and we're going to type in if food name dot value equals fruits return food name food name message you are still healthy but like sweet things i don't know i don't know why i'm picking this i probably should have done this ahead of time a little bit um so we're checking to see if the food name which is a food enum which we've declared up here is the the member of this class called vegetables then we return this we can also check we don't have to check based on the actual model type or the class type i should say we can instead check the value of this instance that's passed in which here is just going to be a string because we're declaring this string enum otherwise return food name food name and message um i don't know i like chocolate milk i honestly have no idea why i picked these things okay so we now have this extra app right here and what we're going to do is we're going to try foods slash meat and we can see we get a nice little descriptive error value is not a valid enumeration member it tells us we can have fruits vegetables or dairy and you can see we got this 422 error unprocessable identity so it didn't conform to what fast api and pedantic were telling us it it's allowed to conform to if we were to pass in instead vegetables you are healthy fruits you are still healthy or dairy i like chocolate milk and we can go into our docks and if we refresh the page we'll scrunch these up a little bit the thing that's nice about our docs here is it gives us a drop down it doesn't even give us the option of adding something else um so we can you know we can go in here and we get the same exact results okay so i think uh for now i think that's pretty much it um there's not really much else that we want to touch on in terms of path parameters um this is a very simple introduction you know we'll get into more of this sort of stuff in in later videos in the next video we are going to talk about um query parameters which is you know if you're searching for you know if you want to do a question mark hello equals world this is the query parameter that we're going to pass in so until the next video have a good day
Info
Channel: JVP Design
Views: 37,397
Rating: undefined out of 5
Keywords:
Id: nCrX79LqDT8
Channel Id: undefined
Length: 14min 29sec (869 seconds)
Published: Tue May 03 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.