import json, urllib.request

PB = "http://localhost:8102"

def post(path, data, token=None):
    req = urllib.request.Request(PB + path, data=json.dumps(data).encode(),
        headers={"Content-Type": "application/json", **({"Authorization": token} if token else {})})
    return json.load(urllib.request.urlopen(req))

def get(path, token=None):
    req = urllib.request.Request(PB + path, headers={"Authorization": token} if token else {})
    return json.load(urllib.request.urlopen(req))

tok = post("/api/collections/_superusers/auth-with-password",
           {"identity": "edelmar.edd@gmail.com", "password": "564712564712"})["token"]

try:
    cu = get("/api/collections/cuentas/records?perPage=50", tok)
    print("=== CUENTAS de Caja ===")
    for c in cu["items"]:
        print(f"  {c.get('nombre','?')}  (saldo_inicial={c.get('saldo_inicial',0)})")
except Exception as e:
    print("No se pudo leer cuentas:", e)
