Fast API crash course | easy way

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there everyone hates here back again with another video and in this video we're going to take an in-depth look about the fast api a modern way of how to design the api and i'm pretty sure the fast api is going to impress you a lot first we are going to move on to computer we'll design a simple crud application for let's just say courses of the website and i'll walk you through how to work with the fast api so that at least our one application is up and running and also by the way for all those my goldfish friends with a little bit shorter attention span i have linked in the description section the blog where the entire code files and the pip file everything is mentioned and is available so you can check that out too so once we are done with the fast api basic uh code walkthrough then i'm gonna come back here and i'll talk you and discuss about that what are the potential problems that fast api is solving and why should you should consider this as a great option for picking up for the next project so that you can design apis faster and better so without further ado first let's get into the code walkthrough and i'll come back here after that and we'll discuss a little bit more so welcome to the computer in this first we are going to take a brief look about the fast api and we'll design a simple crud application so that you know how to work through it and how easy actually it is to work with the fast api so without a further due let's get started up here first i would like to fire up my vs code just like always i've customized it a little bit more so don't worry i'll i'll definitely release a video about this one by this week i'll drag and drop a folder fast api which is obviously an empty folder right now now let's go ahead and do our magic of installation a few things the first thing you can choose any of your favorite virtual environment i prefer to use pip env so in case it is not installed just go ahead and say pip install uh pip env since i am on a mac i have to run through every command with the pip 3 install env mine is already installed if your is not just go ahead and install that once that is installed all you got to do is say pip env and simply grab a shell of that this will not only activate your shell environment a virtual environment but also will create a file which is so much easier to give to all the audience who are watching this so this is all what we got so this is how it works it gives you an environment but also activates this one for you and i can give you all the packages that i'm installing and working and everything like that so the only thing that changes up is obviously pip env this is how we install instead of pip so we can go ahead and say pip install and obviously i'm going to be working with fast api so i have to grab that for the production obviously i would mention a version number after that but that's what we are having now apart from this we need a server which which can make sure that our file is up and running of course for the web and there are two options there is a hyper con and there is a uv corn so feel free to just choose any one of them i'm going to be choosing up a uv corn but feel free to choose hyper corn they are almost exactly the same so looks like it's going to take couple of second come on do this faster or i have to speed forward this let me fast forward okay it is already up here now the next one that we are going to have is going to be uv corn so let me just bring it up and uv corn now there is a hyper corn and there is a lot of debate which one you should get which one you shouldn't get but i'm not gonna go in that debate let's go ahead and work on with that so this one should be fairly fast okay there we go we have got everything up and running and you can see everything is updated up here so i'll give you these files as well now what we're going to do is we're going to create a new folder and i'm going to call this one as api because that's how usually the workflow is all of the apis reside inside the api folders and then we are going to simply call main come on not like that we are going to create a new file inside this one and i'm going to call this one as main dot pi okay now first the syntax of the uv cone first we need to travel this into this api so there we go and please don't do this okay and we are now inside this now all we have to do is simply say uv corn now uv cone requires two flags or two parameters that we have to pass on first is the file name so my file name is main and then what is the main app name so i'll create this one in a second i'm going to call this one as app and then you can pass on a flag of reload so that automatically reloads and work on that but before we do any of that we need to write something up here so let's go ahead and see that how it actually looks like uh when we work in this file and the fast api and you'll be surprised to know that how easy it is first and foremost obviously we have installed this so i'm going to simply say fast api let's go ahead and say i need to if i can write that import first api that's the first thing then obviously you will need some kind of models to work on with that we are going to just push everything into an array but probably you'll be working with the postgres or anything like that if you want me to make a video on that please drop a comment section that we need more in-depth video guide on postgres or authentication on that i would love to make it but in the comments first okay so then we are going to have up identic so there we go pi identic let's go ahead and import base model what this will do this will allow us to craft a model in so much easier way that we can craft all this so just like we create models in the django it's almost similar to that but much more easier than that in fact that one is also easier but this is much more and then we are going to have uh from typing let's go ahead and import optional now we won't be using it too much but i wanted to just show you that you can have the optional fields in your base class which is going to be basing all of your objects that you will be inserting in the array but this is we won't be using it too much and i'll show you that later on you can remove it entirely if you're not too comfortable in that now this is how you create an app so we're gonna simply say that please fast api create an app for me and whatever the name of the app you are giving up here that's exactly what i was talking about what you have to mention up here so you got it now okay so now that app is created this obviously is not going to be doing anything so we need to first define our route and the way how we define route in the fast api it's ridiculously simple i don't think anything can be much more simpler than this so we're going to simply mention app.get so obviously app is your main app that you have created and then it comes with all the verbs that you can use or you probably use in the api get put post patch everything is up here let's go ahead and run it on the home route so let's go ahead and define uh this is read route now again i'm using read root so i'm using the same method name which is mentioned in the docs but feel free to call this one anything and i'll talk about that mode in a second but this is how we're going to get this is not going to take any parameter or anything and we are going to just go ahead and simply return an object which is going to be greetings and we are going to be simply saying welcome to learn code code online dot in okay so this is it that's all what we have written nothing too fancy nothing we are not even using python tag anything but this is it this is all you need to make sure your apis is up and running along with the documentation yes it surprised me hell a lot so i'm going to just run this one and hopefully this is not going to do any problem and i can just go ahead and launch this one so let me bring up my browser first uh there we go and we can just copy this one copy that my browser is up here and we can go ahead and paste this one and we are being through and this is json by the way and notice i didn't mention anything i'm definitely throwing an object but i didn't convert it explicitly anything into the json and that's one of the advantage of the fast api that whatever he throws up list strings boolean it automatically converts them into json and that is amazing now one more thing that it also gives you the access of two types of darks so you can go ahead and simply say docs and voila your docs are ready and you can just use get and you can try them out if you have some parameters it will show you the equivalent parameter fields with validation that it is integer list or whatever and you can execute that it will show you that this is my request url this is my response along with the response code and if there is any failure or something it works on that as well and there's another kind of docs as well which are pretty interesting redox this is the url where you get it it's not actually redox i need to check in the docs actually let me quickly check that and let me show you that it's not read docs it's actually really dark just i just check in the docs actually so there we go it is just re-dock and this is also same thing we can have a get response we can fire up the response and get the response code and everything i'm going to go for the swagger version which is this one so i'm going to be going for this one so coming back on to the part where we are writing the code we'll keep it all running so that we don't have any issue now let me walk you through a couple of more things that will get you more idea about this one okay so first we need a database we are going to use a fake db and we are going to be calling that as an empty array now at this point uh if you want me to have a more in-depth guide about postgres or using authentication or something leave a comment in the description section i'll replace this fake db2 postgres and show you how to process that that's not a big deal but right now we're going to use this fake db now we need a class to be designed so let's say you are creating a learn code online which is a course based website and you want to push up a lot of courses in that website so we're going to simply say i'm creating a class of course and which is going to be run by this base model which again we are importing from pyrentic and then you have to mention that what are going to be the fields in that so obviously every course is going to have a unique id and i just have to mention that it's going to be integer and it's going to take care of all the validations for me that make sure it is 100 person in teacher i don't have to write even a single line of code and then every course is going to have a name the name is going to be just a string format then we are going to include the pricing of the course obviously which is going to be float integers float string and you can have a boolean fields as well so we're going to say is early bird bird if i can write that okay so it is early bird and this is the point where we can use our optionals as well to give more uh typing response and everything uh i'll probably talk about that in later on videos right now otherwise it will be too much deviation here and i'm going to just say that by the way you can just mention the bool i will uh probably get a separate video of explaining this optional because i have never made a video on typing an optional so i'm not covering them up here probably a separate video okay coming back on to the story we have now our app we have a fake database and we have a class which is modeling my entire data that how it is going to be there so the next thing is obviously to put up all the operations that in theory you will be putting up in any application so let's go ahead and do that again decorate it everything needs to be decorated before adding the verb so let's just say i am again putting up a url which is going to be slash courses again this is a get request so how this is going to work we are going to just throw up all the courses so we're going to simply say get courses and once that method is being called we are going to go ahead and simply say i am returning you the entire fake db notice here i'm not rewriting this or converting it into any json or anything i'm just throwing up the entire array there we go okay let's go ahead and grab a couple of more routes here so app the next one is going to be let's just say we are going to get again you get but this time i need to show you that how we can take the query parameters so we're going to simply say courses and then if you want to take any parameter from the url all you got to do is simply use the curly braces and let's just say we're going to take a course id here okay so if somebody passes me a course id then obviously i'll give him a course so i'm going to simply say let's just say a get course or get a course probably a better naming can be used but i'm going to simply say get a course there we go okay couple of things before we get a course obviously the first thing that we need is going to be a course id which is being passed on to me here so i can take a access of this directly but i have to cast this one because there are chances that it might be treated as a string by default they are actually treated as a string so i need to cast them as a string rest all validation will be handled up by them by the fast api now once this is done all you have to do is make sure that in the course so the structure that we are following is that integers are going to be starting from one two three but the arrays actually start from zero so let's just reduce that now again if you are using foreign for an id a randomly generated number then in the array we have to find out and you can use variety of algorithms to find an id which is matching to your course id but i'm not going to make that that complex i'm going to just keep that as an integer 1 2 3 4 but feel free to add a little bit more logic here to find the id which is matching the past id again we're going to do that in probably some course so i'm going to call this one as a simply course and then we are going to take this course id and we are going to subtract just one from it because array starts from that and we are going to simply go ahead and return from the fake db the value which is calculated by the course again trying to keep that as simple as possible okay so this is the home route a simple basic route this is getting all the courses this is getting individual course and now it's time to add a course as well as delete a course so that is also really simple we are going to go ahead and simply say app.get not get actually post so we're going to use the verb post now and again we're going to follow the standard practices i'll throw up another video this week on the api best practices but we're going to simply say if somebody hits the route with the post method here then we are going to go ahead and define another method so we're going to call this one as add course and when you're adding the course so obviously you're going to send me a course which should be of the type of base model class the course that we have defined up here okay now how we're going to add this one it's going to be like really really simple we're going to go ahead and say fake db dot append and again we are not re-evaluating it because a lot of work is being done by the fast api but it is always recommended to do a one more thorough check from the back end and we're going to just convert that into a simple dictionary and that's it we are taking the course literally converting them into dictionary and appending that into the fake db and that's pretty much it now in theory uh you don't return entire object as a response usually but let's just say in this case we are deviating a little bit and we are gonna be simply saying that in the fake db uh just get me this course which you are sending so fake db minus one so whatever is the last object remember we are appending it so whatever the last object is there we are appending it so that's pretty much it okay not bad now one more thing before we go ahead and call this tutorial enough so app dot delete and then we're gonna do a little bit of testing as well so if somebody hits a route of slash courses and slash obviously we cannot delete everything without an id so now we have grabbed the id so how we're going to delete that my bad there we go let's go ahead and define the method which is going to be delete course and it is it is going to be processed with the course id which is going to be a type of integer so safety check a little bit there there we go now how we're going to delete that we're going to simply take the fake db and we will pop the course id but we are going to be assigning the ids starting from 1 2 3 but our array is going to be taking them from zero but if you're marking a course as with the id 0 then you can just keep it as it is but i'm not going to do that i'm going to start from a 1 so i'm going to just subtract 1. again how you're crafting and then here's the interesting part this time i'm not going to return anything but rather i would like to return a json object with a key value so task which is going to say deletion successful and that's that's all now see this example here we were able to do all of almost all of the crud operations and see the number of lines of code that we have written here uh there might be some issues here there might be some typos which we are going to fix right now but this looks okay so let's just see first that how much we are it looks okay here let's see that how this is going so we need to just hit a reload and now notice here that how well the documentation is first it gives me a documentation about schema so i can click on course and it can say it says that hey this is id strict means you need to pass on you cannot avoid it and then we have this is a title which is id and all of that so name is going to be a string then we have a price which is going to be a number and then this is an early bird which is a boolean but this is not compulsory this is an optional field i'm pretty sure you have guessed it by now and then once you're done with reading the schema you can go ahead and simply move on to the first get can try it out and you can execute that request and it's going to give you the response body which is greetings it also gives you the response header in case you are looking for that and that is a fantastic thing to be able to work on i surely can do that using the postmen but if it is just right here why wouldn't i use that now getting a course list so let's try and execute that obviously this is going to return me an empty array because nothing is there we will come back on to this first let's add some data to our fictitious database so i'm going to go ahead and click on try out and i'm going to start with the one and it's picking up all the default values so let's just say i'm going to go ahead and push up a course on django freelance ready and i'm going to keep a pricing of 199 rupees and i'm going to leave the default yeah true that's fine execute that it gave me that this is the response body and remember whenever we are doing this add a course we are returning the last object from the fake db so this is actually following up pretty nicely here we are having all the validations and everything nicely let's go ahead and add one more up here so i'm going to say id with 2 and this one is going to be let's just say the new react js scores this is i guess 299 not pretty sure okay so execute that and this time it is giving me exactly that back so pretty much nicely it is working nicely and now we can close this one let's just see if we are able to grab the individual courses i'm going to go ahead and say try this out and i'm going to say give me the course with the id to execute that and it gives me the response but but the beautiful part is what if i ask for a course whose id is not there i haven't handled that response so execute that and notice it handles internal server errors and everything pretty nicely surely this could be handled nicely a little bit more if i throw up my errors which are not present nicely that would be better response but at least it is handling my application is not getting crashed that is a nice one and let's go ahead and see if we are able to grab all the courses now let's go ahead and execute that and yep list is coming up and finally let's go ahead and delete a course so again i have to mention that i'm gonna go ahead and delete a course id with uh probably one so the first course is going to be deleted and notice here task deletion successful so i'm getting the response back and for all those folks who are still not able to appreciate that how this is all working on let me show you something really really interesting here copy this entire url and throw it up in your browser directly and this is a json response i have never written a single line in this entire code that this is a json response it is by default converting that into json which is one of the best thing that is happening in the api and how simplistic the code actually has become so i am impressed i am impressed okay so this is all for it now let's move back on to the camera so first meet sebastian the founder of the fast api and we really thank him a lot for creating this amazing product and i'm pretty sure this product is going to pick up quite a lot in the community it is well designed well optimized and the performance is really amazing now coming back that there are plethora of options of choosing how to design the api and why this can be in the same competition race and what are the features that are additional and impressive of that now surely it is mentioned on their blog and the homepage of the fast api that it is ridiculously fast and very performance obviously i have noticed that as well but now coming up that why i would consider this in the next project or the big scale project for shipping the application communication now one of the biggest thing that you are going to notice when you work in a big scale project or a big company is let's just say there is a team of back-end developers who are designing the apis and there are front-end developers who are handling these apis and view react or anything else at that exact moment there are so many tools for communicating tools like jira trello and i have used a ton of them slacks and everything that is there but still there is a lot of communication gap between the api designers and the front-end team who is handling it and i have seen even people shouting from this room to that room and slack channels and everything and this is really painful an additional work comes into the picture when these back-end api designers have to write the entire documentation and then pass on and update the documentation to the front-end team they fire up their postmen and do all of the testing first and then write the code it is a process that we have been working on so far but what extra edition benefit that right out of the box i can see with the fast api is it's cooked up built-in documentation this solves a big problem for us that it is all cooked up as soon as i update my code the documentation gets updated quickly and my friend and team can see that right in the browser and i can just i have to give basically no instruction to them so that is the one thing which out of the box i can see is really really impressive and the selection of all the behind the scene frameworks and everything is obviously very optimistic here now surely there is a lot more to explore and probably once we create more complex application we can see more in depth of the fast api i haven't as of now worked too much in depth of it and a big scale project but i think this is a great candidate and i'll surely consider that in shipping up some of the products in the company in the future and recommending it as well let me know what are your thoughts about the fast api um i'm look for my personal thought i'm thinking that this is going to be a great framework to contribute as well and by the way for all those people who have been asking me hey we want to contribute in open source suggest us something or how to contribute here we go fast api is freshly new up here you want to contribute something go ahead take a pull request design a new feature and submit that for the review just make sure you don't act like a and just update the readme file or just the documentation if there is a full walkthrough and you are updating a lot that's okay but make sure you are contributing something valuable and something edition in this api this is your time this is your chance that you can contribute in something really great so again with the final note a big thanks to sebastian for from the fast api for bringing this into the community i am thoroughly impressed and i'm surely going to be making up more tutorial on this one let me know your thoughts in the comment section as well and i'm going to catch up in another such crash course see you soon [Music] [Music] country
Info
Channel: Hitesh Choudhary
Views: 38,924
Rating: 4.9422474 out of 5
Keywords: Programming, LearnCodeOnline, python, javascript
Id: TQfIUS52QHA
Channel Id: undefined
Length: 26min 11sec (1571 seconds)
Published: Thu Jan 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.