we have a general question calling API endpoints, especially if we cannot be sure if this endpoints have a reasonable respond time.
What we see is that an API call is made by the action server itself so it is not independent for each conversation. This means that the complete action server waits for every API call that is made. So if there is an API call that takes a couple of seconds (for whatever reason), the action server is on hold and all other conversations are waiting as well.
Is there an in-build solution for this behavior or do we have to work with asyncio like solutions?
It would be sufficient to handle API calls conversation independent, so that other conversations don’t wait for API calls of other conversations.
We would appreciate your suggestions on this topic.
Thanks in advance!
you are right. It seems like it is common practice to use asyncio. To realize our approach while using OAuth in our API requests, we need also authlib, aiohttp and httpx.
Here is a code snippet as a blueprint for combining this:
async with AsyncOAuth2Client(
client_id=client_id,
client_secret=client_secret
) as oauth:
try:
token = await oauth.fetch_token(
url=token_url,
username=username,
password=password,
#grant_type="password",
client_id=client_id,
client_secret=client_secret
)
print("Token fetched successfully:", token)
except Exception as e:
print("Failed to fetch token:", e)
And using this retrieved token in a GET-response:
async with AsyncOAuth2Client(
client_id=client_id,
client_secret=client_secret,
token=token
) as oauth:
try:
# Make a GET request to the API
response = await oauth.get(url, timeout=15)
if response.status_code == 200:
data = response.json()
print("API Response:", data)
else:
print(f"Failed to fetch resource. Status: {response.status_code}")
except Exception as e:
print("Error making API call:", e)