#!/usr/bin/env python3
import urllib.request, json

BASE = "https://ota.46-4-121-190.sslip.io"
def call(m, u, d=None, t=None):
    h = {"Content-Type": "application/json"}
    if t: h["Authorization"] = f"Bearer {t}"
    req = urllib.request.Request(u, data=(json.dumps(d).encode() if d else None), headers=h, method=m)
    with urllib.request.urlopen(req, timeout=30) as r: return json.loads(r.read())

tok = call("POST", f"{BASE}/authentication", {"strategy":"local","username":"admin","password":"353fde0a0fe4e8a0b2799e54"})["accessToken"]
NEW = "6a77298d29b7d373b839dde1"
OLD = "6a772081bce64410088d075c"

r = call("PATCH", f"{BASE}/uploads/{NEW}", {"status":"released"}, tok)
print("release NEW (dfb886ee):", r.get("status") or r)
r2 = call("PATCH", f"{BASE}/uploads/{OLD}", {"status":"obsolete"}, tok)
print("demote OLD (71b3e457):", r2.get("status") or r2)

d = call("GET", f"{BASE}/uploads", t=tok)
u = d.get("data", d) if isinstance(d, dict) else d
print("--- released now ---")
for x in u:
    if x.get("status") == "released":
        print("RELEASED:", x["_id"], x.get("version"), x.get("updateId"))
