YTread Logo
YTread Logo

REST API Crash Course - Introduction + Full Python API Tutorial

Jun 07, 2021
Hello everyone, welcome to your Rest API



. My name is Caleb. In this video, we'll talk about everything you need to know to get started with the Rest API. We will talk about the concepts first and then continue. let's talk about how to consume an API and then we're going to build our own API in Python, so it's very useless or it's a lot of useful information, so if you need a reference of all this, I created a PDF that you can get for free with a link in the description, so check it out if you just need to like it, look at the definitions and study or whatever, but anyway we're going to start by first talking about what a


API is and why. you should care where this is used in the software and if you're watching this video and you're not really doing anything with the software I'm not sure why you're here but you can probably skip to one of my other videos so all my videos about software, so anyway, API stands for application programming interface, so I tried to talk and explain this last time I recorded this and it didn't go very well because I can't talk and write very well at the same time. programming interface is basically a fancy way of saying a way that two pieces of software can communicate with each other, so if we have this software here, this is a really cool software system that we built and here's another one and let's say they're integrated in different programming languages ​​that's where an API is going to come in and save the day, it allows different applications that aren't built exactly the same to communicate, so we'll say this one was built in Python and this one was built in JavaScript, poor developer.
rest api crash course   introduction full python api tutorial
I'm so sorry, an API allows us to create functionality in this application to make a request to this other application and then this application can give a response to this application and that's where the API comes in, we are building this interface for these pieces of software to Communicating with each other now is generally a one-way thing, so this application will not request information from this application and retrieve it, instead we always have a server and then a client, the client consumes the information provided. from the server so this is a very common setup that you will see on all computer systems the same way when you access a website you know you go to you make a request and then that server sends you back a response . works exactly the same way with the ar


API, although instead of getting HTML you will get Json, so Json is a language or a notation for describing information, it is like text and it means JavaScript object notation, so it is a way of scoring. what the objects look like, but you don't have to do it with JavaScript;
rest api crash course   introduction full python api tutorial

More Interesting Facts About,

rest api crash course introduction full python api tutorial...

It is actually supported by almost all languages ​​and this is now the standard for APIs. You may come across apis that use XML; however, this is a bit dated and probably not what you would like to use in the future, so I would recommend that even if you are more familiar with XML for some reason, learn Json or Json, you will hear both, so Json is not complicated, don't feel like you have to learn another programming. language or something, it's just a series of key-value pairs like an object in JavaScript, hence the word JavaScript object notation or like a dictionary in Python where you have a key and then a value associated with it, so it's pretty much just an associative matrix.
rest api crash course   introduction full python api tutorial
Let's look at what Json looks like and how to use it soon when we go to create our own API, but don't worry too much about it, so Json is basic, basically the communication language and the API, you know what things we can communicate to you. Maybe you want to get a list of users or maybe you want to update some comments. Whatever you want to do, it's all defined as API endpoints, so you know here where we're making that request. This is an example of an endpoint that Let's talk about the different types of endpoints that we can create now, what about this rest here what does it mean um?
rest api crash course   introduction full python api tutorial
I think representational state transfer. I had to look that up a moment ago because I was like representing what it was. Anyway, this is the means of communication so you know that I can talk to you in English, but how do I talk to you? Am I talking to you on the phone? Am I texting you? Am I talking to you face to face? Well, that describes how we are communicating and this aspect of the rest is communicating over the web or the Internet, so all of this will be done by making requests similar to how you would request a website, but instead of HTML, you are requesting Json just as I mentioned before.
To make this a little more concrete, let's walk through an example of an application I'm working on, so I have some software written in Python and this is the server software. You can also hear it as the back and the software and this is the software that We are going to communicate with a database which is where all of our information and data will be stored, so this communicates with the database. Now we want other pieces of software to be able to communicate with this backend software, but we don't just want them to. we will be able to access anything that we don't want to give direct access to this database so we expose different API endpoints so an example of an API endpoint would be a forward slash and I haven't described this for what serves the actual application. but go ahead drinks SL now the real app is to get a list of drinks and be able to rate them that you know from 0 to 10 or put them in different categories of how good they are and then you can basically keep track of all your favorites. drinks is a little silly but I thought it would be a fun project and basically I like to drink, I don't like to drink, I like to drink non-alcoholic drinks like Lroy or Stevia, energy drinks or whatever, but there are so many. of them I can't keep track of all of them so I just wanted to create a little website to sort them out and instead of just getting all the drinks you might want to get a specific drink so in this situation we would . pass an id, so in this situation we are getting data and that is one of the different API methods.
We're going to talk about AP methods here in a minute, but what this means is my software that I built here and in this situation. Let's say this is Javascript. I can get all drinks or a specific drink from my backend software by simply making a request to this web address. Now you might be seeing this as it doesn't look good like a web address because this is a web server, it's going to have an IP address or a domain name, so let's say I put it on a website, the favorite website of Caleb,, it's not a real website as far as I know, and then slash drinks slash and then you can pass in some ID, like five.
Now the actual structure of these URLs and the data you want to get is entirely up to you; however, there are some general standards and I will describe why I chose this soon, but Basically, we have the list of data that we are trying to get and then we pass the specific element that we want and this will return Json of all the information about that drink now, many times, when you have a website that you go to. have web pages that you know people can view in their web browser or you'll have the API and often these will be split into two subsets of applications so you can have favorite drinks from slap I SL drinks sl5 u another common thing. is to have a different domain as an api. calb favorit, not necessary, you can mix them, however, just for organization reasons, often the API is in a separate section, either a subdirectory or a subdomain, now you can see this configuration as I often do when I am. working on applications and asking why the complexity why do you need to have two applications and then the database why don't you just connect from this application to the database skipping all this garbage obviously you can do what you can do. whatever you want, but this is not always advisable for several reasons, the most important reason not to go from the client application directly to the database is security, specifically if you are using JavaScript now, many times the client will be JavaScript because it is uses for web pages, however, you can also create a console app or a mobile app, so it won't always be JavaScript, but if you go to a website, you can access the developer tools and you can see all this JavaScript or you can right click and view it. source and you'll have the JavaScript files linked there and you'll be able to see all of that, so you don't really want to put sensitive information on the client here because it's all visible to the person using your software, which means it could then go to the database. data themselves directly Make changes, you know, drop the database LOL, we don't want them to do that, so instead we just expose specific things that we want people to see, like the drinks list, but they can't enter there and see everyone's. user information or address of people or anything else Beyond security, the other big reason is versatility, so now we have a front-end application in JavaScript, but with this setup we could easily create a mobile application and yes, this It's a cell phone and we could also consume the same API, so what happens now is that both frontend applications share the same backend, which means they will always have the same data and will sync, that's how when you have applications on your phone, you can go in there and change data and then you can log in on your computer and all those things still exist, it's because they all share the same backend, so you know when you're messaging someone on social media, those messages are being sent to a backend server that does all the processing and all the hard work for the app and the website you're looking at is actually just a nice cover for this backend, so you know JavaScript, maybe your app here is in Swift.
But ultimately we are hiding the back end of this application and separating those, which brings up the next positive point, which is modularity, because the front end and the back end are separate and we are simply defining this consistent way of communicating that we can really exchange. different things come out and they don't necessarily break, I say necessarily because you know in practice it might be difficult, but in theory you should be able to swap different things without breaking other parts of the application, so right now this is written in Python, but it could be like I don't really feel like WR in Python, you know, and I decide to upgrade to something like Visual Basic and in this situation we can expose the exact same API endpoint so that this website and this mobile phone app don't even need. to know about those changes on the backend because they are still getting their drinks list, that's all they care about, they just want their drinks.
This is great because you can change the backend of the software without requiring an app update or requiring a new website implementation, as long as you can provide a consistent interface to work with your backend, last reason and then I promise I'll stop rambling. Okay, I'll ramble on a bit more, but it's basically the last thing I wanted. One of the reasons this setup is good is interoperability, which I think is a word, and basically what I'm saying here is that these front-end applications don't have to be created by you or the development team. front end for your business, you can actually expose certain endpoints like this right here and you could make it public, so if you make certain endpoints public you don't have to worry about authentication or authorization and in that situation anyone could move on and create some new application. new and improved to consume your API so you know if I don't like the look of Instagram I can create my own Instagram viewer because they have an API that can be consumed now sometimes you will still need to get some kind of token or some way of authorize to say: "I'm fine, I can use this API, but many times these APIs will be publicly available, so the API that we will consume soon is just a public API that anyone can use and Honestly, it's really cool, there are a lot of websites which you might not even realize they have APIs, so for example, if you wanted to do something like create a trading bot or a cryptocurrency trading bot, a lot of times different exchanges will have APIs so you can automate different things in the code. , so it's not always just about creating a pretty interface, it can also be about automation, how can we connect these applications and make things happen automatically?
If something is not public, it can be private but you can still access it, you just need one way. to verify that it's you and not just anyone going there and trading cryptocurrency or spending your money, so the authorization aspect of this is like another beast, but a common thing thatyou often see is O off2, which is an authentication technique that is quite common. So if you want to see how to secure your API and make it so you basically have to log in, you can check out o off, that's o o a u t h 2.
Now I want to take a moment to talk about the different methods of requesting data from the server. I've talked about get and this one is pretty simple, it's what is used to retrieve data from the server and these are often written in capital letters so you're like screaming, give me this information, so get is used to retrieve data. I could say just get data but FYI here it is fine so there are some others you should know and that is post is used to write data so whenever you want to write data to the server you will use post and then there .
It is deleted almost no capitalization there.but I fixed it and this is used to delete things. Now there is another one that is quite common. Why do I always write like this? I don't know and it gets and this is actually quite confusing if you're new and the difference between posting and posting is pretty gray and people even argue about which one I should use. Here is my opinion. Posts should be used to write new data. Posts should be used for writing, but in this situation we are going to update the data, so now I want to show you how these things correlate to the most popular things you have to do with the database, which is CR, so the different things you need to do we need to be able to create data, publish, read data, update, place and delete data.
Now, why exactly is there confusion between post and put? That's what I want to talk about now. Well, first let's talk about how post and put are generally used. post is used to add a resource, put is used to replace a resource and that is why it is natural in In my opinion, you can use put to update data and post to add new data, but replacing a resource requires some way to identify it and This is often done with an id, so my take is if you are making a post request you want to add. data, so you may have the address of the drinks and want to add a new drink with put.
You will probably need to identify which resource you want to replace so it looks like drinks SL600 five for an ID now with the configuration I I usually don't have this ID value until the data is already inserted into the database with a post, so when you add new data you don't know what that ID will be because it is automatically incremented in the database so if you want to update that data then you use the ID but there is another difference and this is more defined in the specification of how it is supposed to work an API and less on pure functionality because you are the one who defines the function. fun, so if you want your API to work properly you need to make sure you implement it this way and in a nutshell it is designed to give the same result every time if you make the same request, for example if you are replacing this drink with a new drink, you can replace this ID over and over again as long as that new one has exactly the same ID, so let's say you have a grape drink and you replace it with Cola and then you replace Cola again with Cola and then Cola again with Cola , no matter how many times you do this, the same resource exists and it doesn't hurt if you accidentally make that request multiple times.
The release is usually a little different because that feature or capability is not guaranteed, so what does that mean? is that if you add a new drink, let's say you add Cola and then add Cola again. In this situation, it doesn't replace anything, so every time you add Queue a new record is created in that table, so it's guaranteed to be able to put. be run numerous times without any side effects, on the other hand this specification does not guarantee and in that situation you could have duplicate records in the database or you could get an error on the second one if the drink name is defined. to be unique now I'm probably going to spoil this word but there is a word to describe this item yes it wasn't powerful I don't know so it's said to be like this I'm not even going to try Whether it's called now in general, I think there's a lot of confusion between posting and putting and you can do pretty much the same behavior with either one, so I tend to stick with posting most of the time, even if I'm updating a resource you can do it with posting everyone Either way, you can define it however you want, this is how with modern HTML you can make a section a div or you can be a little more specific and say oh, this is a footer gives the same results and if you're not using the footer page correctly, then you could make things worse, which is why a lot of people still end up using divs, so if you're using HTML, maybe it's those kinds of options. in perspective, I don't know, there is another method patch that is used to replace different parts of a resource, so maybe you just want to update one field;
However, in our situation, the data we are working with is so small that it is practically just easier to replace it with a new version, so you won't have to keep track of individual changes; However, if you're working with very large data you can definitely look for the patch, so my theory is that you basically maintain the state of an object in the client-side application and then once you save, just send that new state and replace the old state in the database. Hope


y that makes sense, so let's get some practice with APIs by consuming an existing API that we're going to grab.
Take a look at Stack Overflow so all the questions here can access this information via an API so we can build an app that works with Stack Overflow. To do this, go to API. and here you can see the documentation for all of this, so here are the different API endpoints so we can get the answers, badges, comments, posts, which is just a combination of questions and answers, and I'm going to cast a look at the questions here so let's get all the questions and here's an example of how you would invoke this so you can hit run and you can see in this web address there's this question mark and then some variables with values ​​assigned and then a sign and an Ampersand and a type, these are known as query parameters and can be used in this situation to modify the results of the API now if you want to know how to invoke this without running it in this little test window.
Basically, you just take this address here and you'll paste it after the API. uh this is what it's going to look like when you press Enter, you're going to get a bunch of data here, not exactly as pretty as this, which by the way, this is Json format, so it's just key value pairs and in the pairs you can have. has arrays or you could consider them lists if you're more familiar with Python, so we have items here which is a list of questions, each question is surrounded by braces so there's the opening brace and it has owner tags and many others things. and then it ends and then we have another question that has owner tags and a bunch of other things and it just repeats in a list, so what we're going to do is work with this API, a tool that I would recommend. what you get is called Postman and here you can work with your APIs and it's a little bit easier because you can customize the body and the different headers of your requests and what we're going to do is just take this web address here and paste it here so we have HTTP coli. and then forward SL questions and then all those other things so press submit and we'll get a response like this so basically this is Json here we have these elements and then a list of the different questions so let's move on forward. and we'll come back to Postman later, but for now let's take this URL and try to work with this in a Python script, so if you need help setting up a Python environment, you can check out my all-in-one programming for Python beginners, whatever That this.
I'm just going to assume that you can at least set up a Python environment and we're in a Python file. What we're going to do is we're going to need some tools, so we're going to say import requests and import. Json so if this is your first time working with requests you may need to install it so what we can do is open a terminal and say pip 3 install requests so now we have requests installed and we should be able to perform a request to a web page by saying request.get and pasting a web address inside a string here, so let's make sure this is nice and formatted correctly and that it will return a response so we can assign it to a response variable and then What we will do is print this response and see what we get and you can see that we get a 200 response which means okay, now we can see the data that is returned by running Json as a method here. and that will give us all the data exactly like we would if we requested it in the web browser or with postmen, so we got the elements and then a list of all the different questions so we can basically zoom in on this and instead of just having a key-value pair where we have elements and then a giant list, we could scan elements specifically by passing elements here, so let's index the elements and then run this now and now we only have one list, so we're not working with the elements anymore, We just have a list of questions, so what we could do is iterate over them, so let's remove this impression and we'll say for the data in the elements or if you want to be more. specific, you could say questions on the elements, this will be good from time to time, what we'll do is just print data and you can print each one individually like this or you can be more specific and take something like the title.
Which I know is an attribute, so run this and you can see here are all the titles, so that's cool, so basically, that's the title that we're going to take for each of these, so each element in this list and you could also get something like the link if you want, so you can say print data link and now we have a series of questions and answers and let's just print a space to make it a little bit prettier, there we go, so here is the question and then here is the link that we can control, click to open and that's it, so maybe you could create some kind of app that helps you find a good stack of Overflow questions to try to answer before anyone else to so I can be a little bit more specific, you know you can go in here and check the response count.
The response count is like two for this one so maybe you just want to print the ones where the response count is zero so we can say if data and then ask for the response count is zero so we want to print the data and just to show them that it's working, we'll say otherwise printing was skipped and we'll also print another space at the end. I'll just delete it. from the loop there we go so this is our setup now and we can run this and you can see skipped and then we have a question and then skipped and then a question another question and it seems to be working this is how you consume an API now I want to talk about how can we create our own API so what I want to do is exit this file here and we are in an API folder and open a terminal that I have here and we can get the path so just do Make sure you are in that directory and let's go You're going to run a script in the terminal here, so the first thing is to create a virtual environment.
To do this, you say Python 3 script M virtual environment venv. and then give it a name like venv and that will create a new folder here for your virtual directory, which I talked about in my previous video where we deployed a Python web application, but this is basically to isolate the dependencies for this. specific application now what we can do is activate it by saying source and then traveling to it so that it is in the folder uh Dov EnV bin activate this is how you activate a virtual environment, then we can install our dependencies pip 3 install flask and We are also going to install p 3 install flask SQL Alchemy and that will be used to work with the database, so this is an example of an OM which I think is an object relational mapper and we can generate all our dependencies in one file. if we want, we can say pip 3 freeze and put this in requirements.txt, so every time you add a new dependency you'll want to make it so that all of our requirements are in this text file, so whoever uses this project in the future can get the dependencies, then we can just create our file, so we'll say and that will create this file, which is where we're going to do our coding, so to get started just create a basic flask application, so we'll say from flask import flask and then we'll say the app is flask uncore uncore name uncore uncore right there, that's righthow we configure flask and what we can do now is make a simple route basically an endpoint saying application. route and give it a route and then we can define a method that we want to use when someone visits this route, so all we're going to do for now is just return a string.
Hey, okay, so let's make sure this is working. what we can do is run our application to do this, we are going to create some environment variables real quick, so the flask application is and the flask environment is development and you will need to do this every time you open configure your terminal, like this that if you exit your terminal with an just open that, okay, i already made a stupid mistake, we didn't put here right after the uh method, so refresh now, this should be in caps, here we go, everything should be fine, we have our app up and running and ready to go .
So the first thing I want to do is make a get request, so we're going to create an application to store drinks, so we'll say application. defeat and the path for this is going to be just drink and we'll say definitely get drinks and it'll return something so we'll just say drinks for now, but that's not a thing so what we need to do is put some code in here to make it work, yeah you want to try it right now, what you could do is return a dictionary and let's say we have drinks and we'll put in drinks data right now so you should be able to save that and it will update automatically and then when we refresh our page to what we actually go to drink/drink and we get some data in Json so it's working now we only have to worry about connecting to a database so to do this with the object relational mapper we basically define all the things. we want to store in our database as models, so we're going to create a class so we can do that up here and all we're going to do is say class drink and for SQL Alchemy we just say DB model, so it's going to inherit from this one, so it will already have several functionalities built in and this is how we can use it with SQL uh Alchemy, so we define the columns and we are going to put three of them here, so You are going to have an ID and the way to create a column is by saying db. column and passing any attribute of it to be an integer and then we set it as the primary key saying primary key is true.
Next we will have a name which is DB column and this is going to be a string so to do that say DB string and you can pass a maximum length we will just say 80 and another attribute here is unique true if you want the name to be unique and you can also say that nullable is false. so you can't have any null values, so the last one we'll use here is description and we'll say db. column and this will also be a string and we will only give it 120 characters. However, I'm not going to put any other attributes in the description.
Now we want to override another method here and it's called rep reer. R method for rendering uh probably and for this we pass self which refers to the object so we can take the attributes of the object by saying self do something so we'll just return a string and this will be called every time we try to print the drink in the list so you'll soon see what I mean, so we'll say F to create a parameterized string and we can say self. name and then maybe you know a script and then me. description alright so this is how we create a model and what we are going to do is set up our database with a table for drinks and to do that the easiest way is to use the


terminal so what?
What I'm going to do is exit our C server control and then I'm going to say Python 3 to open interactive mode in Python and what you can do is say from the application import. DB, so what we do is import our database from the application import database; However, all we did was define the model we need to make this connection to the database in our code first before we can reference it, so to do that let's go up here and say from flat ask SQL Alchemy import SQL Alchemy so everything is lowercase and then sqla a is uppercase here, then to get that DB variable we just say DB is an instance of SQL Alchemy and we're going to pass our application to flask So that's the SQL Alchemy configuration.
The last thing we need to do is configure our database so we can connect to it, so we say app.config and we're going to pass a string here to the SQL Alchemy database. and it is not necessary to memorize all this; you can find it in the flask documentation quite simply and then we'll say SQL light and then three sles data. DB, this is how we say we're going to create a lightweight SQL database called data. DB in the same directory and then I think this DB variable will actually come after this configuration, so we'll just paste it in there and there we go, so we should be able to exit Python interactive mode, restart it and now what should we do? what we can do is say from the application that it refers to our application because we call it import DB great, we don't get any errors, we get this warning here, warning us about significant overhead and you can change some settings, but you don't really have You have to worry about that, so to create our tables we say db. create everything press enter and you can see that it actually creates the database as well since we didn't have that now because this is what is known as an object relational mapper the way we work with our relational database is with objects so if we want to create for a new drink we create a drink object and we can assign it to a variable called drink and the way we pass data to create a drink is by making named parameters so we can say the name is soda grape and then a description with a comma, eh.
I don't know, it tastes like grapes, maybe it seems quite appropriate, that's how we create. Oh and you see, when we make this name, the drink is not defined, so every time we want to use something from our file we have to import it, so this drink class needs imported also from the import drink application and now we should be able use the up arrow key to bring back our previous code, press Enter and now we have a drink object and you can see it typing drink and it says grape soda flavor. like grapes and that's where this method comes in re, basically the name script in the description now to add it to our table we say db. session. add drink then db. session. commit and that is an important step, yes we want db. session.
Don't compromise with the doctor, so db. committed session you should now be able to get all of our drinks by saying drink. consultation. Everyone presses Enter and you can see that now it's a list with just one grape soda item, so if you want to add another drink, what we can do is just do that process again, so we'll do this one online, so we'll say db. session. add and we will create a drink, the name we will call it cherry and we will give it this description and this cherry drink that I drink tastes exactly like this cherry ice cream that I used to eat, so it tastes like that ice. cream, everything good, perfect, press enter and then db. session.
Commit now, let's review the drink. consultation. everyone hit enter and now you can see we have multiple items we have grape and cherry soda so we can exit and go back to the flask let's say run the flask so we have our server up and running but now we need to create a way to get those drinks. Within this route right here, it's going to be pretty similar to what we did before if I can scroll up a little bit, we said drinks. consultation. Everything, so let's go ahead and do that in this code here we'll say drinks equals drink. check out Doall and unfortunately it's not something we can put here saying drinks, that would be nice, but I'll show you what happens, so what we can do is refresh this page and it says drink type object is. non-serializable Json uh, I've been working on trying to figure out how to make this as easy as possible uh, I haven't really come up with too many great ideas, um, but this is basically what I decided: Let's just create a list. uh, so we're basically going to write what the Json should look like, so we're going to say the output is just an empty list.
From here what we're going to do is iterate through the drinks, so we're going to say drink in drinks and we're going to describe what it's going to look like within a dictionary, so the end goal is to have a list of dictionaries, so We'll simply say that beverage data is a dictionary of where the name comes from. to drink. The name and description come from drink. description like that and then once we have that dictionary, all we have to do is say output. add and pass drinks data to add that dictionary to this list here and it will do it twice, once for each drink that is currently in the database and then what it will do is it will just say drinks. and pass the output in place, so now we have a nice list of dictionaries and that should be serializable, so we can do an update here and there, we have an external object and then key-value pairs a key with the pair of to list of dictionaries, so a very similar format to what the API looked like for Stack Overflow now what I want to do is create the ability to pass an ID here, so let's say we passed one and we were able to get the first shot, like this Let's go ahead and build that functionality.
I'm going to clean up my screen a little bit here because we're not going to need this terminal right now. I'm just going to shrink it down and go to hide our different files on the side, so what we do now is create a new path for the application. path and this will also be two drinks and then a forward slash and you can use less than and greater than signs to define a parameter here, so it will be replaced with a specific number. Now a lot of people are debating whether this should be plural or singular since we're having a drink, I decided to make it plural and I'll show you why, so I found this Stack Overflow Question singular or plural name of the resource while I was creating it and this person talks here and you know there's a There are a ton of different ideas here about the best way to do it and as I was reading this I looked up the web address and saw plural questions with the ID so I said H.
I think I'm sold. I'm just going to keep it plural, that's why I decided, so let's go back to our code here and we're going to create a new method, so we'll say def get drink and this one will have a parameter ID, so instead of having a drinks variable, we're going to have a drink variable and now we're just going to get one drink, so to do that we can say drink. consultation. getet or 404 is kind of a tongue twister and from here we can pass the ID which will give us a drink object and then we can return it and build a quick dictionary, then we will say jsonify and go To build the Json, the key will be the name which will come from the drink. name and then the second key will be the description and that will come from the drink. description to make dictionaries instantly serializable, you actually don't even have to call jsonify here, so you can return the dictionary like this, but sometimes you'll need to call jsonify if you're not working with the dictionary, so I think it should work. let's save and go back to our web page and you can see we get a drink back and we can try again with id2, we get that second drink and then we can try ID3 and they can't find us because there's no third drink in Now I wanted to show you very quickly.
If you go back to id2 here, you can right-click inspect and get a little bit more information about this request, so hit Network and refresh a little bit. You can see this request here and it is a get request. the answer was 200, okay and you can see the content type is the Jon app, it's a little different than if you're on a web page. What we can do is inspect here and go to the network, get that first request here and that's it. 200 okay, also a get, but this time the continuous content type is text/html, so yeah, I just want to mention that to you, let's go ahead and come back and now I want to talk a little bit about adding a new drink, so let's go .
Go ahead and create another route here, we'll say application. route, the route will be drinks and you can actually have another parameter here which is methods and say post which is good if you're defining a post request you can put any of the methods here you could put get or delete or whatever and you can actually condition to be able to check what method the person is requesting so you can, in theory, define all of this in one method and then you know, check if they're trying to get all the drinks or add all the drinks, however, I like to have them a little bit more separated, so I put them in separate methods here and justwe'll create a separate method so add drink now, this one will be a little more complicated because we actually have to get the data from the request so when the person calls this they will pass in the structure of what a drink should look like like this that we will obtain this by creating a new drink and where are the attributes for the drink will come in handy the name will come from request.
Json, this is how you can access the Json data they sent through the request. Json and we'll look up the name and do the exact same thing for the description and that will come out of the request. Description Json here we go, now we can add them by saying db. session. add and pour the drink db. session. commit, so what can we do, we can return whatever we want, we could say added success


y um or you could do the id, so if you want to return the id, it will look like returning ID drink colon. ID, so that ID attribute will be added to this drink object, so it's quite convenient, save it and now let's try it, so we'll go all the way here, but we can't really pass the data so that our requests can be posted easily.
So this is where Postman comes in handy. From here what we can do is define what a drink should look like in Json within the body of our request and then just set it to publish and we'll also want to update. our web address so you can copy it from here, paste it there and what we're going to do is give this a name, let's zoom in a little bit, what's the name going to be, uh, we'll call it. Cola and then the description is delicious, so just check that everything is okay. We have the raw Json post from the body and the correct web address hit submit.
We get an error on line 42. Let's get back to our code very quickly. Looks like we might have some kind. of the formatting issue here unexpected name request for requests to work. I think we need to do another import from flask, so we'll say request there and hopefully that will work. I also get another warning down here. I think we need to uh take this and move it up one line, it wasn't causing any problems, but best practices are fine, let's go back down in our code and it looks like we don't have any more warnings, so we'll go back to postman and hit submit and It looks like we are making good progress so the problem here is that we are not using the correct web address so we need to go to drinks without slash and press send so it was a good thing that it worked and in this situation it returns the id of three , so now we can get all the drinks, we can still keep this body here, we don't have to delete that hit submission and you can see all the drinks there or you can pass the ID of three hits submissions and you can get the last thing I want What we talked about is how to delete a drink, so let's go ahead and go back to our code and create a new route that we'll call application. route, the path will be drinks and we are going to take the ID so that we know which drink to delete and then for the methods, we are going to say delete, there we go and we will say definitely delete drink, so for this delete a drink we are going to say DB . session. remove and pass the structure of the drink.
The best way to get this drink structure. I thought it would just be getting it from the database, so we'll create a drink variable and say drink. consultation. getet and pass the ID, so I'll take the ID as a parameter of this web address and then after removing it, we say db. session. get engaged and was having a bit of a problem now overall. I haven't done a lot of error checking and different edge cases, however I was getting that I would delete a drink and it wouldn't exist and that would become There is a problem so I decided to add a little more casing here so if there is no drink, What we're going to do is we're just going to go back and I'm just going to say something like error not found or something like that. or you could return a 404 just showing a few different options here and you know, if that doesn't happen, then I'll assume everything is fine and we'll just come back, say message and no I don't know, we'll just say something like Yeah, here we go, okay, let's try this .
What we are going to do is eliminate and we don't have to do anything with the body. just to show you, I'm going to remove it just to make sure it's clear, we pass the ID here, however, hit submit and it says, if we hit submit again, it says error not found, that's how you do the most basic API. calls we have the ability to get all drinks, a specific drink, add a drink or delete a drink. I didn't really do anything to update a drink, but a drink is so simple that I feel like you can just delete a drink and then read it. or if you want to practice, you can try out some requests here, but that's all I have for you on the basics of creating an API in Python.
There's a lot more, but I hope that gets you started.

If you have any copyright issue, please Contact