# import urllib.parse
# payload = f'widevine2Challenge={urllib.parse.quote(base64.b64encode(cdm.service_certificate_challenge).decode())}&includeHdcpTestKeyInLicense=true'
# service_cert = await corsFetch(licUrl, "POST", licHeaders, payload, "json")
# service_cert = service_cert['widevine2License']['license']
# payload = f'widevine2Challenge={urllib.parse.quote(getChallenge("b64", service_cert))}&includeHdcpTestKeyInLicense=true'
# licence = await corsFetch(licUrl, "POST", licHeaders, payload, "json")
# licence = licence['widevine2License']['license']
from js import console
import json

payload = loadBody("json")
payload['licenseChallenge'] = getChallenge("b64")

# DEBUG: Vérifier le payload avant envoi
console.log("=== REQUEST DEBUG ===")
console.log("License URL:", licUrl)
console.log("Payload:", json.dumps(payload))
console.log("Headers:", json.dumps(licHeaders))
console.log("Challenge type:", type(payload.get('licenseChallenge')))

licence = await corsFetch(licUrl, "POST", licHeaders, payload, "json")
console.log("Response complet:", json.dumps(licence))
payload['licenseChallenge'] = getChallenge("b64")
licence = await corsFetch(licUrl, "POST", licHeaders, payload, "json")
# Afficher dans la console JS
licence_str = json.dumps(licence) if licence else "null"
console.log("Widevine License JSON:", licence_str)
licence = licence['widevineLicense']['license']