Rock Solid Python with Python Typing Transcripts
Chapter: Frameworks Built on Typing
Lecture: FastAPI, Beanie, and Pydantic MongoDB Example

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So you saw it in action. Let's just look at some of the code here. There's not too much going on for FastAPI. We basically have a package,
0:10 API section of routes and a stats over here. So these are over an API and we have a package and stats.
0:18 So right away, you can see the response model is a stats model, which is a Pydantic model with these three things. So that's pretty cool.
0:28 we've we've got this right slash API slash stats as you saw right here with data that
0:38 looks like this out of our database out of somewhere and we don't really see exactly
0:43 where it's from yet we will in a moment but not only does this stats model control what
0:48 data is sent back and what its format is right here we're setting up that model and returning
0:53 in it, but if we go over here and we say docs, check this out, go to the stats, it comes
1:05 over here, it says it's a void sort of thing, but here's what a response looks like.
1:12 It has a schema with these three things that implicitly are specified as numbers.
1:18 So that documentation is also driven by Python type hints and this model right there again, which is just a standard Pydantic model.
1:30 Okay, super, super cool. So that's the FastAPI layer. We're talking to beanie, which requires us to do which talks to Mongo, which requires
1:40 us to do async code. So this is an async API endpoint and a weight here. These are pretty simple, we'll look at them anyway.
1:49 So how do we go and say, well, returns an integer, obviously we say that. How do we do this? We call package.count.
1:56 That is, we look at what is package. Package is a beanie document with Python, type hints, and even overriding things like
2:05 how do we create the default values? Well, you call the function datetime.now when you need a value if it's not specified.
2:12 There's some optional ones and so on, right? So here's our beanie document, which is really a pedantic document. And then we just call count.
2:22 And we await that because it's async, talks to MongoDB, pulls that information back.
2:26 But down here a little bit more, we can do a more interesting query, you can say I want to get the most recently updated, maybe this should say int.
2:37 We want to get a count, which is an integer, because we specified by five there, it assumes
2:42 that's always an integer, but right by default, it's five, but we could pass in however many and we get a list of package.
2:49 So how do we do that we go over here, we go to the class, this is the beanie pedantic model.
2:55 And we can say find find all find one find many, there's a lot of different finds, but find all that means just give me everything.
3:02 But then let's sort them and do a limit on how many there are. So if we sort them, so the very newest is first and the second newest is second.
3:11 And then we say just show me five. Well that gives you the five newest ones because everything after that is older call to list.
3:20 We await that call, which does the async stuff, turns it into a list of package objects again Pydantic models. And those get used over here.
3:34 So for example, this is the packages recent or you saw get the count passed in by fast
3:42 API converts that to an int instead of a string, make sure it's there.
3:47 And we call this function get the packages, we come up with this transformation for the
3:53 return model, because we don't want to show all the data, it turns out there's way, way, way too much data.
3:59 If you pass all the releases, all that over, so we kind of transform it to this response
4:06 Pydantic model that we're going to return, set it up here so it drives the documentation
4:11 and also controls what can be returned by FastAPI itself and boom, off it goes.
4:17 So there it is, Pydantic, Beanie, FastAPI, all working together, all using this Python type information in many, many interesting ways.


Talk Python's Mastodon Michael Kennedy's Mastodon