Note: Persistence helpers and the retry demo are included in the runnable :::{pyodide-cell} below so the demo definitions and execution appear together in the proper order for browser runs. The notebook also shows a CSV-based persistence pattern intended for local runs; browser file persistence may vary in JupyterLite/Pyodide.
## Hero: Reliable Invoice Automation for Business
<div style="display:flex; gap:12px; align-items:flex-start;">
<img src="media/images/invoice.png" width="90">
<div>
### Why this matters
Billing mistakes cost money and reputation. This notebook shows patterns to generate invoices deterministically, deliver them safely with idempotency keys, and retry transient failures — all without network calls so learners can run the demos in Pyodide.
</div>
</div>
## Learning objectives
- Generate deterministic invoices suitable for testing and demos.
- Implement idempotent delivery using idempotency keys and an in-memory store (show persistence pattern via CSV simulation).
- Add a retry wrapper for transient failures while preserving idempotency.
---
Invoice Automation — safe invoice generation and delivery patterns¶
Business framing: Reliable billing automation reduces manual errors, speeds cash collection, and improves customer experience. The in-notebook demo below shows deterministic invoice generation, idempotent delivery simulation, and logging suitable for Pyodide runs.
Pyodide-safe invoice generator + idempotent send simulation¶
Visual intuition: Billing workflow¶
Caption: Invoice generation, persistence, delivery, and safe retry using idempotency keys.
MCQ¶
Q: What mechanism ensures a repeated delivery attempt does not charge or send duplicate invoices?
A) Shorter retry intervals
B) Idempotency keys and stored results
C) Larger thread pools
(Answer: B)
Exercises¶
Persist
_id_storeto a CSV-like list (print rows) and load it back to simulate restart persistence.Add a
statusfield and simulate transient failures wheresend_invoice_safesometimes returnsfailedand needs a retry wrapper that respects idempotency.(Stretch) Show how you would integrate a real email API (describe required steps in markdown) and what secrets/config you would avoid storing in the notebook.
Notes: This demo is deterministic and runs without external services. For production, persist invoices to a database and use provider SDKs with secure secret management.