Comm Between 2 Apps
Comm Between 2 Apps
Using
our example before, let’s think of a scenario like this:
App A = Student Manager
App B = External Reporter, which pulls or sends data to App A
In this case:
We are going to use the FastAPI app, we created last class as APP A, then we will
define APP B to communicate with APP A which is our student manager.
APP B will be another agent that will be used to send HTTP Request to APP A and
get responses.
What we need
• Python + FastAPI (we already have)
• Uvicorn (ASGI server to run FastAPI) (we already have)
• httpx (to make async HTTP requests between apps) (we will install using pip
install httpx)
• Two terminal windows (or run apps in separate Python scripts) (we will use
swagger here, but you guys can decide to use your terminal or Postman if
you wish)
Steps
Let’s put up our already existing code from last week in a python file and run it on
port 8000 or any port of our choice.
Now let’s create APP B, the app that will communicate with APP A. Let’s make APP B
to run on another port e.g port 8001.
How They Will Communicate
We will use httpx to send HTTP requests from APP B to APP A. (i.e. HTTP requests
from an external agent to the student manager)
These requests will be defined by the HTTP methods we earlier discussed.
Note, only code for App B is below, we will get App A’s code from the previous class
please.
app = FastAPI()
# We're using thesame schema as the one from our last note
class Student(BaseModel):
name: str
department: Optional[str] = None
matric: int
level: str
for this communication, we will use the httpx library which is a modern HTTP client
with async support. It allows sending of HTTP requests asynchronously to other
APIs. It is important because it enables your FastAPI app to talk to another app
using HTTP.
OUR ENDPOINTS
a. POST Endpoint
@app.post("/send-student/")
Here, we defined a POST route at /send-student. This is the route the client will call
to send a new student to the student_manager app.
Next is our async function
async def send_student_to_manager(student: Student):
SomeThings to consider
Httpx: Is a third-party python library used to make HTTP requests. httpx support
async operations, it allows our app to call other APIs.
async with httpx.AsyncClient() as client: Tells python to do the task
asynchronously
httpx.AsyncClient(): Is a tool used to send HTTP requests in async mode. It helps
FastAPI act like a client and talk to other apps.
as client: This means we’re naming this HTTP client as client so we can use it to
call client.get(), client.post() etc.
response = await client.get("http://localhost:8000/students/"): Means,
FastAPI is sending a GET request to the /students/endpoint running on another app
on our device and wait for the response.
note:
• await – will wait for the server response before moving on.
• client.get() will sends an HTTP GET request to fetch data.
• response = Stores the server’s response (status, data, etc.) so you can use
it.
async and await: Are part of asynchronous programming, which is used to write
code that can pause and wait for something (like a network call) without blocking
the entire program. It makes the program to do things while waiting for something
to finish.