Using the httpx module
The httpx
external module is a Python extension that can consume both asynchronous and synchronous REST APIs and has HTTP/1.1 and HTTP/2 support. It is a fast and multi-purpose toolkit that is used to access API services running on WSGI-based platforms, as well as, on ASGI, like the FastAPI services. But first, we need to install it using pip
:
pip install httpx
Then, we can use it directly without further configuration to make two microservices interact, for instance, our student module submitting assignments to the faculty module:
import httpx @router.get('/assignments/list') async def list_assignments(): async with httpx.AsyncClient() as client: response = await client.get( "http://localhost:8000/ch04/faculty/assignments/list") return response.json() @router.post('/assignment/submit') def submit_assignment(assignment:AssignmentRequest...