← back to Reid Witlin Onboarding

archive_discontinued.py

42 lines

#!/usr/bin/env python3
"""Archive the single Steve-approved discontinued Reid Witlin product."""
import json, os, urllib.request

PID = "gid://shopify/Product/7774951604275"
DRY_RUN = os.environ.get("DRY_RUN", "1") != "0"

def token():
    for line in open(os.path.expanduser("~/Projects/secrets-manager/.env")):
        if line.startswith("SHOPIFY_ADMIN_TOKEN="):
            return line.split("=", 1)[1].strip().strip('"')
    raise SystemExit("SHOPIFY_ADMIN_TOKEN not found")

def gql(query, variables):
    req = urllib.request.Request(
        "https://designer-laboratory-sandbox.myshopify.com/admin/api/2024-10/graphql.json",
        json.dumps({"query": query, "variables": variables}).encode(),
        {"X-Shopify-Access-Token": token(), "Content-Type": "application/json"})
    return json.load(urllib.request.urlopen(req, timeout=90))

READ = """query($id: ID!) { product(id: $id) { id handle title status vendor } }"""
ARCHIVE = """mutation($input: ProductInput!) { productUpdate(input: $input) { product { id handle title status } userErrors { field message } } }"""

before = (gql(READ, {"id": PID}).get("data") or {}).get("product")
if (not before or before.get("handle") != "pass-the-torch-concrete-architectural-fabrics"
        or before.get("title") != "Pass the Torch Concrete"
        or before.get("vendor") != "Architectural Fabrics"):
    raise SystemExit(f"identity check failed: {before}")
print("before", json.dumps(before, sort_keys=True))
if DRY_RUN:
    print("DRY-RUN: would archive; no write")
else:
    result = gql(ARCHIVE, {"input": {"id": PID, "status": "ARCHIVED"}})
    print("mutation", json.dumps(result, sort_keys=True))
    errors = (((result.get("data") or {}).get("productUpdate") or {}).get("userErrors") or [])
    if result.get("errors") or errors:
        raise SystemExit("archive failed")
    after = (gql(READ, {"id": PID}).get("data") or {}).get("product")
    print("after", json.dumps(after, sort_keys=True))
    if not after or after.get("status") != "ARCHIVED":
        raise SystemExit("archive verification failed")