import json, urllib.request, sys
base = 'https://api.synaglobal.vip'
req = urllib.request.Request(
    base + '/api/auth/login',
    data=json.dumps({'username': 'admin', 'password': 'Nexus@2026'}).encode(),
    method='POST',
    headers={'Content-Type': 'application/json'},
)
tok = json.loads(urllib.request.urlopen(req).read())['access_token']
h = {'Authorization': 'Bearer ' + tok, 'Content-Type': 'application/json'}

# get all servers
srv_data = json.loads(urllib.request.urlopen(
    urllib.request.Request(base + '/api/servers/?per_page=50', headers=h)
).read())
servers = srv_data.get('items', [])
print(f'Total servers: {len(servers)}')
for s in servers:
    print(f"  id={s['id']} name={s.get('name','')} user={s.get('username','')} target={s.get('target_path','')} domain={s.get('domain','')}")

print()
# browse server 8 several paths
for path in ['/', '/www', '/www/wwwroot', '/tmp']:
    b = json.dumps({'server_id': 8, 'path': path}).encode()
    try:
        d = json.loads(urllib.request.urlopen(
            urllib.request.Request(base + '/api/sync/browse', data=b, method='POST', headers=h),
            timeout=20
        ).read())
        n = len(d.get('items', []))
        err = (d.get('error') or '')[:120]
        print(f"server8 {path}: items={n} error={repr(err)}")
        if n > 0:
            for it in d['items'][:6]:
                print(f"    {it['type']:9} {it['name']}")
    except Exception as e:
        print(f"server8 {path}: EXCEPTION {e}")
