Quiz: Working With Outside Services¶
Test your understanding of adapters, response validation, timeouts and retries, silent failure, three-state results, circuit breakers and webhooks with these review questions.
1. What is an API adapter?¶
- A package the vendor publishes that wraps its API in ready-made functions
- A setting on the vendor's dashboard that limits calls per second
- A small piece of your own code that is the only place in the app that talks to one vendor
- A plug that converts one kind of API key into another
Show Answer
The correct answer is C. An adapter is the one door to a vendor. The rest of the app calls the adapter and gets back an answer in the app's own shape, so when the vendor changes, there's one place to fix. It is also where the timeout, retry rule and response check live. Option A describes an API client library, which belongs inside an adapter. Option B describes a rate limit.
Concept Tested: API Adapter
2. What does idempotency mean?¶
- Doing an operation twice has the same effect as doing it once
- An operation always finishes within its timeout
- An operation can only be run by its owner
- An operation returns the same shape of JSON every time
Show Answer
The correct answer is A. Reading data is idempotent, since searching twice changes nothing. Sending a text or adding scans after a payment is not, unless you make it so. That matters because retries and vendors like Stripe can deliver the same request more than once. Each Stripe event has a unique ID, and recording which IDs you've handled makes a repeat harmless. The other options describe timeouts, access and response shape.
Concept Tested: Idempotency
See: Idempotency
3. An adapter wraps a search in catch { return []; }. Why is this dangerous in a safety app?¶
- It crashes the app whenever the vendor is down
- It retries the call forever
- It sends the member's data to the wrong vendor
- Every failure looks like a successful search that found nothing, so a vendor outage reads as "all clear"
Show Answer
The correct answer is D. This is failure read as empty, a common silent failure. The code does keep the app from crashing, which is why agents write it when asked to make a feature robust. But a timeout, a 401 or a changed API all come back as "no matches," and the results page looks normal when it shouldn't. The fix is a third result, "not checked," and a test that proves the adapter uses it.
Concept Tested: Failure Read as Empty
4. A vendor renames the field matches to results. How does vendor response validation change what the member sees?¶
- It renames the field back so the old code keeps working
- The schema check fails, the check says "not checked," and the gap shows on the results page
- It retries until the vendor uses the old name again
- It hides the check from the results page
Show Answer
The correct answer is B. Without validation, the code reads a missing matches as empty and reports "no matches" for every scan. With a Zod schema and safeParse, the unexpected shape is caught and the check returns "not checked." The member sees a truthful gap, and you get a visible sign that something changed. Validation doesn't rename fields (A), retry (C) or hide checks (D).
Concept Tested: Vendor Response Validation
5. Why should the trust score fail closed, while analytics tracking can fail open?¶
- Analytics is more expensive to run
- The trust score runs on the server and analytics runs in the browser
- A wrong trust score can reassure a member about a scammer, while lost analytics costs only some data
- Analytics vendors never go down
Show Answer
The correct answer is C. Fail closed or fail open is a choice about which mistake costs less. If a check can't run, leaning toward "safe" can hurt a member, so the score leans toward neutral and says "not checked." If analytics fails, letting the app carry on loses a few page views and nobody is harmed. Cost (A) and where code runs (B) don't decide it. Every vendor has outages (D).
Concept Tested: Fail Closed or Fail Open
6. An agent proposes scoring by averaging only the checks that ran. One check passed and seven didn't run. What should you tell it?¶
- "Count each not-checked result as a neutral 50, so this case scores about 56, not 100."
- "That's fine, since the one check that ran passed."
- "Drop not-checked results from the page as well, so the numbers match."
- "Count not-checked as a failure, so the score is about 13."
Show Answer
The correct answer is A. Averaging only the checks that ran turns one pass into a score of 100, a false all-clear. Counting each not-checked result as 50 gives (100 + 7 × 50) / 8, about 56, close to neutral. If nothing ran, the score is 50. Hiding the gaps (C) makes it worse. Counting them as failures (D) scares the member about a match nobody checked, which is wrong in the other direction.
Concept Tested: Score Aggregation
See: Score Aggregation
7. Which failures should an adapter retry with exponential backoff?¶
- Every failure, up to ten times
- A 401 from a bad key, since the key might start working
- A 400 for a malformed request, since the vendor might accept it later
- A timeout, a 429 or a 5xx server error, since these might clear on their own
Show Answer
The correct answer is D. Retry only failures that might go away: a dropped connection, a vendor restart, a rate limit that clears in a second. A bad key (B) or a malformed request (C) will fail the same way every time, so retrying only wastes time and calls. Retrying everything many times (A) can hold a scan open for minutes. Double the wait after each try, and add a little jitter so failed scans don't all retry at once.
Concept Tested: Exponential Backoff
8. Your Stripe webhook adds five scans each time it receives "checkout completed." Stripe sometimes sends the same event twice. What should you tell your agent?¶
- "Ask Stripe to stop retrying events."
- "Record each event ID once handled, and skip any event ID you've already seen."
- "Add scans only on the first webhook of each day."
- "Wait ten seconds before handling each event."
Show Answer
The correct answer is B. Stripe delivers each event at least once, and retries for up to three days if your endpoint doesn't answer. Recording the unique event ID makes a repeat harmless, which is idempotency. You can't turn off Stripe's retries (A), and you need them when your server is briefly down. A daily limit (C) would drop payments members made. A delay (D) doesn't stop the duplicate.
Concept Tested: Payment Webhook
See: Payment Webhooks
9. The image-search vendor goes down for an hour. Each scan waits out three 10-second timeouts before marking the check "not checked." What does adding a circuit breaker change?¶
- It makes the vendor come back sooner
- It turns the outage into a passed result so scans finish faster
- After a few failures in a row it stops calling the vendor for a while, so later scans get an instant, truthful "not checked"
- It switches to a second vendor automatically
Show Answer
The correct answer is C. With a breaker, the first few scans of an outage wait out their timeouts. After, say, 5 failures in a row, the breaker opens and skips calls for 30 seconds, returning "not checked" at once. Then it allows one trial call, and if that works, normal service resumes on its own. It can't fix the vendor (A), and it never turns a failure into a pass (B). Switching vendors (D) is a separate design choice.
Concept Tested: Circuit Breaker
See: Circuit Breakers
10. The message check names its AI model ID in the adapter, and the same ID is copied into nine other files. What risk does this create?¶
- When the provider retires that model, calls fail in ten places and some copies are likely to be missed
- The model will give different answers in each file
- The model ID is a secret and will leak
- Copies of the ID slow down each call
Show Answer
The correct answer is A. Model providers retire models. When the named model goes away, every call that names it fails, and with ten copies, one is easy to miss, leaving a check that quietly stops working. Keeping each model ID in one config file means one change. The model doesn't vary by file (B). A model ID is a name, not a secret (C), and copies don't affect speed (D).
Concept Tested: Model ID