Idempotent creates and retry loops are great, but they both presume valid input. If the title field is missing from the payload, you waste an API call discovering it. How do you catch that earlier?
Check the payload at the top — before any network call — and raise if something's wrong?
Raise or return an error object. For a create function, returning an error dict lets the caller collect many validation failures without wrapping the call in try/except. You only call the API once the payload passes:
errors = [k for k in required if not payload.get(k)]
if errors:
return {"ok": False, "errors": errors}So if errors: return ... is a guard clause — it short-circuits before the expensive work happens.
Exactly. Validation is the cheapest operation in the function; do it first. If payload passes, reuse the idempotent create pattern from Day 17. Here's the combined shape:
def validate_before_create(payload: dict) -> dict:
required = ["title"]
errors = [k for k in required if not payload.get(k)]
if errors:
print(f"Validation failed: missing {errors}")
return {"ok": False, "errors": errors}
title = payload["title"]
existing = toolset.execute_action(Action.GOOGLETASKS_LIST_TASKS, {"max_results": 100})
for item in existing.get("items", []):
if item.get("title") == title:
print(f"Found existing task '{title}' with id={item['id']}")
return {"ok": True, "id": item["id"]}
created = toolset.execute_action(Action.GOOGLETASKS_CREATE_TASK, {"title": title})
print(f"Created new task '{title}' with id={created['id']}")
return {"ok": True, "id": created["id"]}Why not payload.get(k) instead of k not in payload? The second catches missing keys but what about empty strings?
Exactly — not payload.get(k) returns True when the key is missing, is empty string, is empty list, or is None. That's usually what you want: any falsy value is a validation failure. If you need strict 'missing' semantics, use k not in payload.
So the function either short-circuits with an error dict or runs the full idempotent create. One call, one clear result, no wasted API traffic on bad input.
Validation first, idempotency second, retries on top if you need them. That is the three-layer defense of a production write.
TL;DR: Validate at the top, short-circuit with errors, only touch the API once input is clean.
not payload.get(k) — catches missing, empty, None, and falsy values| Field | Meaning |
|---|---|
ok: True, id: ... | validated and created/found |
ok: False, errors: [...] | validation failed with all missing keys |
Idempotent creates and retry loops are great, but they both presume valid input. If the title field is missing from the payload, you waste an API call discovering it. How do you catch that earlier?
Check the payload at the top — before any network call — and raise if something's wrong?
Raise or return an error object. For a create function, returning an error dict lets the caller collect many validation failures without wrapping the call in try/except. You only call the API once the payload passes:
errors = [k for k in required if not payload.get(k)]
if errors:
return {"ok": False, "errors": errors}So if errors: return ... is a guard clause — it short-circuits before the expensive work happens.
Exactly. Validation is the cheapest operation in the function; do it first. If payload passes, reuse the idempotent create pattern from Day 17. Here's the combined shape:
def validate_before_create(payload: dict) -> dict:
required = ["title"]
errors = [k for k in required if not payload.get(k)]
if errors:
print(f"Validation failed: missing {errors}")
return {"ok": False, "errors": errors}
title = payload["title"]
existing = toolset.execute_action(Action.GOOGLETASKS_LIST_TASKS, {"max_results": 100})
for item in existing.get("items", []):
if item.get("title") == title:
print(f"Found existing task '{title}' with id={item['id']}")
return {"ok": True, "id": item["id"]}
created = toolset.execute_action(Action.GOOGLETASKS_CREATE_TASK, {"title": title})
print(f"Created new task '{title}' with id={created['id']}")
return {"ok": True, "id": created["id"]}Why not payload.get(k) instead of k not in payload? The second catches missing keys but what about empty strings?
Exactly — not payload.get(k) returns True when the key is missing, is empty string, is empty list, or is None. That's usually what you want: any falsy value is a validation failure. If you need strict 'missing' semantics, use k not in payload.
So the function either short-circuits with an error dict or runs the full idempotent create. One call, one clear result, no wasted API traffic on bad input.
Validation first, idempotency second, retries on top if you need them. That is the three-layer defense of a production write.
TL;DR: Validate at the top, short-circuit with errors, only touch the API once input is clean.
not payload.get(k) — catches missing, empty, None, and falsy values| Field | Meaning |
|---|---|
ok: True, id: ... | validated and created/found |
ok: False, errors: [...] | validation failed with all missing keys |
Create a free account to get started. Paid plans unlock all tracks.