Files
ai-workflow-course/modules/17-secrets-config-and-environments/lab/starter/sync.py
T
claude 389ac2e460 style(no-slop): remove every em-dash + banned words across all modules + capstone
Apply the no-ai-slop standard (now binding in AGENTS.md): the em-dash character is
banned outright (restructured, not blind-replaced), plus the banned word/phrase
list (delve, leverage, robust, seamless, truly, unlock, etc.). 0 em-dashes remain
in modules + capstone; the only "robust" left is the planted M10 ai-change.patch
trap. Module H1 titles use a colon separator.

All deliberate teaching devices preserved; labs compile/parse (py/sh/yaml/json);
no junk. AGENTS.md updated with the hard no-slop rules.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TfzV5QvtPDz8LJS3Pu5VLT
2026-06-22 23:21:09 -04:00

50 lines
1.8 KiB
Python

"""A 'sync' command for the tasks-app: the BEFORE picture for Module 17.
This is exactly the kind of file an AI hands you when you ask it to "add a command that syncs
tasks to our backend." It works. It also has two AI-classic mistakes baked in:
1. The API key is hardcoded right here in the source (see API_KEY below).
2. The backend URL is hardcoded too, so there is no way to point dev at a dev server and
prod at the prod one without editing code.
Your job in the lab is to refactor BOTH out of the source and into the environment. Don't read
ahead and fix it yet; first run it as-is so you can see the smell.
Run it:
python sync.py
It does not actually hit the network (so the lab works offline, on any OS); it simulates the
request and prints what it *would* send.
"""
import json
from pathlib import Path
# --- The anti-pattern. This is what we are here to remove. ---------------------------------
API_KEY = "sk-live-9f8a7b6c5d4e3f2a1b0c9d8e7f6a5b4c" # <-- a real-looking secret, in source
BACKEND_URL = "https://api.example-tasks.com/v1" # <-- environment baked into code
# -------------------------------------------------------------------------------------------
STATE = Path(__file__).parent / "tasks.json"
def load_task_count() -> int:
"""Count tasks from the tasks-app state file, if it exists."""
if not STATE.exists():
return 0
return len(json.loads(STATE.read_text()))
def sync() -> int:
count = load_task_count()
# In a real client this would be an authenticated HTTP request. We just show what it'd send.
print(f"POST {BACKEND_URL}/tasks/sync")
print(f"Authorization: Bearer {API_KEY}")
print(f"Body: {{\"task_count\": {count}}}")
print("(simulated) sync OK")
return 0
if __name__ == "__main__":
raise SystemExit(sync())