MongoDB with Async Python Transcripts
Chapter: FastAPI Example
Lecture: Sometimes API focused models are required

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, that was really easy. Let's just do the same over here. Okay, so how'd this go?
0:06 So we've got this count coming in. Maybe we'll say count equals max of one and count just
0:15 to make sure that it's a positive number, right? If they pass like negative one or something
0:19 silly in, we won't let them pass that over or zero. So we're going to get our sum number
0:27 And we have our package service, we've already written all this code recently updated, and we'll pass in the count. We have to await await this.
0:39 And what do we get back here? Remember, this is going to give us not how many packages were updated, but actually, the packages themselves.
0:48 So in a sense, this should come back as a list of package, which we didn't say, did
0:53 we. But that's what we're getting as we call to list on our packages. Okay, great. So we can return
1:03 what do we want to return here, we could return just an array, not sure that's gonna work out.
1:08 So we can say packages, we could pass back the count. Yeah. And maybe we want to return packages.
1:21 store this in a variable, then we return that. Okay, we don't yet have any documentation or
1:27 anything like that. But let's see if this will work packages recent. Okay.
1:30 Well, there it is. Look at this. It totally worked. How big of a response is that? 265
1:45 kilobytes. What if we asked for 50? The response time starting to get up there is two and a half
1:54 megs. Maybe, maybe we don't want to return all the details about it now. Maybe the goal is just to say,
2:02 I just really want to tell you the name of the most recent one. And if you really like, for
2:08 example, let's see, what is this? Oh, it's Beanie. So what if the goal was like, okay, well, if I see
2:14 beaning that list and I can just go into detail slash beanie and get the details if I want, right.
2:22 So maybe the last updated date would be interesting and the name as a response there. So well, then what do we do? Do we go into a list comprehension?
2:32 I'm going to try that. Let's try that for P and package. And then we're going to return what name?
2:41 dot ID, comma, date are updated. Let's just say that you don't last updated. How's that
2:52 going to work? Oh, not with the details one, but this one. Okay, that's better. The size
3:00 is back to 2k, we kind of still get the same information. All right, I'm liking this. I'm
3:06 likeness. What about over here? Am I still liking it? I'm gonna tell you no. Because again, what I
3:14 get back is a string. That's not really what I was hoping for. So how do we approach this?
3:19 So here's a little bit of maybe a cold splash of water, not terribly bad, but maybe somewhat.
3:27 We've got these pydantic models in the database, and we're modeling them. And they're, let's just
3:32 assume they're perfect, we love them. And sometimes that is the thing you want to just
3:37 send back you like, well, here's everything we have doesn't have any personal information
3:41 we need to redact like a password or anything, we can just return it back, it's fine. But
3:47 here, we don't want to return back just a list of packages or even a straight array
3:52 of them, we want this subset of data with a name and an updated. So in order to do that,
3:58 Even though what we get back is somehow involving Pydantic, we need to create API focused Pydantic
4:05 models specifically for both the return data and the documentation and schema generation.
4:12 So without this already being called models, I might call it models. Where will we put it?
4:18 I thought about calling this API models and putting those models in there. I don't want to mix them.
4:23 When I look here, I want to see only database stuff. That's my opinion.
4:27 So I could have API models and models or how about API and just in here, we just make a thing called models. Okay. And what is this is a recent.
4:37 So call it recent package model. Recent packages, how do we make a Pydantic model derives from base model.
4:53 Over here, we were trying to return something like that's well have name, which is a string
4:57 not optional, because that's the primary key and updated. The date time, date time, date time, there we go. And that's it. So
5:06 let's go. Let's go and say we want to take the same data here. And actually, I want to retract right, but there, let's, let's
5:13 also include the count and packages over here. So let's, let's make this a recent package. I'll call it recent
5:22 packages model. So this will have a count which is an int, we're echoing that back and
5:30 this will be packages. This would be a list of recent package. There we go. Leverage the
5:37 embeddability, the embedded nature of that. So we'll say model equals one of them, right,
5:43 one of these characters. And what goes in here, the count equals count. And then packages
5:50 equals let's call this package models. We've got to define that and we already have this
6:00 list comprehension here, right? I'll comment this out just for a moment. So I can commit
6:05 it you can see it in source control. If you go into history, right? We're going to say
6:09 it's equal to this but instead of creating a dictionary, we'll create a package model. We call it recent package, sorry. Name equals p.id.
6:26 Updated equals. Like this. We'll use a little list comprehension to generate that list and project down into this much smaller set here.
6:42 And then for our recent package models, we'll just return model.
6:45 And we'll round it out by making this part of the documentation by saying response model equals that. Let's see if this hangs together.
6:56 Here this should look the same. It does. Whoo, that's good. But this, this should look different. Recent packages and look at that.
7:07 Here's the schema that we expect to get back is application slash JSON. Super cool.
7:13 Again, really easy to implement using the fact that we already wrote the code to go and get the recently updated packages.
7:21 But in order to transform that into something that makes sense for this API, even though
7:27 what we got back is a list of Pydantic models, effectively, that's not enough, we don't want
7:33 to return that all we want to give them a different view, this is going to turn out to be really important when we get to the high performance section.
7:42 Because if we had returned, you know, that 2.5 megs of data, that is a lot of data coming out of Mongo.
7:49 That's a lot of deserialization into Pydantic models in memory on the web server side.
7:55 And then serialization back into JSON and then shipping that over the network. That turns out to not be awesome.
8:01 Remember this is 100 times smaller, maybe more, and went down to 2.7 kilobytes. I don't remember if that was compared to the 5 or the 50 or more.
8:12 So anyway, it got much, much smaller, which is excellent. So there's this little bit of extra work that we had to do to transform our data.
8:19 And that's just the way it is. You don't always want to just turn your database into a thing that users request over the internet, right?
8:27 Maybe you do, but most of the time you want to have more structure and more control over it. And here's one example of doing that.
8:33 We'll see the same thing with the stats in just a moment.


Talk Python's Mastodon Michael Kennedy's Mastodon