Two APIs, two counts, one dict. Email count, event count, total — all returned together so callers never have to add the numbers themselves. What's the minimum number of API calls?
Two, I think? One for Gmail, one for Calendar? Do they run back-to-back?
Right. Both respond with a dict. Gmail's list lives under messages, Calendar's under items. Count each list, then compute the total inside the return dict:
emails = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": 5})
events = toolset.execute_action(Action.GOOGLECALENDAR_FIND_EVENT, {"query": ""})
email_count = len(emails.get("messages", []))
event_count = len(events.get("items", []))So each API's response key is different, but the defensive .get(key, []) pattern stays identical.
Exactly. The key name changes per API, the shape of the code does not. Wrap it into a function that returns a three-field dict with the derived total included:
def combine_counts(max_emails: int, max_events: int) -> dict:
emails = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_emails})
events = toolset.execute_action(Action.GOOGLECALENDAR_FIND_EVENT, {"query": ""})
email_count = len(emails.get("messages", []))
event_count = len(events.get("items", []))
result = {"emails": email_count, "events": event_count, "total": email_count + event_count}
print(f"Combined counts: {result}")
return resultWhy include the total field if the caller can compute it from the other two?
Because a good return contract answers the question the caller asked. If they wanted a summary, force them to do arithmetic to see it. Including the derived total makes the return self-sufficient.
And both counts run against my real Gmail and my real Calendar — the total reflects what's actually in each right now?
Live counts, live dict. One function, two APIs, three fields — the production shape of every Week 1 aggregator from here.
TL;DR: One function calls two APIs, counts each list, returns a dict with both counts plus a derived total.
messages, Calendar: items; defensive .get(key, []) each time"total": a + b computed inside the dict literalresult["emails"] / result["events"] / result["total"]| API | Response key |
|---|---|
GMAIL_FETCH_EMAILS | messages |
GOOGLECALENDAR_FIND_EVENT | items |
GOOGLETASKS_LIST_TASKS | items |
Two APIs, two counts, one dict. Email count, event count, total — all returned together so callers never have to add the numbers themselves. What's the minimum number of API calls?
Two, I think? One for Gmail, one for Calendar? Do they run back-to-back?
Right. Both respond with a dict. Gmail's list lives under messages, Calendar's under items. Count each list, then compute the total inside the return dict:
emails = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": 5})
events = toolset.execute_action(Action.GOOGLECALENDAR_FIND_EVENT, {"query": ""})
email_count = len(emails.get("messages", []))
event_count = len(events.get("items", []))So each API's response key is different, but the defensive .get(key, []) pattern stays identical.
Exactly. The key name changes per API, the shape of the code does not. Wrap it into a function that returns a three-field dict with the derived total included:
def combine_counts(max_emails: int, max_events: int) -> dict:
emails = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_emails})
events = toolset.execute_action(Action.GOOGLECALENDAR_FIND_EVENT, {"query": ""})
email_count = len(emails.get("messages", []))
event_count = len(events.get("items", []))
result = {"emails": email_count, "events": event_count, "total": email_count + event_count}
print(f"Combined counts: {result}")
return resultWhy include the total field if the caller can compute it from the other two?
Because a good return contract answers the question the caller asked. If they wanted a summary, force them to do arithmetic to see it. Including the derived total makes the return self-sufficient.
And both counts run against my real Gmail and my real Calendar — the total reflects what's actually in each right now?
Live counts, live dict. One function, two APIs, three fields — the production shape of every Week 1 aggregator from here.
TL;DR: One function calls two APIs, counts each list, returns a dict with both counts plus a derived total.
messages, Calendar: items; defensive .get(key, []) each time"total": a + b computed inside the dict literalresult["emails"] / result["events"] / result["total"]| API | Response key |
|---|---|
GMAIL_FETCH_EMAILS | messages |
GOOGLECALENDAR_FIND_EVENT | items |
GOOGLETASKS_LIST_TASKS | items |
Create a free account to get started. Paid plans unlock all tracks.