import os
import requests
headers = {"Authorization": f"Bearer {os.environ['OPENTYPE_API_KEY']}"}
key_id = "key_d92a9043204d41c09c78fc813d54ef06"
old = requests.get(f"https://api.opentype.dev/v1/keys/{key_id}", headers=headers, timeout=30).json()
if old["state"] == "revoked":
# Create a replacement.
resp = requests.post(
"https://api.opentype.dev/v1/keys",
headers={**headers, "Content-Type": "application/json"},
json={"name": old["name"], "scopes": old["scopes"]},
timeout=30,
)
else:
# Rotate in place.
resp = requests.post(f"https://api.opentype.dev/v1/keys/{key_id}/rotate", headers=headers, timeout=30)
body = resp.json()
if not resp.ok:
raise RuntimeError(f"{body['error']['code']}: {body['error']['message']}")
# body["secret"] is shown only once. Store it now.
print(body["id"], body["secret_prefix"])