I have to serve an API that sends multiple requests to other APIs and merge their responses as the output.
What will be the most optimal way to do that? Is this some async await scenario? Appreciate the advice.
from fastapi import FastAPI
import requests
app = FastAPI()
@app.post('/api')
def main_api():
JScontent = json.loads(request.json())
input = JScontent['content']
response1 = requests.post(url1, json={"input":input})
response2 = requests.post(url2, json={"input":input})
response3 = requests.post(url3, json={"input":input})
response4 = requests.post(url4, json={"input":input})
prediction = fuse_responses(response1, response2, response3, response4)
return prediction
I am currently using Flask for development, but figured it might not have the capability, or will be a hassle to manage such a scenario, hence, open to changing to FastAPI.
[UPDATE]
Did some digging came across this link. I guess it can be applied similarly to FastAPI? I have no prior experience in async & await so appreciate the validation.
from fastapi import FastAPI
app = FastAPI()
async def send_requests(url, input_):
res = await app.post(url, input_)
return res
@app.post('/api')
async def main_api():
JScontent = json.loads(request.json())
input_ = JScontent['content']
res1 = await send_requests(url1, input_)
res2 = await send_requests(url2, input_)
res3 = await send_requests(url3, input_)
res4 = await send_requests(url4, input_)
prediction = fuse_responses(res1, res2, res3, res4)
return prediction