Modern APIs with FastAPI and Python Transcripts
Chapter: Modern language foundations
Lecture: Modern language features
0:00 Let's just take a moment to reflect on the title of this class.
0:03 Modern API's with FastAPI. Well,
0:06 what is this "modern" about? Is
0:07 it just because FastAPI is new? No,
0:10 it's because FastAPI embraces many of the latest Python language features and external libraries,
0:17 leveraging those features, to make it easier and less error prone to write API's
0:22 and do that quickly, so fast. In this chapter, what we're gonna look at
0:27 are these language foundations. So in particular,
0:29 what are we gonna cover? We're gonna first start by talking about type hints or
0:33 type annotations. We already saw that if we specified a type which wasn't an
0:38 API method to be an integer that meant that one, it was required,
0:42 and two, if a value was passed,
0:44 it would be automatically converted over to an integer.
0:47 So it's not just that the type hints provide a clue about what type we expect,
0:51 FastAPI uses those to actually convert the types themselves.
0:55 So we're going to talk a little bit about this idea of type hints.
0:58 One of the super cool things about FastAPI is it supports the modern
1:02 async and await programming model that have made things like Node.js and Go
1:07 so incredibly fast. And, if you go look at the documentation,
1:11 the FastAPI folks claim that they're about as fast as Node.js and Go.
1:16 Do you know why? Because they're leveraging the same underlying feature.
1:19 So we're gonna look at the core idea behind async and await. Later,
1:23 we're gonna actually apply that to some of our view methods in our internal libraries we're
1:27 using, which would be cool.
1:28 But here, we're gonna focus on just getting that idea down nice and clear.
1:31 We're gonna talk about ASGI servers; Asynchronous Server Gateway Interface's. Normally, when we
1:37 work with Python Web apps like Flask and Django,
1:40 they're working with what's called a WSGI or "wizgy" servers.
1:44 This is an older model, or older gateway interface
1:48 I guess, that doesn't allow for things like async and await programming,
1:52 so it's a big limitation towards that modern,
1:54 scalable stuff. That said, to use FastAPI,
1:58 we've got to use, to really leverage it,
1:59 we've got to use one of these more modern web servers.
2:02 So we're going to talk just super briefly about that.
2:04 You may have heard of data classes.
2:06 And there's another set of classes,
2:08 another library called "pydantic", which has similar structure as data classes and actually integrates with
2:13 them in some ways, but there's a lot of the same types of validation we've
2:18 already spoken around about for FastAPI.
2:21 It does that for just General data Exchange.
2:24 So JSON data, dictionary data and so on, and FastAPI is built upon
2:28 these pydantic classes, so we're going to see how we work with them for defining
2:33 our schemas, for validating what comes in, for converting the types,
2:36 all that kind of stuff. And we'll see that the things we've already mentioned above,
2:40 especially type hints, will provide automatic rich editor support.
2:46 FastAPI has even its own PyCharm plugin.
2:50 I think maybe it's just the pydantic, one of those two, pydantic or
2:53 FastAPI has its own PyCharm plug in,
2:55 and because they put type hints all over the libraries,
2:59 the editors that can leverage type hints to give you more help will do so, and that includes PyCharm and VS Code.