Modern APIs with FastAPI and Python Transcripts
Chapter: Building a realistic API
Lecture: Concept: Partitioning with routers
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
If you're building a little toy app in some tutorial,
0:02
Yeah, you just create a main.py or app.py and
0:04
you cram all your stuff in there. That is doing such a disservice to people,
0:08
though, to show that way.
0:10
No one wants to build real apps and just have one ginormous file.
0:13
So how do we build real apps?
0:15
Well, we're gonna not directly use the API or the app,
0:20
whatever we named that variable.
0:21
Instead, we're gonna use this thing called the "APIrouter",
0:23
which allows us to define routes for a section of our API.
0:27
So here we have our weather API section and we're going to the router and
0:31
we're saying for the weather method, it's gonna respond to "/api
0:34
/weather" and notice it has a router object? In the views,
0:38
say home views, we also create a router,
0:40
and we say this one responds to "/" and we could even say,
0:44
don't include this thing in the schema.
0:45
We'll talk more about that later.
0:47
Once we have all of our routers set up,
0:49
then we go back to our main.py and we say import those items and say
0:53
include router from home router, include router from APIrouter, and as many of
0:58
these as you want. And this way we can partition our app by functionality, by
1:03
use Case, allows us to work with it more simply and understand it more simply and
1:07
just focus on the part of the app that we're really trying to work on,
1:11
which is one of the goals of software,
1:12
isn't it? Make it easy to maintain and evolve over time.
1:16
Definitely recommend that you use these routers for your API.