import json
import os
import ssl
import urllib.error
import urllib.request

api_key = os.environ['API_KEY']
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
cases = [
    ('origin-lb-chat', 'https://sub-lb.tap365.org/v1/chat/completions', {
        'model': 'gpt-5.4',
        'messages': [{'role': 'user', 'content': 'reply exactly ORIGIN_LB_CHAT_OK_20260410'}],
        'max_tokens': 64,
        'stream': False,
    }),
    ('origin-lb-responses', 'https://sub-lb.tap365.org/v1/responses', {
        'model': 'gpt-5.4',
        'input': 'reply exactly ORIGIN_LB_RESP_OK_20260410',
        'max_output_tokens': 64,
        'stream': False,
    }),
]
for label, url, payload in cases:
    req = urllib.request.Request(url, data=json.dumps(payload).encode(), headers={
        'Authorization': 'Bearer ' + api_key,
        'Content-Type': 'application/json',
        'Host': 'sub-lb.tap365.org',
    })
    opener = urllib.request.build_opener(urllib.request.HTTPSHandler(context=ctx))
    try:
        with opener.open(req, timeout=90) as resp:
            body = resp.read().decode()
            obj = json.loads(body)
            print(f'[{label}] HTTP {resp.status}')
            print(f'[{label}] Content-Type: {resp.headers.get("Content-Type")}')
            if '/chat/completions' in url:
                print(f'[{label}] content={obj["choices"][0]["message"]["content"]}')
            else:
                print(f'[{label}] output_text={obj["output"][0]["content"][0]["text"]}')
    except urllib.error.HTTPError as exc:
        print(f'[{label}] HTTP {exc.code}')
        print(f'[{label}] Content-Type: {exc.headers.get("Content-Type")}')
        print(f'[{label}] body={exc.read().decode(errors="replace")}')
