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