How to Build Async API in Python with FastAPI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends i am parusi and welcome to my new course on building apis with fast dpi so before starting our main topic if you want to support me uh you can support me on my patreon page i have added the link in the video description now this course is divided into three parts in the first part we're going to build our epis using regular function and we are using uh orm sql alchemy now in the second part we're going to use a synchronous functionality with encoded databases library and core sql alchemy we're going to create our user and registration system also we want to talk about gdlt token and protecting our routes and in the third part we are going to build our apis using uh tortoise orm library now if you want to uh if you want the table of content you can just check the video description i've added a complete table of content content for this course now let's get started alright guys now let's create our first api in python now when it comes to python there are different options and frameworks that you can use for example for building apis and for building full stack websites now we have django and flask they are the most popular web frameworks that you can use it for building for example rest apis and also uh for building a full stack web development now uh uh i already have uh different videos on django and flask and also integration of these frameworks with the frontend libraries like for example react.js or vue.js or react native mobile frontend now i have already added the links in the video description if you're interested you can check those links now we are going to learn about fast api so you can see that this is the website of the fast api and it is a framework so fast dpi framework is a high performance easy to learn fast to code and ready for production framework for building apis now they have if you see that there are cool features and it's a modern fast high performance web framework for building apis with python 3.6 now you should have this version of python and above based on a standard python type hands now you can see the key features different features that it has and also if you come down now these are the different thoughts and opinions about this web framework and you can see that this is the requirements we need this python and above uh also you can see that fast tps stands on the shoulders of two libraries now the first one is starlet it's also uh another uh web framework for python and they're using this for the web parts and also we have pydentic now it's used for the data parts and now we need to install this so for right now we need to install two libraries so the first one is fast dpi and also you can see that you will also need an asgi server for production such as uv corn or hyper core now first we need to install fast dpi and so i already have created a virtual environment and now i'm going to install this and also for uh the web server we need to install uv core so we can just grab this from here and pip install uv curtain let me just copy this okay now fast api is installed and let's install uv core okay now the installation has been completed and we are going to create our first example so first we are going to create uh a new folder in here i'm going to just uh or so now let me just create a new folder in this section and in here we can just say okay now i have added a new folder at name of fast api and here i'm going to create a new folder at name of api so now let's create a new file and we are going to just create our first example so i'm going to just create a new file and i want to just call it main.pi file okay now in the main.pi file uh so first we need to import fast api and we can if you see they have a simple example in here now first we need to import from fast api we are going to import api like this also import fast api sorry fast api so from fast api import fast api now let's create uh the object of this fast api so we can just say app fast api like this now we're going to create a simple or simple get route so we can just say at app dot get and for right now and okay now this is our uh we can say view function and now let's create our this not this is our route and now let's create our view function now they have a name for this now uh in the fast api let me just first write for example this is my index right now i'm going to just give it index and i want to just return a message and also we have hello world okay so now we have created our simple main.pi file now we need to run this so i've already said that you can use uh we can use uv corner we can say uv icon so if you see there documentation and if i come down you can see that we can use uv icon now let's just uh use it in here and okay now the first thing is that we are in the python api thoughts now let's just just change the directory to the fast api folder now in here we can use uv and let me just show you and we can just say uvi core or we can see uv core and after this you need to give your file name so because my file is in the api so we can just say epi dot main so my file name is main and after that we need to use this app so we can just say up and also because we want to add a reload functionality so we can just say reload now after this hit enter and now we are waiting for this okay now you can see that it is running on this local host and on this port now let's just open this in here and let me just go to the localhost port 8000 and we should see now you can see that we are receiving message hello world now if we test this in the postman and this is a git drought and we can just send this you will see that we are receiving hello world also in our in the postman now will not use this postman a lot because uh uh with a fast api you can use swagger ui and you can just test your end points and also apis so now you can see that we are receiving a message hello world now also if you see their documentation now you can use also a sync because it has a synchronous functionality for example we can just say uh like this and there's no problem you can just use it and you can just use a sync div index and if you uh check uh the result will be the same but you can use uh uh synchronous functionality with a fast api so now so you can uh name it like for example this is route for example if you are from django background so you can say that this is route and this is view function and there is no problem now in the fast api they have different concepts for this now if you if i come down so you can see uh we have imported fast api and after that you need to create the app instance and you know you can see that for this we can call it path operation decreator for example at app.get now for this we can say that path operation the creator and for uh the function we can say that it's part operation function like diff route and after that run the development server like uv icon and after that reload so now like this you can create your first simple uh api and fast api all right guys now this part we're going to learn that how you can uh pass path parameters in your uh router paths i also will learn that how you can use docs from fast api and how you can integrate or interact with the swagger ui now if you see a documentation of fast dpi so you can pass you can declare path parameters or variables with the same syntax used by python format strings now for example you have a route for example uh you have some data from the database and some articles from the database and you want to uh get specific articles now for example i want to get first article second article or third article now for that you can use path parameters so let me just create my new route so i can just say get and this should be articles route and in here i can just say diff get article so you can use a sync but i'm going to just use the regular function now and here i'm going to just return and i can just say so we can say article and article id okay so this is the new route that we have created and uh so for right now if i uh go to the articles uh let me just go to the articles and you can see that i am seeing the article and article id for example if i give articles for example article one i want to get article one and you can see that it's telling me detail not found or we can or if we say article two details not found now for this what we want to do is we can pass path parameters in here so we can just say we want to get the article according to the specific id that will be passed on the path and here also you need to add id now the volume of the path parameter that we have in here so for example id this will be passed to our function uh as the argument id as we have in here now in here we can just say uh and we cannot id in here so we are going to just returning the id now let's just uh now check this so if i go to the uh now you can see that we're receiving article 2. if i say article 3 so this is article 3 and if i say article 4 this is article 4. [Music] also we can declare the type of the parameter now right now if you see in here you can see that article and you can see that we are receiving id as a string so you can see id4 if i say id2 we are receiving the ids string now we need to change this we want integer id now for this we can use type conversion in fast api so we can declare the type of the path parameter and the function so we have path parameter id and we can specify the type in our function for example in here i want it should be integer id so let me before this show you uh one thing now for right now uh if i add uh without for example i'd if i add another integer for example like any slack so how to make api and you can see that i'm not receiving any error because by default fast dpi accept this as integer accept the path parameter as integer now if we want we can use type conversion for converting of this [Music] then here we can say for example id to integer now this time uh if i uh for example say how to make epi and if i refresh this you can see that i am receiving error and it's telling that we are receiving a message value is not a valid integer and we have a type error dot integer now if i add an integer for example article 2 i'm not receiving any error and you can see this time we are not receiving into a string but we are receiving integer now we can test this with the postman so in here if i say uh for example article two now you can see that we are receiving article two in here article and also two now up as i've already said that there's docs and also swagger ui that you can use it and fast api now if you come down in the fast api documentation so now you can see uh and where is it let me just check this so you can see that in here we have a nice fast dpa swagger ui now how you can get access to this so anywhere uh in here we can just say docs like this and now you can see that we are receiving fast api swagger ui and and here you can see that we have two routes the first route is our index route that we have already specified in here and our second route is uh you can see that this is that we can get the article according to the specific id so if i click on this you can just click on the route and after that you need to click on the try out and if you execute so now you can see that we are receiving response body and this is the status code is 200 and there are different status code that you can use and you can see that this is our message and the message is hello world and if you come down you can see that we have a successful response and we have vertical id in here okay so now so we also have a get article so that we can get the article according to the id so let me just okay now i can just click on this get so now you can see in here we are receiving id and it's required and we need to give this so for example if i execute this without giving the id i will receive an error you can see that i'm receiving error that you need to give the id in here so for example i'm going to give id 3 and if i execute now you can see that we are receiving so you can see that this is the requested url and this is the code response code and with the response body is article id 3. also there is an alternative if you see the documentation and you can see that open api are there money also so you can see because of this fast api itself provides an alternative api documentation using re-talk so you will not use this redoc match but you can if you want you can also use it so for example in here we can just say read doc and now i'm waiting for this and it's loading okay now you can see uh that we have our index routine here we have our get article route and you can see that we can access this according to the id and path parameter and for the index we don't have anything in here now we have saw path parameters let's talk about the query parameters now it's telling that when you declare other function parameters that are not part of the path parameters they are automatically interpreted as query parameters now if you see in our example it's telling that if we have some parameters that are not in our path that's called query parameters now in our example you can see that in here we have id and we have the id in our path and after that we are passing the id uh to our uh function to our view function now when we create some parameters that are just added in our function or view function and that are not passed in the path in here that's called query parameter for example and here if i create a slack for example it's just like an example now you can see that it's not passed in our uh in our path in here now this is accepted as qd parameter and fast api now let's create uh an example in here uh so for this uh okay and here i'm going to just create some data uh some dummy data and i can just say data and i want to create an array or a list of some data and i can just say title so we can say first title and let me just copy this so we have some data some three dummy data in here so second title and also third title now in here we can pass our query parameters as we have already said that query parameters are not the part of the path parameter now we can just say uh for example skip to zero or or even we can just create a new because we i think we don't need the id in here and we can just create another i'm going to just remove this and okay no problem we cannot we can remove this id from here and [Music] now we could just remove this id because we don't need this id let me just remove this return for right now okay now here we can just add uh for example also we need to remove the id from here because we don't need i'm going to just add a skip and uh so you can give the type for example for the skip i want it should be integer so i'm going to just give it a 0 so the default values to zero and also i can give limit and then here i can add the type or type conversion in here and i want it should be integer and i'm going to just give it 10. now in here we can simply return data and we can just say skip skip class limit like this okay now in this example if you see uh we have two qd parameters in here and we can see that the qd parameter the qes or we can say the query parameter is the set of key volume pairs uh that go after the question mark in the url are separated by and characters so now we and here we can if you see in here now right now if i so go to the articles so details not found so we need to save this and now we can just go to the articles so now you can see that we have our title this is the first title this is the second title and this was the demi data that we have added in here so in this section so now if i say in here uh so we can just add a question mark and we can just say skip zero and we can just say and person and limit 10 now if i run this so you can see that we are receiving our three uh dummy data in here now if i make the skip to for example 2 1 you can see that i'm seeing the two data in here so the first then so this title and also this title now if i add this uh for example two and you will see that i will receive one uh data and if i say one so two data and if i say four you'll see that i'm receiving an empty list or array now this is called qd parameters so the parameters that are just passed in the function and that are not the part of the path that we have in here and also you can if you see the documentation also you can use optional parameters for example if you want to use optional parameters you can add for example this is the name of the parameter and after that you can use optional and you can add the type for example string and you can make it to none and also you need to import this like this you can use optional parameters also now let's check this in the swagger ui and we can just say dogs and it's loading okay now this is our git shout out now if i click on this git so now you can see that we have default value to zero and this is for the we have the escape and we have the limit now if i try it out you can see we need to add a skip so for right now uh we if i say uh for example uh zero and if i execute this you can see that i am receiving the three data in here title title title now if i say one you can see that i am receiving the two title in here and for example if i say four you will see that i am receiving a name on mpt less than here so you can see that the response body is an empty list so now let's talk about the request body now when you need to send data from a client for example we can say a browser to your api now you send it as a request body and a request body is data sent by the client your api and our response body is the data that your api sends to the client now to declare our request body if you see uh according to the fast api documentation to declare our request body we need pydentic models with all their power and benefits now as we have already in the introductory video of the uh fast api we have told that fast api stands on the shoulder of two libraries so starlet for the web part and by then tick for the data parts now what's spidentic if you see the documentation for the pydentique it is a data validation and seating management using python type annotation and pedentic enforces type hands at runtime and provides user friendly errors when data is valid now you can see that they have an example in here so when we want to create a pydentic model what we need for that so the first thing is that we need to import a base model from the pythentic and after that we can create our class that will extend from the base model and after that we return our pythentic model in our we can say in our view function now uh let's create our example so this is my simple uh main.pi file so i'm going to to uh so let me just remove the data here because i'm using it in here now at the top the first thing is that we need to import a base model from pydentic so we can just say from by identic import base model so from fidentic import base model now we want to create our class our fidentic model class so we are going to just call it article that extends from the base model now we need to add our data for this for example we want id the id should be integer so you can see that i'm specifying the type that the type should be integer and after that i want the title and the title should be string i need description and this should be a string also so we have imported the base model from the pi intake after that we have created our pedentic model class that extends from the base model and after that we have added our fields like id title and also description now now what we want to do is so we need to create another route so right now these two routes are forgetting because we want to post the data for example for that uh we need to use post so right now we have get now we can just use at app.post and we can just say article like this and now we can just say diff you can just say for example add article or insert article whatever you want and for right now let me just return uh article added okay so now we have created our this route in here if you see in the swagger ui okay now you can see that we have post and if i click on the tryout execute you can see that i am receiving the article added as a response body now we have created our pydentic model now how we can add this model to our view function now as we have already saw that how you can add qd parameters you can do the same for the identic model you can add your pidentic model and you as a parameter as a query parameter in your function so in here we can just say article and we can add the article in here like this and in this section we are going to just return the article so we have added the pydentic model as a query parameter in our function now if i see let me just check it once more okay now you can see we have post if i click on the post you can see that we have the id 0 title description now you can see also we have schemas in here if you see the documentation of fast api if i come down and you can see that the json schemas of your models will be part of your open api generated schema and will be shown and then tractive api like this so you can see it will be shown like this now if i go to uh here and if i click on the schemas and you can see that i have the article schema in here so id title and also description so now we can go to the example volume and here we want to just try it out for example i can just add id2 and this is the title and this is the description and i can execute this and now you can see that we have a response body with our data so you can see the id the title and description and we have added like this now let's just try it once more and so right now we haven't used any database but we will use mysql database in the later part of the video and we will save this data for right now we can just say for example uh try it out and id1 and string for example [Music] title and so this should be string and we can execute and now you can see that we have our response body in here all right guys now it's time to create our database now if you see fast api documentation and you can see that fast api doesn't require you to use sql uh or relational database but you can use any relational database that you want now uh in our example we are using sql alchemy now sql alchemy is the python sql toolkit and it's an object relation mapper that gives application developers the full power and flexibility of sql now you can use different databases postgres mysql sqlite oracle and uh in our case we are going to use mysql and we want to use mysql and there are different steps that you need to uh do so first you need to create uh your database tables and after that database connection and after creating of the database connection you need to create your models for table models and after doing that you need to create your schema or we can say that the structure for the data model and after that you can just use the database connection uh in your main dot py file so before that because we're using sql alchemy the first thing is that you need to install sql alchemy so you can just use pep and style sql alchemy so you can just use pep install sql alchemy and you can just paste it and hit enter and also because i have already said that we are using mysql database now if you're using mysql database you need to install mysql client and it's a python interface to mysql and if you're not using mysql database for example you're using sqlite you do need to install this library or for example if you if you're using postgres also you didn't need to install this library but if you're using mysql database then you need to install this uh so we can just say pep install mysql client and we can just add it in here okay now after installation uh we need to create our database so we have already said that we are using mysql database so i'm using wamp server so here i'm going to create my database so let me just call it flask or not flask but it is we can say fast api so we can just say fast dpi and after that click on the create so the database is created but right now we don't have any data in this fast api database or we can say we don't have any table now according to the documentation of the uh uh fast api now the first thing is that we need to create a file at name of database.pi and in there we are going to create our database codes like this so you can see that they're using sqlite but it's telling that if you're using another database you can just use it like this so for example in there because it's postgres and in our case it's mysql database now we have an api folder in here now let me just create an init file in a dot pi file that i want it should be treated like python package so an mpt.init file and after that in the epi folder i'm going to just create another file called database.pi so now in the database dot pi we need some imports so first we need to from sql alchemy uh we are going to import a create engine so we can just say from sql alchemy import create engine and we need to also import uh declarative base so from sqlcami dot ext not extent but ext dot declarative we are going to import declarative base and also from sql alchemy dot or m we are going to use session maker so session maker now after this we need to create the url for the database now if you see so you can see that as i have already said they are using sqlite but you can change this to another database let me just copy this and paste this in here so now instead of postgres we are using mysql and this is the user so the username is root and i don't have any password uh for the database so for this i can just leave it like like this and this is the uh it's localhost and this is the db name and for the db name we can give it fast api so after doing this now we need to create our engine in here and we can just say create engine and the create engine we need to pass our this uh database url like this now if you're using sqlite you need to also add another parameter in here now according to their documentation if you're using sqlite you need to also add this connect arcs and you need to add this but if you're using for example mysql and postgres you don't need to add that and you can just leave it like this so now after doing this uh we need to create a session uh we need to create the object of the session maker so we can just say session local and we can say session maker and you need to pass some arguments or parameters in here for example auto flash and it uh takes a boolean volume and also autocommit so we can just uh in here we can just say uh for are example commit and we can just make it false and also auto flash so we can just make it false and the last one that we want to add our or bind our engine in here so we can just say bind like this and we can add our engine like this now after doing this and the last step is that we need to create our declarative base so we can just say base and declarative base like this so now the database is created after creating of the database we need to create our models now models are the tables that we want to create so we have uh sql alchemy models and we have uh pythentic models now the identic models are the structure of the data models and the database are the actual tables that and the models is the actual tables that we want to create now we need to create another file in here and we can just call it models.pi so we can just say models.pi now the models.pi file first from the database we need to import our base so we can just say base now we from our this dot database we want to import the space and after uh not declarative base but it is base and after doing that we need to create some fields from sql alchemy so we can just say import column and also integer and we need string like this now we can create our table we want that our table name should be article and it extends from the base after that you need to specify the table name we can just add it like this table name you can we can just give it for example articles or you can just use it like this now this will be the name in our database in our mysql database now after doing this we need to specify the fields that we want we want three fields so the first one is id and we can just say column so column and it should be integer and also we can just give it primary key to true and also we can just add an index we can just make it to two and now we need a title and this should be column and string and also we need to specify the character so 100 we want to just use it let me just copy this and for another one is that this encryption and this should be for example 400 or whatever you can just give it so now we have created our uh database model now after creating of the database model you need to create your schema so now if you see the documentation so in here so this is the database connection and after the database connection that we have done uh okay now we need to create our the database model so we have created our database models and there is no problem we have created our table with some fields and after creating of that now we need to create the pythentic models or we can say the schema models and you can see that there is telling the 12-volt confusion confusion between the sql alchemy models and the pythentic models we will have the file model dot by with the sql algorithm models and the file is schema.pi with the identic models now the pythentic is just uh we can say that it's the structure or you can see that these pedentic models define more or less as schema a valid data shape so now let's create this we are going to create another python file at name of schemas so and here we can just say apis in the apis folder we can create schemas.pi now in the schemas.pi file we need to import so we can just say from pydentic we want to import base model and now we can just create a class article schema and in here it extends from the base model and now we need to define for example the id should be integer the title should be string and these are the fields that we will have in our database and our models dot pi file and this encryption should be string like this okay so now we have created our database connection according to the documentation after that we have created our models dot by file we have created our tables uh with some fields and we have created our schema or we can say our identity model for the for example for the structure now we need to add this in our we can just add this and the main.pi file so now in the main.pi file we need to remove some codes from here we don't need this we are going to remove this and we don't need this data and i'm going to just remove this route we'll create new routes so we want to just leave this app dot get and okay now in here the first thing is that we need to just say from database we are going to import our engine and also session local and after importing this also we can just say from current directory we want to import our models and in here we can simply say models dot base dot method data so we can just say meta data dot create all and in here we can just bind our engine so we can just say bind engine okay so right now if you see in our this uh fast api database we don't have any tables now if i save this i should see a table okay now let me just check my fast api now so you're not seeing any table let me just save this okay now okay now let me just reload the server again and close this and let me run it again and okay now i am seeing any error in here and it's telling that table let me just okay now in table name we have a right thing so in the models okay now it is table name it should be like this so now we can save this and let's run our server again okay now we don't have any error but if you see our fast api we have an articles table and we can click on this and you can see that we have id title and description all right guys now in the previous part we have learned that how you can create your database how you can work with sql alchemy how you can create your models and how you can create your uh pydentic models that we have created now it's time to create our first child route and we want to insert some data to our mysql database right now if you see we have our fast api database but in the articles table we don't have any data so we want to insert some data in here now we need to create our post uh route in here so let me just remove this get from here okay so the first thing is that we need to just say at app dot post and it is articles and after that we are going to just say diff at article now in the default add article you need to specify some parameters now the first one is that you need to add the schema that you have created so as you can see that we have created our article schema in here now we need to first we need to import this and after that we need to use that in our uh and add article so now in here i can just say from we can just say dot schemas we want to import article schema so you can just say article schema okay now this is the first parameter or query parameter that we need to add in here and we can give it a name for example article and it is article schema after that we have another uh parameter that we want to add that is our db session now if you see a fast api documentation and uh so let me just bring it where is it yeah now you can see that in here we need to create our db session and we need to tell that it depends on our uh get db method and our dependency now gdp is uh get dbs and dependency and you can see that for creating of the dependency for this work you need to use python 3.7 or above or and python 3.6 install the back ports and now you can see that using the get db we can create a session local class and this class is as we have already said that this class is created in our database dot by file and also we need to have an independent database session connection or we can say session local per request and use the same session through all the requests and then close it after the request is finished now for that we are using yelt and you can see that we will create a new dependency with yield and our dependency will create a new sql alchemy session local that will be used in a single request and then close it once the request is finished now we need to create that uh you can see this dependency we are going to just copy this so first we need to import this uh or first let me just copy this and i'm going to add that in here and first we need to import now you can see that from database this session local is imported and the session local is located in our database and here we have created our session maker and after this now you can see we are using try and yield db and finally we are closing that and it should be smallest because we are using a small lesson here okay so now in here we can just say db and we can just say session and after that we need to add we need to say that it depends you can just say it depends on our this get db and we can just add git db in here and this section okay now we need to import the session and i think it's related to the fast api or from okay session so let me just check that this session is oh we can just say okay yeah you can see that this is related to sql alchemy.org import session and let me just copy this and paste it in here so from sql alchemy.org we are going to import session okay so now this is uh completed now we want to create our adding functionality so i can just say new article and we can say model start article and okay now in our model we have title we need to add the title and it should be article dot title and also for the description we are going to use article dot description okay now after that we need to add this to our database so we can just say db.ad and we want to add our new article and after that we need to just commit the database so we can just say db.commat and also we need to just refresh the database so db.refresh and we can pass the article and at the end we can just return our new article okay so now it's created and now let's check our fast dpi swagger ui let me just refresh this so i think we have a problem uh yeah in here and here we have uh trailing commas not allowed oh okay sorry we have added a comma in here now let's just check it again and we are going to and now the server is reloading and now okay now let's just see this so now we can see that we have post in here and we can just click on this so now you can see that we have our id title and also description now in here i don't want the id so this is the structure that we have defined in our schema so if you see our schemas dot pi so we have id title and also description now because my ideas uh are to uh incrementing that it has a primary key of my models dot pi so i don't want to this id and i can just remove it from here and when i remove this from here you will see that i'm not seeing the id in here because i have removed this from the schema structure now if i try it out again and so let me just go to the post and or let me just make it refresh this again and we can post and now you can see that we have just title and description and we don't have the id now let's add the data uh i can just say this is my first title this is my first description and i can just click on the execute so now you can see how we are receiving a response body and the id the title and also the description now this is added automatically the id let's check our database that we have this or not so in the fast api we can just go to the articles and now you can see that we have our first data in here you can do the same with the uh with the postman let me just copy this and i'm going to open the postman let me just add this url and i can just post so we can grab this data and we can paste it in the body and we can just go to the row and add this and let me change this to json and remove the id because we don't need and we can just hit this or we can just say add it by postman so add it by postman and also you can bring change to description added description by postman and now we can send so now you can see that the data is added and if we check our uh fast api database and you can see that the second uh article is added added by postman and this is the description now if you see in here we have the status quo 200 but for example when you want to create a data for that we should have 201 uh so for that what we want to do is to add a status quo and fast api so we can just open our main.pi and where we have our this post route and here we can add a status code so we can just say status code and first we need to import the stats in here so from the fast api import status and so in this section we can just say status dot and you'll see that for example we have 100 continue processing 200 okay we want to create a 201 created okay now let's just try it once more and we want to add another data and we want to check the status code or not that it's working or not let's click on the post try it out and let's just a new title so we can just say testing status code this is our description for the status code and let's just execute and the data is added now you can see that we are receiving 201 response and this is the response body and if we see we will have the data in our fast api database article table and you can see that this is our data now let's learn that how we can get our data now so for right now if you see we have some data in our database three data and uh we have just created our post route now we know we need to create another route that's for getting all the data so for this uh in my main.py file what i want to do is to create another get route so we can just say an at app.get so articles we want to get our all articles and this should be get article so you can give it any name but i'm going to just give it get article so now in here we need to add our parameters so because it's related to the it depends to that get db uh so we need to add that in here like this now after this after that we need to create our query so we can just say my articles and we can just say db.qt and in here we can add our model so models.article so our models is article dot all so we want to get the all articles and we want to store that in our my articles variable and at the end we can just return this my articles just simple return my articles so we have created a new route a name of articles and after that we are going to just qd over the all articles and after that uh then we are going to just return the articles now let's uh check our swagger ui so you can see we have another route get i'm going to click on this and try it out and execute okay so now we can see that we are receiving our three data and this is the code and we have our description uh three data that we have you can see that this is the last one testing status code and the same as we have in our uh database and also we have the id now let's just test this in the postman and let's just add it in here and i want to this time it should be git and we want to send the request and you will see that we are receiving our three data in here so now we can see the data one data two and data three so now it's working perfectly but in here what i want to do is to remove this id because i don't need the id i just need the description and also title now for that we're going to use response model now if you see a fast api documentation you can declare the model used for the response with the parameters a response model and any of the path operations so we can add response model and or any path that's uh get post put or delete so and uh now let's just do this and we want to just uh and if you see uh the response model usage so you can see fast api will use this response model to convert the output data to its type declaration validation the data add a json schema for the response and open api path operation and will be used by automatic documentation systems but most importantly will limit the output data to that of the model we will see how that's important below now in here we want to just limit our data and we want to for example hide or remove the uh id now for that because uh if you seen here this is our schema this is related to the schema so we need to bring change to the schema now in here we can create another uh schema so we can just say my article schema and now this time this class extends from the article schema article schema and in here we can just add our title and also description like this so now after doing this we need to add this our article schema as a response model to the main dot pi file so where we have our get trout and here in the path we can just add a response uh model and first we need to import that uh so from dot schema article schema my article schema and we can add this in here so we can just say response model my article schema so now let's just try it and that it will work or not and we are just checking so this time if i uh so let me cancel this if i execute this so get try it out execute so we are receiving internal server error and if you see uh you'll see that we are assuming that value is not a valid date or type now and here you can see that we are uh curing over all the data we are getting all the data now if you see a fast api documentation and i'll let me just where is it it should be let me find it now you can see that it receives the same type you would declare for a pi identic model attribute so it can be a pidentic model but it can also be a list of identic models like a list item now here if you see we are getting a list of the articles so by this reason we need to add less than here so list list and after this we need to import this list and we can just import this from the typing so from typing we are going to import our list and now let's uh so also we need to add another thing in here because we are using uh orm model in here so skill alchemy or m now we need to just uh add a configuration for that in our schema so we can just open our schema and in my article schema that we have we are going to add a class config and so in here we can just say orm mode so orem mode and make this to true so we are making the rm remote to true okay now let's just check that it's working or not and we can just refresh this and let's get the article and now you can see that we are not seeing the id in here we have just title description if i try it out and if i execute and you can see that we are receiving the data without the id so like this you can get your data and fast api all right guys now let's learn that how we can get the specific article so for right now we have uh our articles for example i want just for example the first article or the second article now for that we need to create another route um we want to because we want to get the details of that article so for that in the main dot pi and here i'm going to just create another get route so i can just say it app dot get and it is articles and this time we are going to add id in here because we want to get the article according to the id now we can just create our article details function in here and we are going to pass id in here because we have already learned about this that how you can pass parameters in your path and after that you can add that in your functions like this so now we can just say my article so because it's just one article so we can just say my article and we can carry on that so db.qt uh we can qd on our also we need to also add the db in here because our this so this is related on the db uh depends on the git db by this result we also need to add this in here and also you can add your type in here so for example for the idea i want integer because by default it will be treated as a string now um here we can just say models dot article so we want to done our query on the article model and after that we want to filter that uh we want to filter according to the id so we can just say models.rt dot id should be according to the id that we have passed in here and we want the first one so we can just say dot first and uh now we can check that for example if we have this article we want to get that article and if you don't have that article we are going to just raise an exception and http exception so you can use http exception in the fast api and first we need to import that we can just say http exception so and uh and here we can just say uh if or we can just say for example if my article or if not if my article for example if we have the article we can just return the article we can just return the my article and if we don't have that we are going to just say in the else case we want to add an http exception so we can just say note return we can just http exception so we can erase an http exception we can add the status code so status code and this should be status dot http we can just say 404 not found and also we can add a message in here for example the article does not exists so like this we can just uh do it like this and now let's just check that it's working or not and also we are going to add by default uh for example if we had the article we want to add a status code of 200 so we can just say status status to the status code so you can just say status code dot for example http 200 okay okay now we can just check this that we have our new route or not and let's just refresh it again okay now you can see that we have another route get article and this is according to the id so if i click this we are receiving that the id is required and we need to add our id in here so for example i want to get the first article like this execute so we're receiving the first article in here and we're receiving 200 status code now for example i want to get the article 3 if you see we have article 3 also and if i execute this we are receiving the third article in here and also you can for example if you don't want the id you can add your response model as we have added in here so uh in this section uh so this is a response model and for this we don't need to add list i think because we are just getting one article it's not a query set so response model we will check this that it's working or not it is my article schema now let's try it once more let me cancel this and so for example try it and i want to get our article 2 execute yeah you can see we are receiving title description because when you're when you're using a query set or for example some more data or all the data then for that you need to add less but for example for one data you can just use the response model to the uh specific schema that you want now for example if i search for the article that is not in my database so five and execute we are receiving internal server error and message show it is not message yeah it's i think detail not message detail and i think this is built in the details built in and we need to use it like this and now let's just check it again so cancel and you can just try it out fifth execute yeah now you can see detail the article does not exist now let's just uh do this with a postman and you can just go to the postman and articles three we want to get the article tree and you can see that we are receiving the article three we don't have any id for this for example if i say article 7 and if i send this you can see that we are assuming that the article does not exist now there is another way that we can do this operations and the simplest way so right now if you see in here we are filtering the data and we are getting the data according to the specific id now uh instead of doing this we can just comment this and we are going to just say my article and we can just cutie over the models dot article and after that we can just simply say dot get and we want to get the article according to the specific id so now let's just check this and you can see that it's simple so easy and now if we go to the swagger ui and then here if we click on the get so i want to get according to the for example i want to get the second uh or we can say the third id and if i execute so now you can see that this is our title and this description so this is the third id and uh for example if i say the second id and if i execute and you can see that we are receiving updated by postman and uh so this is and you can do the same for or even we can add an id that's not existed and for example 7 and if i execute and you can see that the article does not found we can do the same in here and that's not a big deal because we haven't changed it anything we have just changed the curie and we are receiving the same result in here now let's learn that how we can update our data so we have learned that how we can insert a data how we can retrieve the data and how we can get the data according to the specific id now let's learn updating the data we want to create another route at app.put and we can just say articles and we want to update according to the specific id so we can add id in here also we are going to add our status code in here so we can just say status code and status dot 200 to accept it now we can create our method in here so we can just see our function uh update article now as we have already learned that when you are passing a path parameter then you need to pass that in your function so we can just add it in here id because we want to update the according to the id we want to update specific article according to the specific id that is belong to that article so now we need to do our query so we can just say db also we need to add our uh db in here because it's uh related to the uh get get db it depends and also we need to add our schema in here so for that we can just copy this to from here and we can add it in this section so we want our article schema and also we want our db now we can just use db.qd and and here we want to qd our article model so we can just say models.article and after that we want to filter because we want to update according to the id now if we see our models we have id in here so we want to just update that according to this id now in the filter we can say models dot article dot id should be according to the id that we are passing and after that we can just say dot update now we want to update the fields which fields we want to update so we want to pass object in here we have title and description we want to update our title and also description so we can just say title to the article.title so that we are receiving and the description should be according to the article dot description so we have title dot update title article the title and description article dot description and at the end you can just return something for example i want to just return a message in here so message and i can just say that the data is updated just simple we've created our port route we want to update according to the specific id also we have a status code after updating we want it should be 202 uh accepted because by default if we run this you will see that we have 200 but we want it should be 200 to accept it uh we have the id we have our schema and we have our db in here and after that we carry on our articles we want to filter the article according to the id and after that we update our title and description and we just return a simple message now let's just try it so i can just open this so right there you can see that we have this data in here and we have a pot route let's click on this we want to update according to the id and you can see the id is required let's try it out let's add id one i want to update the id one and i want it should be for example i can just say updated title and updated description and let's execute so we are receiving 202 it means that it's working and the data is updated and also we are returning the message that the data is updated now if we see our database you can see updated title and also updated description we have and also you can do this using the postman so let's just open this we want to use postman this time so and i want to update the id 2 this time so this id that we have this id i want to update this so now the method should be put let me just make it port and also we can just grab this and we can just uh add this in here or not in here we want to just go to the body so we have title description here we have already added now let's just say updated by postman so this time updated by postman and updated description by postman let's send and we should see the updated data so you can see that the data is updated and if we see our database and the data is updated now let's just get our data in here so let me cancel this and we can just get our all data in here let's just try it out execute so we have our data you can see this is updated and also the second data is also updated all right guys now let's work on our last operation that is deleting functionality so let's just create our delete route so we can just say delete and we can just say articles and we want to delete according to the specific id and also we can add a status code so we can just say status code status dot so when we delete uh we can just say that uh uh 204 no content it means that after deleting we don't have any data so now let's just uh create delete article and in here i want to add id and uh i can just say for example it should be integer and after that uh it depends on this db session get db and let me just add this in here and now we can execute our query so db.qt and we want to query on the models dot our article so we can just say models.article and after that we want to filter the specific article that we want to delete so we can just say filter and models dot article dot id and it should be according to the id that we want to pass and after that we can simply just say delete also you can add for example a synchronization in here and they're different value for this so for example we have files we have evaluate so we're going to use false in here and you can read about this and see in sql alchemy uh documentation and after deleting we want to commit the database so we can just say db.commit and also we will do these functions so right now if you see as we have already said that we have a sync functionality and fast api now right now you can see that we haven't used any asynchronous functionalities in our fast api but we will do all of this uh using sync functionalities so right now we're going to just do it like this that i want to show you so we can just say db.commet and [Music] uh so now the route is created and we want to check uh so right now i don't have any data in my database because i have removed all the data and now let's just post the data in here and i can just try it out so i can just say this is my data this is my description and now i can just uh execute so now the first data is added now this is the id because i have used a lot of data in here now if i see you can see that i have this data and now let's just add another one so let's just say a second data in here so i can just say second data and and we can just say my second description and let's execute and now the new data is added and we have two data in here you can see so i want to remove this uh data and let me just refresh this and now i can go to delete so i can add the id in here and i can just execute this so now you can see we are receiving 204 it means 200 for no content and it means that we don't have any content in here and here so now this data should be removed if i see articles you can see that we don't have this data in here so now this was using up so we have done our crowd operation using simple uh functions now uh in the next part we'll learn that how we can do these functionalities using uh assign functionality and fast api okay guys now let's create our crowd operation using our passing functionality now if you see a fast api documentation about def vcs and div now here we are using sql alchemy code inside of the path operation function and in the dependency and in turn it will go and communicate with with an external database that in our case it was mysql database now that could potentially require some weighting but as sql alchemy doesn't have compatibility for using a weight directly as would be for example something like this user away db.query user.first and instead we are using like this we have used it like this then we should declare the path operation functions and the dependency without a sync diff like normal function for example like different user or for in our case it was get article now it's saying that if you want to connect to your relational database uh asynchronously then you need to see this documentation now if we go in here so we can use encode databases for this so you can see you can also use encode database with fast api to connect databases using a sync and a weight so it's compatible with postgres mysql and also sqlite so if you go to the this encoded databases you can see that it is a sync database that supports for python and databases gives you a simple essential support for a large for a range of databases it allows you to make curious using the powerful sql alchemy core expression language and provide support for postgres mysql and also sqlite now databases suitable for integrating against any sync web framework like starlet sonic quartz and also fast api now the first thing is that you need to install this so uh you can use this pep installed databases and also because we are using mysql and for that you need to install the mysql driver so you can just use this comment pep install databases mysql so i've already installed it and if you're using another databases you can just use it uh according to their documentation now so up now along with this uh databases also you need to install fast api so i've already installed it you can just use pep install fast api and also you need to install mysql client with uh because we are using uh mysql client mysql database now for that you need to install mysql client and also uh you need to install uv corn and these are the libraries that we want to use so we have already done the installation in the first part of the course so and i don't want to install it again so you can just install these libraries so you can first install this databases mysql after that uvicorn fast dpi and also mysql client now let's start our coding and i've created an api folder package now in here i'm going to just create a new file at name of main.pi file so in the main.pi file what i want to do is to first from fast api i'm going to import fast api let's create our fast api in here so fast api and after that we need to create our route so i'm going to just create a simple get route so i can just say get and this is articles and we can just use sync dev for example get article and in here we are going to just return for example a message list of articles so now let's just run this and we are going to just open a new terminal in here we can just say uvi core and from here we are going to just use our api so we can just say api dot main and app and we can just say reload so now let's just wait for this and now you can see started through loader process and you can see that uv core running on this local host and this port now we can just check this localhost port 8000 so now you can see details not found because we are we have articles and you can see list of articles now we can just go to the docs so docs and for the swagger ui we can get try it out execute so now you can see that list of articles we have a message in here all right guys now let's create our databases and also we're going to create our uh tables in the database so first of all we're going to create a new python file and i'm going to just call it db.pi and then the db.pi the first thing is that we need to import so from uh sql uh alchemy uh we are going to import uh so the first one is column we want column and we want integer also we want meta data and string table and create engine so create engine so these are the imports that we will need in here so after uh importing now we need to uh also we need to import the databases we have already installed the databases and if you want to use a synchronous functionality for your database then you need to install the database uh you need to install that and after that import that so we can just say from databases we want to import database now let's create our database url okay so as we have already said that we are using mysql database now we need to add our database url in here so before this we need to create our database in here so i'm going to just call it article db and let me click on the create so this is my database name and uh for right now i don't have any table in here so now let's just add our uh url in here so we are going to use mysql database and after that the username is root uh and if you have password after this root you can add your password in here for example like this but i don't have any password i can just leave it like this so this is localhost so localhost and also the database name is articledb okay so we have created our database url in here now after doing uh this now what we need to do is to uh create our engine in here so we can just say engine create engine and you can add your database uh url so we can just say database url in here now it's time to create our table and we want to create article table so we can just say article and we want to create a table so table and we need to specify the metadata our fields for the table so we can just say metadata so now in here also we need to create our metadata in here so we can just say data and we can just say meta data so okay now we have our article in here uh we need to add uh for example column so we can just say column and the first column is for id and also we need to it should be integer the id should be integer and we can just add a primary key and by default we can just give it true like this so okay now after doing this let's create another uh column in here it is for the title and we don't need primary key and it is string and we can just give it for example hundred and let's just copy this we want another column for the description so column description and this should be 500 like this okay so now after adding these columns to our particle table now we need to create our query builder for the database and we can just say database and add our database url in here so we can just say database url so now uh it's time to add this in our main.pi file i'm going to open my main.pi file and then here we need to import so we can just say from.db we are going to import uh metadata and database and also engine so let's create our engine in here so we can just say metadata dot create all and we can pass the engine in here up also we need to uh create so let me just add this at the top and here up also we need to create uh our event for the starting and also disconnecting of the database so we can just say at app dot on event so this is start app and we can just say sync diff start app and in here we want to start our database and we can just simply say await database not databases but database dot connect and let's just copy this we want one more for the disconnecting of the database so i'm going to just say shutdown and this is shutdown and we want to use disconnect in here so we have imported our metadata database and also engine from the db.pi uh we have already created uh so you can see that we have our engine we have our metadata and and also our database that we have created at the bottom and after doing that we have created our engine and we have uh added our on event for event for the starting of the database and also for the disconnecting of the database now if you see the documentation of the fast api you can see that they are doing the same uh process in here so database url and you can see that we have on event startup database dot connect and on the shutdown we have a weight database dot disconnect as we are doing in here okay now i have saved this let's check that we have the table in our article db you can see that we have our table article table with the id title and description fields so now like this you can create your database code and fast api using async functionality alright guys now we have learned that how you can create your database tables and how you can create your database now we want to learn that how you can post some data or how you can insert some data so in our case we are going to post our title and description for the article uh as we have in our db.pi so we have id title and description now let's create our route and i'm going to just create my route in here so let me just copy this uh get and i want to change this to post and this is articles now let's just create our async diff insert article and now uh so the first thing is uh we uh also we need to add a status code in here so we want to create a status code of 201 and in here first we need to import that so we can just say stat okay like this and now we can just say uh and here we can add so you can just say code dot http and this is 201 created okay now let's create our query for inserting the data now before that we need to create our article schema so as we already talked that schema is the structure of the data now it's not the actual data that we want to add in the database it's not the db model but it's the identic model for our structure for our data structure now we want to create that and in the api i'm going to just create another file python file at name of schemas dot pi like this now in the schemas dot pi uh first we need to import so from identic so from pi uh then take we are going to import base model now after importing this now we need to create our article schema so class article schema and uh so this will be for the uh inserting the data and we want to just extend this from the base model and we want the title and we want to do and we want the description so the title is string and description is also string so now we have created our schemas dot py file and now in here we need to add our schemas up in our insert article so we want to add that now and here i'm going to just say article so first we need to import that and i can just say from uh dot schemas i'm going to import article schema like this and now we can just add this in here like this so after uh adding our schema now we can insert the data and we can run our qd we can just say cutie and also you need to import your table in here so if you remember how we have article table in here because we want to insert the data in the article table and by this reason we need to import this from db import article and now we can just secure the article dot insert dot values and now we need to add our values so we want the title and the title is article.title also we want the description and this is article dot uh dot description so now we have created our qd uh article and this is our table and we want to use insert for inserting the data and these are the values that we want to insert and we are getting that uh data from the from our uh schema or we can say that from the identic model now after doing this uh let's create uh we are going to just create last uh record id and we can just say uh a weight so await uh database dot execute so in here we are going to execute our query and we want to execute this cutie like this now if you see fast api documentation and they have for inserting now you can see that they have async dev create note and they are creating the qd nodes.values this is the value and this is the last record and after that they are returning this dictionary so we can just say return and uh so last record and this is our the spidentic model and you can just add it in here so article.txt and if you seen here there uh they have some tips for this now so notice up identic note object so in our case it's article so article is a pything article object and article directory in here it's telling that node.txt returns addict with its its data something like this for example text or completed and in our case it's title and also description but it doesn't have the id field and also so we create a new dick that contains a key volume pairs from node.txt with this syntax so now uh we have our this uh data uh inserting method uh let's just try this that it's working or not so we have our post articles this status code 201 created and let's go to the our mysql database and let's check our table and you'll see that right now uh you can see that in my article db and the article table uh for right now i don't have any data okay you can see uh i need title description now we can just uh refresh our swagger ui and we have a post in here and let's just add so you can see we have title and string and uh so this is our uh we can see that this is our schema or identic model and now let's just add uh this is our first title this is our first description and i can just execute this so now you can see that we are receiving the response body title description and also id now if i check my article database article table you can see that this is my first data that has been added uh also you can add response model uh for your this uh and here for your post method so for that we need to create another uh identic model uh in here uh for the response uh for uh adding that in the response model so i'm going to just copy this and paste this in here now you can extend this from the base model if you're extending this from the base model then you can add the id in here but if you for example extending from the article schema and then you can remove these two because uh you can access to this and we can just add id in here integer and that is the type of ideas in teacher now after creating this uh now we can add this as our response model so i can just go to the main.pi and in the main.pi i can import this uh not uh we need to change the name so for example i'm going to just make it article schema so now we can import this in here and let's just add a response model uh in this section so you can just say response model is our article schema and now let's just another data we can just refresh this and post try it out our title our description and let's execute so now you can see that the data is added and this is our response body or we can see that our response model now the description and you can see that the second data is added in here now let's do the same functionality using postman we want to test our api our post end point using the postman and let's just grab this and then here i can just add this and this is post and let's just get the data so from the response body and i can just go to the body and we can just say row and from here i want to choose json and i don't need the id and now let's just say our title from postman our description from postman and now let's just send and you can see that the id3 data is added and if we check our mysql database you'll see that we have the third id data in here so you can see that we have this data in here so now we want to learn that how we can get all the articles so we have learned that how you can post the data now for getting the articles it's so simple we are going to create a new git route in here and this is articles and also we are going to add our response model in here because we are getting all the articles so by that result we are going to use so we can use first our schema in here so we can just say article schema but with the article schema we also need to add list because we want to because we want to get all the articles that secure set and it's a list of data by this reason we need to add less than here and we need to import this list and it is we can just use from typing import list okay now and here we can just say uh sync diff get articles and um we can just we don't need to add any schema in here and we can just directly use our query article dot select and after that we can just say return weight database dot fetch all because we want to fetch all the data and you can just say qd to qd so it's so simple we are creating our get trout in here so the response model is uh the list of the article schema and we can just just use the significant articles and after that we can just qd over the articles and we return the fetching data so now let's check our this route and we can just go to the swagger ui and we can just refresh this so we have one get route now let me just remove the trout from here uh yeah this uh yeah this is route we can just remove this and we have an error in here expected and no we don't have any or so now let's just check okay now we have our get trout in here i'm going to just click on this and try it out after that execute so i think we have an error let's check no we don't have any error and oh yeah we have the data yeah yeah this is the data this is the title the description and we have the three data in here let's execute it once more and you'll see that we have the data in here now let's just use this in the postman and this time we want to get the articles let's send and yeah you can see that we are receiving the three data in here so we have learned that how we can get all the articles now we want to get this and specific article according to the id for example if i write id one i should get the article uh that's related to id1 now for this we need to create another get route so we can just say it app dot get and we can just say articles this time we want it should be according to the specific id and also we need to add the response model in here so we can just say response model but we this time we don't need a list because it's just one article and we can just use response model to article schema uh now let's create our uh method in here and it is git so i can just call it get or we can call it another name for example get details i can just say get details so now in the get details i want to add id because we want to get the data according to the id and this is not id this is the end it's the type okay now let's just create our curie and we can just say article dot select dot where and and here uh we can just say id should be according to the article dot column dot id so we are matching the id and after doing this we are going to create another variable in here and we can just say await database dot fetch one because this time we are fetching just one data so we can just use it like this and we can add cutie cutie now we want to check that if not my article if we don't have the article we want to just add a http exception so we need to import that in here and i can just say http exception and so in here we can erase an http exception and with the status code of we can just say status dot http so we want to use 404 yeah not found and we can add also a detail in here so for example we can just say article not found so in the else case what we want so we want to just return the dictionary of my article so you can just say my article so now this is just a simple route for getting this specific article so we want to get our article according to the id we have a response model of article schema and this is good details and we want to get that according to the specific id now we can just go to the route to the swagger ui and we want to check this okay now you can see we have get and for example i want to get id1 and i can just try it out id1 execute so now you can see that i am receiving id1 in here now let's just check this with the postman and this time it's get id2 for example i want to get id2 and send so now you can see that we are receiving this title and also description with id 2. now let's learn about updating the data updating this specific data so let's create another route in here and i'm going to just copy this and i will bring changes to this so articles and i want it should be because we are going to update according to the id and we have a response model so it's article schema and we need to change this to put now let's just create our method and we can just say update article and we want to add our id and the types integer and also we need to add our article schema because we need that so for that we can just copy this from here so we need to add our article schema in for inserting the data because we need to pass some data and so now let's create our query so we can just say article so this is our table name and we want to use update method and we can just say dot we are so in here we are matching the ids so article [Music] dot column dot id it should be according to the id and we can just say dot values and in here we can add our values so for the title we want uh so we want our this schema so we can just say article dot title and for the description we want article dot description so title article.title and for the description article.description and now let's execute our query so we can just say wait database dot execute and we can add our qd in here and then we can just return as we have done in our posting the data so we can just return it like this so and instead of this we can just pass our id okay now let's just try our this route that it's working or not so we have a put route in here and we can just click on this and let's try it out so i want to update id1 let's just add our id in here and we can just say this is updated and you can just hit description is updated and let's execute so you can see that we're receiving this is updated and description is updated now let's check our database and you can see that this is updated and even uh so now let's let's just do this with a postman and we can add this in here so i want this time id to put and so in the body we can just say uh let me just say for the title updated by postman and our description and let me just add a update it in here so update it now let's uh send so the method support put okay now you can see updated by postman on our description updated from postman now let's get our let me just clear this and cancel this okay now let me just get my articles my articles and try it out execute and now you can see this is updated also this is updated now let's create our last operation that is deleting the data so we are going to just copy this and we need to change this to delete so we want to delete according to the id and instead of we don't need any article schema so for this we need to add a status code so [Music] let me just copy this and so we can remove this response model and we want it should be http and 204 no content now we can create our delete so we can just say delete article according to the specific id and now we can just create our cutie so article dot delete so we're going to use delete method dot where and we can just match the id so dot c dot id and it is according to the id and after this we can just say we can execute our query so we await database dot execute and we can add our qd and we can just return a simple message for example article delete it so we have created another route for deleting of the article and we want to delete that according to the specific idea and this is the status code and this is the cutie okay now let's check this that it's working or not so it's not working i'll wait outside oh sorry this should be a thing so always i'm forgotting so i think dev delete article now we can just try it once more so now we can okay now you can see we have delete and let's just try it so we want to delete the first one let's add id execute okay we are receiving 204 and and it should be deleted and if we see yeah the first one is gone and it's deleted now let's do this with a postman and i can just so let me just create a new route in here and you are okay let me add this in here and we can just use delete and let's uh so first one is deleted we want to delete the second id and let's delete it so we're receiving 200 for no content and now uh if we see our database we will not have the second id data yeah and also you can if you check so let me just close this and we can just get all articles try it out execute and now we have just one article in here all right guys now in this part we're going to learn that how we can structure our project so for right now you can see that my all routes are located in the main.pi file so we are we want to clean this because we will create our user system and for that reason we need to create a separate file for the articles crowd operation now before that let me just run my server so i can just say uv icon or we can just say uv core api dot main app and reload so now for doing that we are going to use uh api router from fast api so now this is my development server let me just go to the localhost docs and you will see uh our routes in here now so for this we're going to create a new uh python file and so in my api folder i'm going to just create a new file and i call it articles dot pi now in articles.pi we need to import our uh api router so we can just say from fast api we want to import api router now let's create our router in here so we can just say router and it is epi router now we can bring our all routes from main.pi file to the articles.pi file so i can just grab from the post up to the delete i can grab these files and i can bring these codes and i can bring that in here so i will obviously we will receive errors because we need to add some imports so the first thing is that we need to change this up to router so first let's change this this should be router router router and the last one is router so now we need some imports uh let's bring our imports from here now we need to import this schema we can just grab this and add this in here and also uh i'm going to remove that from here and i need these two and i can just add that in here and also i need this article from dot db import article and let's just grab this list from typing and we can add this in here so uh also we need to import database so you can just say database from db and now i think we don't have any urine here and these are all our all routes so now if i refresh and if i save this main dot pi so the main.pi you can see that right now i don't have any route and see if i go to the fast api swagger ui you will see that i don't have any route in here okay now how we can add our this articles in our main.pi file now for this the first thing is that we need to import this so from dot import or we can just say from dot import articles and now at the end we can just say app dot uh router link oh or it's i think it's include router yeah include router and in here we can just say articles dot [Music] router okay now we want to check that we have our so let me refresh my docs water ui okay now you can see that we have all our routes in here now if i click on the get i can try it out i'm going to execute this so i'm seeing my one data in here now yeah i think i have just one data in my article db yeah this is the third one and this is working and let me just close this we can post an article try it out let's add a new data this is the new data and we can just execute so you can see that this is added and obvious obviously we will see this in here okay now you can see this is our id4 and we can get a specific article so for example i want to get the fourth one and i can just say try it out for and i can execute so i'm seeing the fourth one article and even i can update this so try it out id4 and let's just say for example update it and update it and let's execute and you can see that this is updated and also the last one is delete we can delete i can try it out i want to delete the fourth one execute so 200 for no content and let's check out our article so now we don't have the id4 in here all right guys now let's create our user system so now we have our article system now we want to add another uh table database table for the users now the first thing is and uh i need to open my db.pi file and i need to create another table so i can just copy this and paste this in here and this should be i can just call it user and this is user i need id i need username and also password and so we can just say username and also password or we can just give it for example so you can just give it as you want so for example this should be the username 20. so i've created a new table at name of user and this is our metadata we need three fields the id uh the username and also the password so now let's just save this now when i save this after that if i check my article my article db you'll see that i'm you can see that i have a new user table in here with id username and also password okay now let's create our routes for for example inserting the user now the first thing is that i'm going to just so let me just close this db i need to create another uh python file so as i've already created for my articles and i have added my routes in here so i need to create another python file and i'm going to just call it users.pi so we need some imports and for this i'm going to copy some codes because i don't want it should be because i i have already described this and let me just add these imports in here and we need schema for this and and here i can just create uh so let me just copy this so now this is user schema in uh so that we want to insert the data we have a username and also password and okay and this should be from base model and let's just copy this and we want to add this once more and this time we want the id even in the because we are adding this in the response model and in the response model we don't need the password but for right now we are going to just add the password in here but we you can remove this and we do need to add the password in here and uh i think let me just remove this and i don't need this i just need my username and also id and username and response model so now we need to import this uh the first one is user schema in the second one is user schema and also we need some more copy copy pasting so for inserting i'm going to just copy all of this routes and we will bring some changes to this okay so the first this is uh users not articles we have a status code and this is 201 we want to get and for the response model we want to use user schema instead of article schema like this and this is async diff this should be insert user and this is our article and for this we can just use user schema in so qd and from db we want to import our user table and instead of this we can just use user so user.values now the value that we want to insert the first one is username so we can just say username and this is we need to change this to user okay this should be user dot username and for the password we can just use a user.password so user dot password now we are receiving last record with database dot execute and we are executing our qd and in here we want to just say return user.txt so now this is our route for inserting the data for the users now right now we are not uh seeing this because we need to add this in our main.pi file so we can just open this and from here we need to import our users and we can just copy this app dot include router and we want to add our so users.router yeah users.router okay now let's just check our new routes and we can just go to there so now you can see that we have a user route in here and let me just click on this and now you can see that and here we need to pass the username and password because in our schemas dot pi file we have our user schema in and in the response model if you see we are receiving our id and also username and it's because because in our user schema in the response model we want just id and also the username now let's add a new user so i'm going to just say [Music] like this and i can do it like this and i can execute so i should see uh yeah now you can see that we have a response body with id username but also the password is added and let's check our database so now this article db users so now you can see that we have our id1 and also username and password now the problem is this that our password is not hashed and it's a plain text now for this we need to use another library that's called passlip now first we need to install this i have already installed it this past slip and now let's just do our password hashing in the users so first we need to import this library so we can just say from passlip dot hash from paslib.hash so there are different algorithms that you can use so let me just add this one it has or let me just add this so let me show you the documentation now if you see uh the pass slip so first we need to import this so i think i have already import uh now i haven't imported so we can just do it like this and after that you can just use it like this so dot hash now what you want to do is in here so before inserting the data you can just create a hashed password and we can use this dot hash and in here what you want to do is to just say user dot password hashed password and in here so now instead of uh now in the in here inserting the data instead of this user.password we want to pass our hashed password so we have just imported this library and after that we have used this algorithm for hashing the password and now if we go to the swagger ui and now if i post try it out and this time we want to add so let me just add it like this and now we can just execute so we are receiving internal server later now let's check that where is the problem all right guys now we're receiving an error from pi mysql that it's linked data too long for call and password now it's because because now we are hashing the password and if you see our db.pyen and here we have added 2050 for the password now we need to just make it more as we had so for example 200 and let me just also make this to for example 70 now or we can just say hundred we need to remove this user table because we need to drop this and after that we need to save this again and i think it should be in the privilege and we can just or uh i just where is it how we can remove this db from here this table not db okay now this uh not data obviously we don't want to remove the database but we want to just remove the table so we can just delete it from here i think okay it's deleting the row okay now you can just go to the operations and from here you can just say delete the table or we can say drop this table okay and if okay now the pop users table is deleted and let's just save this and now it's reloading and we can just check over this article db and then we have our user and id username and password now this time let's just try it once more i should not get any error this time and i can just click on the post try it out so we can just add like this and we can just add it like this now let's execute so now you can see the id is added the username is added now if we check our article not article sorry our users let me just refresh this okay now if i go to the user now you can see that i'm seeing the id1 username and this is the hashed password let's add another one and we can just say for example let me just make it one and let me just make it one and we can just execute so now the id2 is added with this username and if we check our user table i have another username with the hashed password in here now let's work on getting dollar users so we want to create our get tryout now uh we want to copy codes from our articles dot pi so we can just copy this and we can bring this in our users and we can change this to the users and the response model is uh this time because we are getting a q reset so we need to use less than here and we can add our user schema as a response model and we can just say get users so we need to use our user table so we can just secure user.select and we are going to return our query just simple and now let's just check our get route that it's working and i can just go to my swagger ui and now i have get users and if i click on this if i try it out execute i'm seeing my two users in here now uh and here you can see that we have our all routes now we want to add some tags for this for example attack for the articles and attack for the uh users that i want to separate them now for that you can just add that so you can do that in your path in here but if you want to add for all of them you can just you add that in your router so here we can just say tags and we can just say users and for the article we want to do the same so you can just add tags and it is articles uh steer object so i think let me check that uh white's giving error okay guys yeah this should be added in a list like this and let's just do this for the user so user and now let's just check that it's working or not now the server is running and we can see so now you can see that this is our articles and we have another one for the users let's work on our login functionality so i'm going to create a new uh python file in here and this should be for the i'm going to just call it you can just call it whatever you want let me just call it art dot pi now uh in this file uh we need some imports uh we can just grab these imports from the articles let me just copy these and we will bring changes to this so it's from and now from db we want to import user and we don't need this to schema but we want to create our schema for the login so by this reason i'm going to just open my uh schemas.pi file and in here we want to create a login schema so or we can say login python thick model and so we can just say login or login schema there's no problem and we can just add so username and also password so password and it is sdr str okay so now we can just open our pi file and in here we need to create our route for the login so also let me change this to art the tag and now i can just say at router dot post and this should be uh login and now we can create our login method so we can just say a sync diff login and now we can add our uh this uh schema that we have created and login schema and we can just grab this and first we need to import this like this and i can just add it in here so login schema and now let's just create our query so cutie and i can just say user dot select dot where and i can just say user dot c dot user name and it should be according to the request dot username now we can execute our query so await database dot fetch one and we can add our cutie like this so now first we are going to check that if we have this user so we can check if not my user so if we don't have this user we want to raise an http exception so i have i think if i can copy from here yeah so i can just raise that user not found also because we have hashing password now we need to also check uh that for for verifying of the hash password now for that we can just come down and from the users i think yeah we need to import this algorithm that we are using for the passion password and we need to import this and now we can just say if not and we can use this and you can use verify method and so first we want to use the request.password and after that we want to use the uh password from the user so we can just say my user dot password and and also in here we can just raise an exception and i can just say that password so this should be found and we can just say invalid passwords and in the else case we up so for right now we are going to just return a power user but uh and the next part instead of user we will return a token gwd token so in here we can just say return my user oh it is not cs but it is c so user.c dot username request.username and now let's add our this route this new login route in our dot pi file and so the first thing is that we need to import this and it's alt and we can just include that at the bottom so it is odd.router and now let's just refresh our swagger ui okay now we have aut in this section so first uh now you can see that we have username and password now the first thing is that i'm going to if you see i have two users in here with a hashed password so first i'm going to add the username correct but i'm going to add a wrong password for example like this a password and if i execute so we are receiving internal server error now let's check that where's the problem as this is the error line 15 and login user.select.where object has no attribute here so this should be like this select dot here and now let's check so it's reloading and uh okay now this time so so this is my username is correct but the password is not correct and now i can execute so again internal server error or got unexpected keyword details i think it's not detailed it's the tail and it should be detail detail and i think in the articles.pie i also made a mistake for this that it's not details it is i think detail so first let me just check this now if i execute so you can see that invalid password and we are receiving invalid password in here so because the password is invalid now if i add a wrong username and a valid password and if i execute now you can see user not found because we don't have this user in here and now if i add my correct username and password so this is the correct username and this is the correct password and if i execute now you can see that i'm receiving the id the username and also the hashed password in here so now let's just do this using the postman so now let's just go to the postman and login supposed and body row and in here so first let me okay now we need to add the username and password let me just copy this we can change this to json okay now if i give a correct username and password if i send so you can see that i am receiving back the username and the password hash password let me just try with another user with this user and now you can see that we are using receiving this user if i give a wrong username like this and you will see that i am and user not found and if the username is script if i give a wrong password for this username now you can see that we are receiving and valid password in here alright guys now let's talk about the token if you see uh in the previous part we have created our login system now instead of returning the uh power username and password we want to return or create a jwt token so now if you see the fast api documentation uh you can see that uh gwt means json web token and it says thunder to codify json object and a long dense string without spaces and it will be look like this so now let's just uh do our implementation now the first thing is uh that we need to install python juice now if you want to generate and verify the gwd tokens and python then we need to install this python juice library so you can just use pep install python just and it will be installed so i've already installed it now you can see that there are different steps so we have already done password hashing and we do need to do this we have already installed uh passlip and we have done our hashing password and we have verified the password so uh now for creating the access token uh if we come down let's just come down so there will be something yeah now we can see that we can use this create access token for creating of the token now first we need to import add these two so let me just copy this so in here i'm going to create a new file and i'm going to just call it token dot pi and first we are going to just add these two in here and also we need some imports uh so we need to import this uh jwt we can add this in here so i think we do need this gwt roof right now we can just use jwt and now we can come down and also uh so i think for right now we don't need this class token uh identity model uh we are going to just copy this from here div create access token and we can add it in here so now this is optional this time i'm going to just remove it and also we can just remove this or we can just remove this too and let me just bring it in here so now in here we are going to use to encode data.copy and after that this is the expiry date time also we need to import this uh let me just make it for example 60 minutes and we need to import date time and let's grab this date time from yeah from here and we don't need this time dult i think so in here and here you can see that we are updating the expiry date and this is the place that we want to encode the gwt and this is our twin code this is the secret key and this is the algorithm and we return the encoded gwt so now we've created this we want to uh add this where we are logging so where we are going to log in now if i go to the users.pi or odd.buy not users.pi so in here now in here i'm checking if the user is correct and if the user is i'm checking the hash password of the user and after that i'm returning user now instead of this we are going to create our x token in here now for this uh let's just come down uh let's check this uh yeah you can see that login for access token now after doing the required things we want to use this let me just copy this and paste it in here so now uh we uh we don't need this and let me just remove this so we can just use so we haven't imported this create access token now also we are going to remove this and let's just bring this in here and now we need to import this from the token dot by create access token so we can just say from dot token import create access token and so in here we are using access token and we want to create our access token and we want to create that according to the username so this is our my user so let's change this to my user dot username and we are returning the access token and the token type is bearer so now let's check this that it's working or not time delta we have removed the time delta we need to import it back so time delta so now let's check that it's working or not and now i can just refresh my swagger ui now if you see i have these users in here and with the hashed password so for example this time i want to log in with this user okay now in here i'm going to come down in the login so let's just add the username so so this is my username and let me just add a wrong password this time so first let me just add the drawing password and if i execute so i'm receiving invalid password because it's invalid and let's just add our correct password and now if i execute this now you can see that we are receiving 200 in the response body and this is our access token and this is the token type and that is b error now let's just do this using the postman let's test our this end point so we can just say login and we want to post this and let's just go to the body and in here we want the row and let's copy our this username and password we'll change this to uh we have another username and password in here and we want to use this so let's just add change this to json and so this time okay now let me first give the wrong password with the postman and you'll see that i'm receiving an valid password yeah and if i make this to tree and you want to send this so you can see that i'm seeing the access token and access and token type in here now we have our password and username system and also we have gwt token now we want to protect our routes in here so for example we want to protect these routes against unauthorized users so for this we are going to just go to the documentation and for this we're going to use oauth password bearer and also or2 password request for so the first thing what you want to do is to create this password bearer and we can just uh you can use it like this so let me just copy this and i want to do this in the token dot pi and i can just add this in here and uh so this should be login route and we can just add this is the token url route and it should be login so now we need to import this award to i think so we haven't imported this yeah we need to import this uh from the top and we want to import these two let me just copy this and add it in here so after importing this now let's just use our method in here for protecting our routes now we can just use uh we can just copy uh the codes from the documentation so we use this async diff get current user and this is the place that it depends on or2 schema and the what to schema is the or2 password bearer that we have in here so this is now we can just grab uh this code uh a significant current user and i can just copy this and i want to paste this in here so in this section okay [Music] so now uh we need to bring some chains in here uh also there are some imports that we need for example we need to import the pens http exception so we can just say from fast api we want to import http exception so it's http exception and also depends uh let me just copy it and add this in here and also we need to import status we can just grab this and add this in here so now we have a depends and i think also we need to import token data now this is the pythentic model or we can say schema model we need to create this in our schemas.pi file so i'm going to just open my schemas and let's just copy this and it should be yeah it's token data let me just copy this and i can add this uh after this login schema and we need to import optional uh from typing so you can just say from typing import optional and okay so and so this token data token and [Music] race credential exceptions okay now we need to import that in here so you can just say from schemas we want to import token data and we need to import the gwt error also from jos library okay and we don't need this fake user because we have user from the database so we can just remove this and we don't need this so now this is our talk uh uh get current user so it depends on what two and this is our credentials uh status code in here now if that for example if the uh username and password is uh wrong we can just say could not validate credentials and this is the header the header is better and this is our payload that we are going to just decode our token in here and this is the secret key and also algorithm and then here we are checking that if username is none we can just erase the credential exceptions so now this is created after that what we want to do is to add this get current user in our uh every route uh or every endpoint that we have so now if you see the documentation and so create access token so this is our get current user now when they're using this uh you can see so the first thing is that we want to uh for the login we want to add this or2 password request form because it depends on this so we can just copy this and we can come to the r.p file and in here now instead of our login that we have so this login login schema we want to add this or to password request form and it depends on this depends and under depends we don't want to add anything so we need to import this uh from fast api we want to also import depends and also we need to import this password or two password request for and i think we should get this from the token.pion in here we don't need that uh yeah and this in here we don't need that we just need or two password bearer so we can just copy this and we can just come to the art dot pi file and let me just add this in here and we are going to just remove this okay now after uh doing this uh let's just open our routes so first i want to just go to the articles.wi-fi and i want to uh protect some routes in here so for example let's just first do on the get so in the sync dev kit article now in here we can add our that current user so first we need to import that so in the articles dot pi we can just say from dot from dot tokens import get current user and now we can use this so let me just copy this and so on the get tryout and here on the git function uh we can add this and we can just say current user and username schema so we can just say user schema and now it depends it depends on this get current user so we have user schema in here now it's uh we don't have the user schema so we need to import that so user schema and also we need to import depends so current user user schema and it depends on git current so now let's just check that it's working or not and first let me just open my terminal and i can check that if i have a problem in here so we are receiving an error from the multipart we need to install the python multipart so if you see pipe.org so we can just install this and let me just go to the cmd and in here i can just say pep install python multipart okay now the library has been installed the multi-part library now let's check and uh okay now this is my users i have some users in here and let me just go to the swagger ui refresh it and now you can see that in my articles get article route so i have this log and i also i have an authorized button in here now for example if i try to get the articles you will see that i will receive error and you can see that not authenticated so so if i add my username and password so i have a username and also password and if i click on authorize now you can see that we have it's authorized now we have our username password and you can see we can log out now i can get the execute my query so if i execute and you can see that now i'm getting one article that i have in here so now you can check this using the postman and that so let's check our some more routes for example posting route so right now i haven't added the posting route now first let's just add this in our all routes so i can just copy this so i want to add this through and if a sync differences article and for the get article id and also we can just come down for update article and i think for the deleting also we need this we need to add this so now i think we are done with our all routes endpoints and we have added so now if i refresh this you'll see that my all routes our endpoints are locked you can see that i have locked in here uh so we have saw the get now let's post article and i can click on this so if i add this is so try it out let's add new data and my new description and i can execute so we are receiving not authenticated because i have newly added this now let's just add our so if you see this time let's just try with this another username so let me just add the password and we can just authorize and now we are authorized as this username and now we can execute this so execute so this is added the data is added and now because we are logged in now we can do our all operations for example we can get the specific id so article according to the id for example i want to get this article if i execute and you can see that i am receiving that also you can update or delete so let's just let's work on the update and try it out i want to update id this id and i can just say update it and updated description and let's execute and it's working updated updated okay now let's check this using postman and we want to do this using the postman so let me just copy this and first let me just refresh this okay now in the postman i want to get the articles so now if i send this you will see that i cannot get the article because i am not you can see not authenticated now i want to get my token so i can just go to the uh this login so this time i want to try this user and try it out so now let's just add our username and let's add our password and execute so this is my token now i can copy this token and i can just go to the headers and from here i can just add authorization so out to risation and you can just paste this in here and now if i send this so okay now you're receiving so response the status quo is 200 okay and let me just make it and you can see that i'm receiving my two data in here so like this you can build asynchronous and pines using fast dpi alright guys now this is the third part of our course now the first part we've learned that how you can create your endpoints using regular python functions and also we have used uh ormsql command that part now uh in the second part we have learned that how you can uh use encoded databases and how you can use async functionalities with core sql alchemy and site fast api so that was in the second part and also we have implemented the uh authentication gwt authentication and also login and registration system now this is the third part and and the third part we want to learn about another library that's called tortes orm that you can use asynchronous functionality with fast api and also orm functionality now so this is the documentation and they have implementation for the fast api now the first thing is that we need to install this now before installation of this library first we need to install fast api so you can just use prep install fast api and after that you need to install uv icon so you can just use pep install uv icon and in the third part you need to install this library so because we're using mysql database for the mysql we can use this comment and this is the driver for the mysql database that we can use and now after installation uh we have already created our project and virtual environment let's create a new file and i'm going to just call it main.wifi file and after that we need to create another file for our database so or for our models or we can say database so uh let me just call it model so i can just say models.fi file now in the main.pi file let's create our sample route so from fast api we are going to impo so yeah no sorry from fast api import fast api and let's create our app in here and i can just say fast dpi and we can just say sync dev and get first okay let me just say get article and then here i can just say it up dot get and we can just return message working okay so now let's just check this so if i go to localhost for the thousand and get i can just go to the docs to open the swagger ui and now you can see get try it out execute and we have message working now let's implement our this library now the first thing is that we need to create our model and also database connection so first we need to create our model in here and i can just say import so we can just use uh their documentation and i think so first we need to import these and i can just add it in here so we need fields models and also from this contrib library we need a pydentic model creator so now let's create our model it's article and it extends from models dot model now if you are familiar with the django uh and here a lot of concepts or a lot of code will be similar for you for example in here we are using models.model as we are using in django and for example we can give id so it is end field and we can just add pk so it's a primary key as we have in django and also so it should be fields dot and fields so fields dot enfield uh we need title and for the title we can give fields dot color or chart field as we had in django and we can give a max length of 255. let's copy this and we can just add it in here you want to change this for the description and let me just give it uh like this max lid now after creating of the model we need to create our pydentic model so we can just say article identic and we can just say paid antique model creator and we can just say article so we want to create our identic model according to the article table that we have and we can give a name the name is article and we will use this for the response model so it is article okay now let's copy and paste this we want another one and this should be article n paid intake and article n so now we have created our uh class article this will be our table with three fields and we have our pi identic model for our response model and also for the input model now we are going to open our main.py file and in the main.pi file we want to create our database connection so we have already imported fast api now from dot models we want to import article uh so article fidentic and article and identity and also okay so right now we need this import and also we need to import the register uh tortoise so from dot contrib dot fast api we want to import http not found error and also register tardis now let's create our mysql database connection in here and so first we are going to use this and now we need to add our app in here and after adding app we need to use a database url so we can just say database url and because we're using mysql database now let's create a database in here so i can just call it uh uh let me just say what m or we can just say article one so now article over m database is created we don't have any table for right now and we want to create our models table so now for the database url we want to use mysql so and after that the uh with the username is root and after that if you have a password you can give it your password but i don't have any password for that and it should be localhost you can give the port and also the database and the database name that's article worry and also we can add a generate schema make it to two uh also we need to add exception handlers and make it true and let's just add this in here so this is our database now if i save my this file you will see that we will have a article table in here and with this three fields that we have id title and also description now let's just save this and now we can just check our article over him uh okay so i have forgot to add our module in here and sorry for that after database url you need to add that which uh module that you want to create or which table that you want to create so we can just say models and this is epi dot models so in the epi folder we have our models dot py file and we want to create our table according to that model now let's just save this and this time we should see a table no we are not seeing any table for right now so article worry muscular route localhost and modules models api dot models and oh this should be yeah this should be i think db url not database url so app db url modules generate schema disk exception now let's check okay now you can see that we have article that's not database url but that is db url so we need to add our app fast api we need to our database url we need to have our modules so according to which model we want to create our table so now we have our article table and you can see that we have just id title and also description now let's go work on our first round that's inserting the data or inserting the article so if you see in our database i don't have any article in here so i have just id title and description so for this we need to create insert route so and here i can just say uh app dot this should be post because we are posting uh the data and this says articles and also we are going to add our response model so we can just say response model and that should be article uh pi dentique so as we have created this in our models dot pi file and we want to create this pi identic model according to the article table that we have so we can just say article python thing after this we need to create a sync diff in their article and in here we need to add our another pidentic model that's for inserting the data and that should be uh this article and paid intake and it will be create the pythonic model according to the article that we have or we can say schema it will create a schema now we can just say article and i can adjust that article and python tick now i can create article obj and i can say await article now we need to import the article i think uh i haven't imported yeah from dot models we want to import articles so this is the table name that we have in our models.pi file so because we want to insert the data to our article table so we can just say article dot create and we can just say article dot dect and we can exclude uh exclude and set to truth and at the end we can just return so we can just say return await uh article pidentique article identique dot from from tortoise and we want to add our article object in here so now we want to check our this post uh method that we have and let's just go to the fast api swagger ui okay now we have git and we have post let's post uh some data i have why i have id in here so i think i should also add a identic method here so let me just say class i didn't think identic meta and for right now i can just pass this okay now let's just check once the documentation and so they have clasp identicate on it's used because in here you can see that they're excluding password hash for example if you want to exclude something then you can add that in the fidentic meta but in here and user and pythentic we cannot exclude read only to true and it will exclude that random id number so now if i check my fast api swagger ui so if i click on the post yeah now you can see we have title description let's try it out and we can just say this is my new article we can use the article title and we can just say this is my new description and let's execute so we are receiving the id one title and description and if you check our it was article worry the table you should see that we have yeah now we can see that we have another id and we have an id one and title and description now let's try this using the postman so now let me just grab this request url from the swagger ui and now i can just click on the send and oh sorry the method should be get and not get it should be post software that get will receive our hard-coded message so you can see that so in here method is not allowed because we should have post and now in the body we can just grab this and let's just in the row we can change this to json and let's add this we can remove the id and let's just say added by post man and this is my news post new postman description i can just say for example post man description now let's send okay now you can see that we have id2 title and also add a description and uh you will see the data and you were in here so if i see and i can just see that we have the second id data in here now you can see that this is the second data let's work on getting all the articles so now in here we have a git article and instead of this hard coded message now we want to just get all the articles so i can just also add this articles and this is ok okay so now it's simple you can just say return await and we can use article identic dot from tortoise orem and we can add our curry in here and we can just say that article dot all because we want to get our all articles now and here we need to add a response model and we can just say response model and we because it is a q reset and it will have more data so it's a list of data lists of articles by this reason we need to add a less than here so we can just say list article identic and we need to import this list so it is from typing we want to import list so it's simple we are just using our article pydentic from tortoise.org orem and we can just say article.all because we want to get our all articles and so right now we have two data and let's just try it in the swagger ui and now you can see we have get if i click on this if i try it out execute internal server error and we have an error i think and so sorry it's not from tortoise orem but it's from curie set sorry for this uh from cutie set because we want to get the q reset so now let's just try it once more so we can get try it out execute so now you can see that we have our two data in here so id1 and id2 you can try this with the postman and we can just say and this time we want to just use get and let's send uh now you can see that we have our two this is the id first and we have the id2 also in here now we want to get a specific uh article according to the id so as right now you can see we have get route but it is for the list of the articles for example if you want to get uh an article or a data according to the specific id then we need to create a route that should be uh according to the id in the route parameter so we can just say uh or we can just copy this from here and let me just paste it in this section and this time we want that it should be according to the id and also for the response uh model we want to use article pedentic but this time we don't want the lights because we want to get one specific article so we can just say response model article identic also we can add responses in here so we can just say responses and responses and in the responses we want to add for example if there uh for example if the id is not available we can throw a 404 of not 404 yeah like this 404 and we can just say uh model model and in here we can add our http not found error so we can just say http not found error so responses we have 404 and after that let me just add this i think i can bring this response model after the b for this so you can just grab this and i can just bring this in here like this okay now let's create our async div get for example you can say get details and we want to get according to the specific id and also we can just add this so the id should be integer so now we can just say re turn a weight article identity dot from q reset single so because it is a single uh article and you can just say article dot get an ids according to the id so just simple and now we can just try this and we should so right now we have two articles with id1 and id2 now so you can see we have another route git and the id is required let's add id for example id2 execute we have our id tune here if i add id1 execute i have id1 for example if i add an id that's not in my database for example id3 and if i execute so you can see that we are receiving the detail object it does not exist and you can do the same with the postman and that's not a problem and you can just say id3 and you can send so you will see that i'm receiving that object does not exist for example if i say 2 and if i send you'll see that i am receiving id to article and here all right guys now let's work on our pot method and we can say that how we can update the data so right now we have uh get uh get the details and also posting the articles now we can just uh so for right now i can just grab this and i need this and we can just add it in here and we have our we can change this to put so it's article and we want to update according to the id so we have response model and this article identic and we also have responses if the article does not exist we want to just uh throw an http not found error and now let's just say diff update article and the ids integer and also we need to add our article and pedentic so schema so as we have added in the posting so if you see and then zert article and we can just grab this and we can add this in here and like this now we can just say so a weight and dot filter so we want to filter and according to the id and after that we can update the data so we can just say article dot dict and we can just say exclude unset so exclude or we can just add this from here so now since it's not dot so wait filter and we want to filter according to the id so in here we can just say article dot filter so a weight article dot filter and we can just say id id dot update now this should be a sync so now we can return so we can just say return a wait article paid antique uh dot from q reset single and we can just say article uh dot get id id so now let's just check our updating functionality that it's working or not so we have bot and we want to put according to the id or update so i want to update id1 and let's just say this is updated and we can just say updated description let's execute and now you can see that this is updated and updated description and if we check our mysql database you will see that the data is updated so now for example if i uh add an article that is not for example not in here you will see that i'm receiving error so for example in here so id 1 if i say id 3 and if i execute so you can see that object does not exist and so now let's just uh do this using the postman and now you can see that this is updated so let's just use this and we want to put a car by using the postman so it will be there will be no problem and i think uh but i want to show you that it's good we can just use it like this and let's just say added by postman change this to updated updated by postman and i can just update this and updated by postman and now if i get my all articles so we can just say try it out execute and you can see that this is updated in the first time and this is updated by postman now let's work on our last operation that is deleting the article so let's just copy this at app.port and we can just grab it in here so we so now in the response model this time we're not going to use a pythentic uh article identic because we're not going to return anything but we can just add a status message in here we can create a new pedentic class in here so first now first i need to import that and i can just say or we can say from pi identic import we want base model so base model and now we can create our class and we can just call it status and it extends from the base model and now we can add a message so the messages type is tr okay now we will use this status according to the response model for our article for the deleting of the article so instead of article identity we want to use status that we have newly created now after doing this now if there was uh an error we can just say http not found either for example if there was the id and the ideas not exist we can use this so and now let's just create a sync diff delete article and we want to delete according to the specific id so we can just add id integer and so now you can just say delete that article or you can say deleted count and we can just say a weight of filter so wait article dot filter and we want to filter according to the id id and id so now we can check if not deleted article so we can just raise http exception so http exception and we can add the status code in here so status code is status dot or http so we haven't imported this i think yeah we need to import this in here and let's just add this dot or even you can use it like this and also you can add it manually for example i can do it like this so status code for example 404 and there's no problem but if you want to make it dynamically you can just use status dot and i think it is 404 not found and this is raise and also in here we can just use it dot delete because you want to delete the data and also we can add up a message for this for example the tail and we can just say if a string user id not found and in the else case we can just return our status that we have created and we can add our message in here that fstring delete it article so it's not user sorry it's article and deleted article and we can just add the id in here so now also we need to change this to delete now let's check our last operation deleting that it's working or not and so we have our delete and we want to delete uh the article that is that has id1 and let's just execute yeah deleted article and now if we get all the articles and write out execute so we don't have that article yet and we have just id2 and for example if i add an article that does not exist for for example article 3 and if i execute so you can see that deleted or we shouldn't see this article 3 deleted let's just refresh this and let's just click on the delete for example if i say id5 and if i click on the execute so deleted article 5 i'm receiving this let's check that why we should not receive this uh 200 okay so you can see in here we're receiving 200 look in the response body so guys you can see that this is it's delete article we have deleted article in here so we need to just make this deleted article and now for example id5 if i execute and you can see article 5 not found because we don't have this article in here let's just try this with a postman and the last operation and for example if i say id5 and for example let me change just put to delete and let's just send so article 5 not found but i have article 2 and i want to delete that send so deleted article 2 and now if we check this again if we get try it out execute now so you can see responsibility is npt because we don't have any article in here so if i say article warren article so now you can see we have just id title and also description
Info
Channel: Parwiz Forogh
Views: 4,559
Rating: 5 out of 5
Keywords: How to Build Async API in Python with FastAPI, Build API in Python, Python API Course, async api in Python, FastAPI course, fastapi with python, fastapi async with python, python api tutorial, how to build api in python, how to build api with fastapi, Python
Id: IZUjJ3rPY1E
Channel Id: undefined
Length: 225min 24sec (13524 seconds)
Published: Sun Jun 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.