MongoDB with Async Python Transcripts
Chapter: FastAPI Example
Lecture: Stats API

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Even the database, we didn't really have this model for stats, did we? We just said how many users are there, how many packages, how many releases.
0:09 So we're going to go just like we saw before, it's not 100% required, but it's preferable
0:16 that we have a Pydantic model that is the well defined schema and exchange for this API. So let's do the same over here.
0:25 have a file called stats model. And here I have a class which says stats model. And it has user count, which is an int, package count,
0:45 and release count. I think those were the three things that we were modeling. So instead of this, we'll say model equals one of those
0:54 import it. We'll come up with some variables, users, packages, and so on. So again, we can
1:04 go back to these services. So package service had a, not for users, packages. We had a package
1:14 count, recall. And we also had releases, which will be the release count. Then we had our
1:21 users, which was not the package service, but the user service. And user count. And what do we have
1:35 going on here? Int, int, int. So what is the problem? If we go look at this function, what
1:40 does it return? It returns an int. Why is there an error here? We'll hover over it. We expected
1:47 an int and we got a coroutine. This is something that's so easy to forget all the time. So
1:54 until we await these, they never even execute and they definitely are not going to be integers.
1:58 Now they are. All right. And so instead of returning this silly thing, we're going to
2:01 return model and then to wrap out the FastAPI integration response model, this as well.
2:09 it one more time. I guess we can see the documentation straight away. Stats. You can see exactly
2:20 what you expect, but let's also see it with real data. Perfect. Those are the numbers
2:27 we would have expected, right? We were last playing with adding new releases and we got
2:32 that from 804 up to 812 while we were fiddling around. But yeah, there it is. If we look
2:37 of the raw data exactly as like you would expect. Yeah. Super super cool. Let's just
2:43 do one more thing. Just look at the network here for a little bit of performance. 17 milliseconds.
2:52 Pretty stable. Really really excellent there. Okay. Guess we can do the same thing. Don't
2:59 know we did that here but we can. Let's do it again. Again pretty fast but this one 100
3:06 milliseconds, shouldn't be happy with this. We can do a lot better as we'll see. But let's
3:11 save all that for the performance side of things, okay? You know, that whole chapter.
3:16 All right, so we've got everything working pretty well, don't we? We've got our three
3:24 endpoints created. Two of them required specialized models, but one of them over here was able
3:30 to leverage the database model because that's exactly what we wanted to return to people. Excellent.


Talk Python's Mastodon Michael Kennedy's Mastodon