MongoDB with Async Python Transcripts
Chapter: FastAPI Example
Lecture: API Endpoints Ready

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now there are three APIs I want to get started with. /api/stats, the recent packages where you can pass over a count,
0:09 or get details about a particular package, like show me the details about /detail/beanie, and we'll get those back.
0:19 So let's start by structuring our FastAPI application, like a real professional application. It's easy and it's worth taking the time.
0:30 So many web demos are like, we don't want to trouble you to think about how you might
0:35 structure your apps are going to put everything and recommend everything just goes into this one main here. Don't do it, folks.
0:42 So what I'm going to do is I'm going to have a potentially a set of modules that handle the different API roles.
0:50 And in order to coordinate them, we're going to create a directory or I guess, since we
0:53 already in a package story, we could just create a package sub package here, we'll call
0:57 this API. And let's go and add a Python file for package API, we could have maybe one for stats,
1:07 we could have one for users, right, this is a real simple application. So we're kind of
1:12 pushing the pushing here, but still probably worth doing. So what we do is we come over here. and let's just say that these two are coming over.
1:24 So it doesn't make sense to say API equals fastAPI, which we do need, not fastAPI. There can only be one of those.
1:36 So we're going to need to come up with something that we can use as a decorator on these functions. But let's go ahead and write the functions first.
1:43 So how about we just call them, since it's in the package API already,
1:46 already, just call it recent and details. Now doing nothing, returning none, it's
1:51 not ideal, but that's a start. Remember that we want to use Beanie and so this
1:57 is going to have to be an async function. So async. Luckily, FastAPI knows about
2:02 these and it is really good at running them on fast workers just the way you would expect. Things like uvicorn using uvloop, a high-performance async
2:12 I/O loop based web server. So we can just make these async and they'll run asynchronously. But how do we do this? We can't use that one.
2:21 If we try to pass that over here, we're going to have to import them back here. We create the circular relationship. It's not loved.
2:28 So what we're going to do is we're going to create something called a router from fastapi.api router.
2:35 And that's kind of a deferred definition of all of the routes. So over here, we can say this one, to the same @API_ROUTER_APP, we say @router.get.
2:46 These are just gonna be get API endpoints. If you build RESTful services, you typically have modifying operations,
2:54 taking a post or a put, or possibly a delete, and then read-only operations being get, but really we're not modifying,
3:02 we're not like uploading new packages or something. So what do we wanna put here? We want to put, let's say, this is recent, so we'll put that.
3:11 And we want to put this count in here as an int. Okay, and let's just say something real quick, like return count. Count, we'll just echo it back,
3:30 just to make sure that everything is happening correctly in FastAPI. Again, down here, we'll say @router.
3:36 I get and this one is going to be the details of a package. Similarly like that. This will be name, which is a string.
3:47 All right, so those are the two API endpoints we want to put here. Maybe we'll make a stats API for the third one.
3:52 Let's just make sure this is working though. In order to, for FastAPI to find these routes, they're no longer included in a particular file.
4:05 So what we need to do is we want to go to the top and say, ""From API, import the package API.
4:17 And then down here, we're going to have a couple of steps. We're going to say, ""API.includeRouter,"" and we give it the packageAPI.router thing that
4:28 we defined at the top. And we're going to want something similar for the stats, so let's just do a quick copy
4:34 and call this stats API. And this is just going to be slash stats. Like this, just something
4:52 real simple. Again, we're gonna have to work that out. But up here, we'll have stats API.
4:58 down here I'm hitting command D or control D on other OS's to do this so to make it duplicate like that. That's how I'm doing that nice and quick.
5:08 Dot router. All right. With these we should be set. Let's go ahead and run it again. Looking good. Greetings from the world.
5:18 What if we say API slash stats. No packages. Look at that. Stats is one.
5:25 packages slash details, I have a FastAPI, the packages FastAPI, just to show you, I put it to it's a two. Put recent five counts five, six.
5:42 Notice also, there's no quote there. This is a number, even though technically, this comes in as a string to FastAPI.
5:50 we told FastAPI it's an integer, it converted it to an integer for us. FastAPI is super nice.
5:57 It has a lot of these really nice quality of life types of things that take a while to really discover, but they're awesome.
6:04 All right, so now we have the three API endpoints that we want, and we have them created as async functions.
6:11 And you notice we didn't have to change anything. Like when we ran uviacorn up here, we didn't have to change anything.
6:19 know, a FastAPI just looks at the view function says this one is synchronous, we'll run it directly. This one is asynchronous.
6:27 So we're going to run it on the asyncio event loop. There's one more thing I want to talk about really quick here is getting set up to run this.
6:39 Now, when we import this, if we run it, this bit of code is run, as well as this. It's great. That's how we're running it now.
6:52 But in production, this is sometimes not at all obvious. In production, this file is not being run.
6:59 What happens is we go to a real, quote, real web server like G-Unicorn or MicroWizGee or
7:05 something and then we just say go to the file called main, find the thing that's been configured
7:12 called API and you in your way more complex production level mode, run that server and do whatever you need to do with it.
7:21 What that means is this code doesn't run. So that code doesn't run well, and the routing doesn't get set up. So let's, let's fix that right now.
7:30 Let's go over here and we'll create a extract a function called and figure a routing. Okay, super simple, right?
7:41 We just took those lines and put them here, but what that lets us do is say else we need to run this other startup code.
7:50 It's a little bit tricky that only in production you would be getting 404 as route not found,
7:56 but in dev it would work fine because the main is the name of this module. A little bit tricky, but you want to make sure you run that in both cases.
8:06 With that, I think the skeleton of our API endpoints is up and running.


Talk Python's Mastodon Michael Kennedy's Mastodon