import json, urllib.request, urllib.error, os

# Upload using multipart
boundary = '----FormBoundary7MA4YWxkTrZu0gW'

with open('/app/working/workspaces/default/balimentari-scene1.jpg', 'rb') as f:
    image_bytes = f.read()

body = (
    f'------{boundary}\r\n'
    f'Content-Disposition: form-data; name="file"; filename="scene1.jpg"\r\n'
    f'Content-Type: image/jpeg\r\n\r\n'
).encode('utf-8') + image_bytes + f'\r\n------{boundary}--\r\n'.encode('utf-8')

req = urllib.request.Request(
    'https://api.viostudio.id/v1/assets',
    data=body,
    headers={
        'Authorization': 'Bearer vio_sk_vA8llIYm51J3oyXtfevrM92yUBJ8-3IS1sIn6xydF8o',
        'Content-Type': f'multipart/form-data; boundary=----{boundary}',
        'User-Agent': 'Mozilla/5.0'
    },
    method='POST'
)

try:
    with urllib.request.urlopen(req, timeout=30) as resp:
        result = json.loads(resp.read())
        print(json.dumps(result, indent=2))
except urllib.error.HTTPError as e:
    print('HTTP Error:', e.code, e.read().decode())
except Exception as e:
    print('Error:', str(e))
