Building An App From Scratch: Building a REST API with Python | #4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so welcome back to another stream and another video what I've done now is I've created just a little bit of a notes for my intro because I just wanted to mention something a little bit quickly before you get started because today what we're gonna do is we're gonna try to build out the rest API for this app that we've been building the last couple of days the last couple of videos and what I wanted to mention before we start it's just that I'm not an expert at this and it's like building REST API so something that it's completely new to me so I've built somewhat of a REST API like two times before I think using flask and Python which is what we're going to use today as well for this REST API and also like kind of the purpose of this series or this stream these stream sessions is to show is for me to basically be able to build this app which I would have been building anyway so I think that I since I would have been building this app either way it makes sense to just share it with you guys live so that you can see kind of the entire process of me building that building this out and just me making mistakes and trying to figure out how to solve these different things so let's let's take a look again at the notes so what I try to do hard with my normal videos that I do on YouTube is I really try to show that I like exactly how it is how good or bad I am at coding but it's sometimes one of those things where if a video turns out to be like 30 minutes long and 15 of those minutes is being spent just on the same problem over and over trying to solve it I might have to cut that out just because a 15 minute video 10 to do a lot better than a 30-minute video so I try to keep them short so but I try to keep in some errors that I do just to kind of show a realistic picture of what it looks like when I code and what I've been getting the impression of is that some people seem to think that I'm like some God when it comes to programming and coding so and that's not really the case at all and that's kind of why I feel like this series is a good way to really show exactly how it is and exactly what it looks like I don't cut anything out and it's just what I'm doing here is exactly how good or bad I am at coding which i think is really good and I think I haven't really seen anyone else do do an entire live build of an app from zero to a complete finished app that's published so that's also one of the ideas with us and then also this stream kind of builds some interest hopefully in the app that I'm building so that when I do release it maybe some people some of you guys will want to download it so that's kind of the idea with it anyway and I also consider these streams not just pure tutorials since I'm also learning these things myself as I am building it out I consider them kind of a mix between tutorials and also kind of just hangouts where I'm coding maybe you're following along and coding along with me or maybe you're building something else out maybe you're just having this on in the background so it's not as formal it's like a tutorial since I'm actually like exploring some of these things myself in the stream so with that said a little bit of a long intro but now we're gonna get into actually building out the rest API so what I've done is I've created a I think I mentioned this last time in the last stream or video and that is that I've created kind of an outline for what I think we're gonna need in all of the classes and then so this is essentially what we're going to have to build out today we're going to try to get the database set up and then also get into get the flask app up and running and then we'll go through and try to create all the classes that we're going to need and then insert that into the table and kind of get the very very basic functionality of the API or database up and working so we're going to start with just creating the database and we're gonna use Postgres so there are different types of databases you can use sequel is like the main one as far as i'm so I'm gonna use Postgres which is called like post grace QL which is based on the SQL language so what I'm gonna do is I'm gonna first start by just checking how to actually create the database because I don't remember that command because I think you can just start it straight from the terminal Oh alright okay I think this is the one what we'll call it is productivity so this command should create a new database for us called in the database will be called productivity app and I also think that it will open up the database for us so that we'll get actually into it I'm not quite sure I think ish yeah no invalid argument psql okay I had copied the wrong one change the use all right maybe we'll just open up Postgres and then we'll go into it and then we'll create the database inside we'll just make sure that we actually have it okay so we have it running okay so now we're hitting the database now we just need to create the database all right create the base SQL alright three thought abase and then now we can name it so productivity all right so this should then create our database for us which I think it did now because oh wait productivity I think you always have to end it with something like that syntax error near create this is something that I get sometimes which I'm not sure why but sometimes when you've pasted or when I paste it tends to not work even though it's like spelled correctly I'm going to try it with the lowercase which I should work with lowercase as well I think product two would be oh Oh create database all right why did that not work all right I think at Nino's seven if that's what you say I think that's yeah all right so we created the database how that works see everything is gonna be a little bit of a struggle right now because I'm so not used to this so therefore the kind of spiel at the beginning all right so now we've got our database created that was essentially all that we wanted to do now we can get into the more of the flash stuff all right so what what I did is essentially I just googled REST API with Python and flask and we're gonna follow this a little bit quickly through and just get it set up all right we're gonna open up a new terminal window and then we're going to go into our project what was this one called again it was cold what do we call it to do I'm not yeah I think we called it to do app to do yeah all right so now we create a new directory called back end which is going to be where we keep all of the back-end stuff and Duke it back ok now we have that now we go into back-end and then we open this up in Visual Studio code ok there we go you're gonna have this here for now what I think we need to do is we need to create a virtual environment as well so we'll create a virtual environment call that n enter and then that should set that up all right so now we have the virtual environment now let's go into it source end and then what is it being activate and then now we're in our virtual environment and that is just so that we can install some of the things a little bit more specifically to this project okay now we'll follow this major project it uh make it resources okay I'm not sure that we're gonna need the hello pie and the following two apartments don't text okay so this is what they outlined that we're going to need we're going to need all of these different files essentially which is going to be app top pie migrate by model dot pie and requirements text and then round up pie I think that this part of it is just for like I think it's gonna be a basic website or something like that that they've got so I'm not sure that we're gonna need that all right so we're gonna just go ahead and add all of those files yep we're just gonna add the files all right there we go let's add a file doc pie add another one what is this one my great doc I think this is the one that migrates so you basically create all of the stuff that's gonna get put into the database within the Python or the flask app and then what you do is you I think it run migrate PI you do it with some in some special way and basically that LEM insert all of the tables and all the stuff into the database for us so we don't have to do that manually and yeah I think that's what migrated up I will do and then we have muddled up I but we're gonna call that models pie and then we're gonna have requirements record your stop T C alright thought that okay and then we'll just add all of this stuff into the requirements text like they said save that and okay they're just explaining all the packages which we don't really care about okay and then pip install requirements okay that'll then go ahead and install everything for us okay then we should add config dot PI as well and then we'll add whatever he said we should add into that one I'm not sure how interesting this will be this is just going to essentially be me setting everything up which isn't super interesting I don't think but this is also like I said I wanted to build everything out live so we're essentially building everything out live configure PI import on and on okay copy this and add this to config top.i we're going to rename this productivity okay so we're essentially before it's that database or something like that we rename it to the database name that we gave our SQL database not sure if we need to actually replace anything here but we'll see what happens when we test run it okay create the app dot PI entry points after PI API from resources to us hello okay we're we're just gonna see what they add to a low and then depending on what they add hello to PI class hello resource def get self return message hello world okay in hello class we define the function get this means that any gift requests on the hello endpoint will be hitting this function okay I think that's actually a good thing to have because from what I understand it it's gonna be essentially that we add endpoints here and that will then call all of these or this file so essentially here we can add all of the gifts per get put post delete all those requests there are different types of requests that you can have when you make an API request so essentially it's kind of good to have them all structured in a nice way so we're gonna actually create that folder we're gonna call it resources and then within these folders is where we're gonna have all of the separate requests so essentially for our app for the purposes of our app let's say that we want to add a new task okay we can do a couple things we can add a new task or a new project let's say so essentially then we would have one of these classes here and one of these files for adding a new task and then a separate file for adding a new project so in that way we're then separating them so that it doesn't become a huge long file of all the different endpoints that we have and all the different get and put requests I think it'll make more sense once we if you don't understand this right now I think it'll make more sense once we actually get it a little bit up and running and we can test it so we have resources right now what we're going to do inside resources we're going to add tasks pie and this essentially I think we're going to add all of the different and points based on or the files that we're gonna add here are gonna be based on all of the classes that we decided we're gonna have so we're gonna have project support file and user because what you can do from the app is you can either get a task you can get a project you can get a support file you can get a user and as you say as you see what they said here here in the hello class we defined function gift this means that any gift requests on the hello endpoint will be hitting this function so anytime we want to get something that is related to the hello class we will we will essentially be calling this file all of this class so therefore for all of these things we can do a get request for all of these different things we can get the tasks at the photos get the support I get the user so therefore it makes sense to have separate files for all of those and create different classes for each of them alright new file project top PI user the top PI and support file as well all right so now we have all of that what we're going to start with I think we're going to start with the user because it's a little bit simpler okay import this into our user class and what we're gonna do before we continue we're gonna just change this stuff out for user or something user and from and as you can see up here it says from resources dot hello import hello what we can do and then is we can call from resources are you sir wait okay so lower case because we named it lower case and then import for you sir all right so now we can then create our class for the user class user resource okay and then we can define all of the different requests get you can also have a post host and yeah there are different several different types that we could use but we're probably where you're just going to start with this we're going to make it as simple as possible to just get it up so if you need a post method you'll do something like post okay finally add the following to run pi copy this add it to what they write we can create the run to PI alright let's create that file as well run PI and now that is within our resources which we don't want okay round up I paste all of this let's see okay I think that is all that we need from your time we'll make sure you are on the root folder when you run this command okay so we'll do that where do you run Python roundup PI file run apply line 17 I prayed missing in this top five package or module path or finance system module name taking all right what did we miss let's take a look at the after pie n and the config file based on import error no module named config no module man okay thank you guys for keeping me or chick helping me out so I basically renamed or named it wrong which is typical isn't it typical of me at least alright so that should now work come on get it together okay no module need research to stop you sir okay again named it wrong okay resources resources okay let's just try this I'm not sure that that will change anything but in force you said oh no Mancha yes there is class hello resource itself yes everything resource okay I see after PI resources at the high resources import okay resources input you sir okay from resources or you know module named resources doc you sir alright not sure why this is not working but let's instead let's copy all of this paste it here you sir you sir you sir let's just leave that as that okay so now we've at least on that let's see run up right now okay copy all of that add it to the user and just change the name here okay so it still doesn't work at least now I've copied everything so now it should be it shouldn't be at least spelling errors unless there's spirit of spelling errors in the what he's put out here which I mean there might be but the question is why does this not work resources dot you sir okay let's just copy that and let's really name this file here no mojo made resources that you sir okay no more named resources after I'll come oh okay maybe if we go further down because sometimes i falled one of these ones before and what I noticed was that I didn't get it to work either the first time and then I scrolled down into this tutorial and like way way down I rely saw that okay there was a import that they had done that I hadn't done because they didn't show that up top so therefore it didn't work but at the bottom they had it and they didn't really say that that was something that we needed to add so sometimes they could be that they've just missed some import but this seems like another problem because it's like it doesn't real it doesn't see that we have this file or this folder here which I'm not sure why because it should say that shouldn't it resources import you sir okay what we're going to do import files from folder alright this is one of those tiny tiny little issues that's like gonna take off the whole take up a lot of time really okay after pie I really like this is where we're running into problems we're not able to import it we're not able to see this folder resources which we should be able to see yeah no module named resources let's google it all right let's see run - hi right bass do the thing is like if we just do this if we pull this thing out here I think it'll work yeah so now now it gives another error but it doesn't but it doesn't give so now apparently I would assume that it finds this file so the only issue really that we have is that it doesn't find it within the folder move okay let's try this resource okay now it doesn't find it again what we're gonna do we're gonna move that out then of that folder just for now because that at least that way we can keep going and work on the other errors that come up all right next error F equals creative input error normal no module named model okay I know what that one should be simple where are we model here so we just do model model still not even all right cannot import okay but that is because we don't have anything there okay we can just comment that out because that is going to be for when we actually have the dollar base okay so now we get the server up and running now we need to figure out how to actually have this file in that folder because really we want to have it in that folder because otherwise it's gonna get super messy having just everything out here essentially what you want to do when you're building anything is just like almost have more folders than you think you need so yeah that's what I would suggest okay figure out this problem now set edit are you in towards our standard the moment on machine Python is empty before we set the Python back quickly did I fail with tray space so export Python path all right not sure I'm not actually sure what I'm doing right now I haven't done this before so race or thirst uh you said but I'm thinking that what I'm doing is I'm setting like the base directory that it's going to look through not sure that the code is going to be needed but we're going to try to just run it now okay no module named of resources that PI what we're going to do export alright still can't find that file which is a bit strange running useful function okay okay why does this not work users all right how high is is not working all right I'm gonna listen to you Mary Jean daya or whatever your name is in the chat create a new empty file called init PI all right we're gonna do that I will try anything at this point in its PI Oh inside resources yeah we got it all right what are we doing now okay it worked thank you for that that was super helpful and thank you thank you for that there we go we have that running okay let's let's just forget that ever happened and now we're gonna pretend like I'm an awesome coder again and we're gonna move on to the next thing okay so we got that working that's out of the way done dusted let's go if we go to our localhost now we could do this Oh essentially what we're doing is we're accessing our endpoint is a capital letters even yep is capital letters but I'm not found don't know your elms or oh okay wait okay all right maybe we need the localhost thing that part of it let's copy this and that here I don't think we need that but okay nope this is hello okay let's just go to the tutorial see what they say Oh API okay that's why all right so there we go we get the message here hello world finally we get some actually actual output from this thing now what we can do is we can change that we can say hello cow something like that and if we go back here now and enter it again it says hello Cal instead that's working now great oh my god alright next thing is going to be I'm assuming creating the models and actually adding it to the database which is what we're we got here so okay baby we're going to import all of this into our models but PI right save it and then we'll take a look at what they've got here okay Tel Aviv what I'm looking for right now is just to see where they create all the classes because I have already done this before so what I'm looking for is just to see whether they do anything different but it doesn't seem like they go through this that much more common scheme of mine equals true okay so what I've done is I've prepared or for the other app that I've done which is this one I've already created different things for a user No okay so we import that Domino's okay maybe we actually need to follow this just to see that we're actually doing it the right way okay category self-named serialize okay so I think we're going to copy this one or maybe let's copy this one because that has a little bit more stuff in it okay and then we'll go in to where were we this one all right and what we'll do here is we'll paste this thing in so this is a class called comment and what we wanted okay yeah so that looks pretty much exactly like this one actually so we can just copy this and this is for another app that I created called which was the startup app that I'm still working on but it's like I haven't worked on it for a while so I'll give you an update once I get back into that alright so here we go user database model users is what that so we defined the table name so that is what if we go into our Postgres database then we're gonna have different names for all the different tables and essentially this is going to be named users and that's going to contain all of the users for our app users for our app and then we have table orgs ID user name not sure what that was actually let's take a look all right anyway and then unique constraint ID username then we have the ID of the user which is a string and that's a primary key all these things is not super important right now they were a little bit advanced and I don't know I don't know them enough to actually be able to explain them in a way that would make sense I don't think but I think that a primary key is like essentially when you want to get something from the database you need to provide something so that you can then tell the database like what are you trying to get so let's say that we when you want to get something from the daughter book database you type in so Lake Soleil and then you can type in some sort of like condition where you want to select something so let's say that you want to select a user where the username is equal to Cal or something like that then you can provide that here not sure let's just look that up quickly select from dog days is QL okay so what we could do for instance we could do select star from let's say tasks where user ID let's say is equal to or what do we say yeah so we're user ID is equal to 12 or something like that this I think this is the right syntax anyone in the chat that knows this better than we can feel free to just jump in if I'm doing it something wrong but essentially the details of exactly how you write this is not super important like you see I just google it and I can find out how to actually do it but what is important is like when we want to get something from the database we essentially so this select star selects everything so this then will select every task from our table called tasks where the use Rd is equal to 12 so or where you could also do where username is equal to Cal which is what I use that's the example just before so essentially what this will then do is they'll get all of Cal's tasks and it will display a yeah so we'll basically get all my tasks and this is essentially how you get stuff from the database and let's say that for for instance the reason that we want to use an ID for our user is because just using the name of the user is not unless you're having like a username which in a normal app would be actually unique what you want to do is you want to let me try to rephrase this so that I make sure that I actually explain it right for instance if we want to select every tasks from the users where the username is equal to Cal there might be several different people called Cal in our database so that means that we might actually get not just my tasks we might get Cal X and Cal Y and Cal said you know like several other users with the same name or with the same first-name we might get all their tests as well which is not what we want to do we want to get the tasks for that specific user which means that we need something called a primary key which needs to be unique in this case if we want to be able to actually get just the tasks referred to or the tasks that this specific user has so that's why you sometimes see like the different long-ass IDs for different users and things like that if for instance if you go to youtube and then you go to a video or something then you have something in the URL that would look something like this there will be some part of the URL that will look something like this and I'm guessing that's like the video ID or something and that needs to be unique because we can't have two videos with the same ID because then if we request that video we might get either of the videos because essentially two of them have the same name so therefore some of the case that we have or some of the fields or yeah fields in our database will need to be unique so that we can actually get so that we can get the specific ones that we want that was a long explanation but I think I think it kind of I hope you kind of understood what I meant all right so therefore we need an ID that has to have that has to be a primary key and it has to be unique and that means that we can then use this ID to get data that is related to one specific user and the user name is also a primary key because what I want in the in the app is for the user name to actually be unique because you I don't want you to be able to have three people with the exact same user name in some apps that doesn't really make matter so you can have this not be a primary key but then the first name last name that's just going to be a string password is just going to be a string email address is just going to be a string as well let's take a look at our class here unless my password email API key write something that I I'm not very familiar with either but is something that I think we should have as well it's called API key and I'm not sure how you would actually type this in but that is what we essentially are going to use so that we can access all of the different I guess the user ID can be an API key in a sense but I think it makes sense that too and what the API key does is essentially it authorizes the user for us so for instance if you have an app and you go into it and you don't have to login anymore that is because you've got an API he stored probably on your device I would guess again I'm very new to this I'm not sure exactly where it gets stored but I think it's stored locally on your device and as soon as you open up the app this API key gets sent to the database or to the API and then it basically authorizes you for yeah does the authorization for you so you don't have to again type in your email address and password or username password whatever it is and what this then will do is they will go to the database I think the way to do it in the right way is to kind of hash it which is a little bit I don't think I should explain that because it's like essentially what it does is it takes let's say that we have a long number let's open up sublime text and open up a new note all right so let's say that we have a long ID here what a hashing function will do is it'll essentially essentially go through this entire thing and then for each I'm not sure exactly there are different types of hashing functions so I think you can do more advanced less advanced but the base principle is essentially you'll take each letter or something like that and it'll multiply it or it'll do it'll do something to it which will make it into make it represented by another number so let's say that we put in a double you into a hashing function will then get out a number or another letter something which if we then put that number into the alga or the hashing function we will get out the right letter so that means that what we will store in the database will be a hashed API key so that's then going to be a lot of like different it's gonna be something different than the actual API key but when we put in the API key we all get back or okay trying to explain this in the right way so what we'll store in the database will be this thing and when we give the API key we will essentially perform the hashing function and then we'll check whether our results match they already saved hashed API key and if it does then we will authorize the user and then it will get put into or then we'll get all the data that we want for that user now I'm not sure exactly what type of hashing function we're going to use and I'm not sure that I can actually share the hashing function not I I know like you probably see now I know a little bit about how it works but I don't I'm not an expert like I've said and so therefore I don't know quite all the details of it but I think that's something that I I can probably share the build out of that hashing function and then I can probably change it to a different type of hashing function afterwards so that you get to see me building that out and then I will just change it so that all the user data is not just exposed or easily hackable but that could also be something that's automatically taken care of by the hashing function so much work all right okay so there we go we have this now which is ID API key all right okay so there we go we have that one up I think we've I'm not sure how long we've been actually streaming for right now but because I'm not sure I don't want to keep it too long and I feel like this one may not be the most entertaining one to watch I think the next one the next stream is probably going to be a little bit more entertaining because that's when we're gonna now I've just like this entire stream is pretty much gone towards figuring out how to set everything of this stuff up so that we have the server up and running and have the API somewhat working but next time I think we will next time is when we're actually going to get into like all this stuff creating the new database tables for the tasks and creating the tables for the projects and all the other classes that we've got here all right because I don't want to start anything as well at this time and then realize that it's going to take me an hour to finish so we'll see I'm just gonna try it okay we'll look at the tutorial again and see kind of where we were asked right here we have comment very fast if I know okay we've also defined columns indeed okay so click cut 56 so that primary key is unique by default I I've heard I would say that I knew this I'm not sure but I I knew this but it's like I think the reason that I set this to be unique I think there was a specific reason when I built this out and didn't the primary key didn't become unique or something I remember like having to actually look up how to make that unique so I'm not sure whether that is something that we actually need to add but I know I did add it for some reason but so I'm not sure I'm not sure that I want to remove it yet okay what we're going to do now is we're just gonna add this to github again so that we keep our project progress okay kids okay how do we add stuff to a git ignore file and folder to get ignore okay let's say we want to add a file call then we just need to write read meetup txt and then get to ignore file okay can we just create say getting nor fowl or let's try to create that I'm going to continue my closet or you okay so that is something that we can get we can do to touch touch dot get called thought cute and what do we do then save those files us all right I think what we need to do then is we need to do back and back end and then slash and then end right save it and then you get status get dogs okay let's see if it's been added morals probably should add this as well back and save it and then do get spiders okay undo github all right so now that's gone as well okay now we can add it back and integration started alright so now we've pushed that to github as well and now you got to see how to do the git ignore file you got to learn that with me if you didn't know it already as you see like most things are pretty simple once you just google them but the the difficult thing I think with programming is probably more it's not necessarily doing the things that you need to do it's more about like knowing what to Google so knowing what you'd need what you kind of what you need to be doing for instance I knew that there are you could just add something to get ignore file and that is kind of the more or the difficult thing about programming all right I think that's gonna be it for today and next time we're gonna go into actually making this making some endpoints here like tasks and basically that you then return a list of tasks for a different user we're gonna create an API key and then once we've done a few of the endpoints we're also going to finish the database or the tables for the SQL database so that we have all that set up and then we're gonna try to figure out a way to get all of the stuff from these endpoints we're going to try out some get requests some push real quick or some get requests some put requests to some post requests and delete requests and then probably not next time but the hopefully the time after that we're going to get actually the app to add stuff to the database and then delete stuff from the database and then get stuff from the database as well so that's going to be pretty exciting I hope you enjoyed this stream as well and I hope you enjoyed the video in case you're watching it on YouTube and yeah that's it for this one I'll see you in the next one
Info
Channel: Kalle Hallden
Views: 83,703
Rating: undefined out of 5
Keywords: building an app, hot to build an app, how to make an app, building an app from scratch, how to make an app for beginners, building an app from start to finish, building an app with python, how to make an app for android, how to make an app on iphone, how to make an app with flutter, flutter, python, flutter tutorial for beginners, flutter app example, flutter app from scratch, flutter app design, how to build an app for ios, rest api python, build rest api python
Id: skVMR0048NQ
Channel Id: undefined
Length: 82min 31sec (4951 seconds)
Published: Tue Oct 08 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.