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
0:03 to an async one. Well,
0:04 we're gonna put async def weather instead of just def weather.
0:08 We don't need to change the router or the URL or anything like that.
0:12 FastAPI can just deal with both kinds and that's fine.
0:15 We're going to say it's async,
0:17 and then that only really makes sense to do if we're actually calling other async things.
0:22 We wanna ultimately call our API, or open weather map, asynchronously.
0:27 So we're gonna make that intermediate piece asynchronous as well,
0:30 so then we're await, get report async,
0:33 get our dictionary back, off it goes. Now,
0:35 one step down, we've got our get report async and it is itself
0:40 async, right? And then we need to do something interesting within it,
0:43 so we're gonna work with httpx instead of response, instead of requests,
0:48 I want to create an async with block and then we just await "client
0:51 dot get url". Couldn't be simpler.
0:53 We swapped it over from the synchronous request version over to the asynchronous httpx version
0:58 by just really changing these two lines of code, sprinkling in some awaits,
1:03 Good to go. A really,
1:04 really nice way to add a huge amount of scalability
1:08 if we're waiting on external things like openweathermap.org.