Modern APIs with FastAPI and Python Transcripts
Chapter: Building a realistic API
Lecture: Concept: Async API methods
Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's review converting our regular API method to an async one. Well, we're gonna put async def weather instead of just def weather.
0:09 We don't need to change the router or the URL or anything like that. FastAPI can just deal with both kinds and that's fine.
0:16 We're going to say it's async, and then that only really makes sense to do if we're actually calling other async things.
0:23 We wanna ultimately call our API, or open weather map, asynchronously. So we're gonna make that intermediate piece asynchronous as well,
0:31 so then we're await, get report async, get our dictionary back, off it goes. Now, one step down, we've got our get report async and it is itself
0:41 async, right? And then we need to do something interesting within it, so we're gonna work with httpx instead of response, instead of requests,
0:49 I want to create an async with block and then we just await "client dot get url". Couldn't be simpler.
0:54 We swapped it over from the synchronous request version over to the asynchronous httpx version
0:59 by just really changing these two lines of code, sprinkling in some awaits, Good to go. A really, really nice way to add a huge amount of scalability
1:09 if we're waiting on external things like openweathermap.org.