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