refactor url encoded form requests

This commit is contained in:
Brahim Hadriche committed 2024-02-26 18:44:57 -05:00
1 parent edbfc86e56
commit f2ffb42876
4 files changed
+33 -30

No files matched your search

@@ -99,6 +99,7 @@ class LoungeApi
end if end if
request = HttpClient.Get("https://www.youtube.com/api/lounge/pairing/generate_screen_id") request = HttpClient.Get("https://www.youtube.com/api/lounge/pairing/generate_screen_id")
response = request.Await() response = request.Await()
if not response.IsSuccess() if not response.IsSuccess()
error = `Failed to get screen id. Please restart playlet to use the cast function.\n${response.ErrorMessage()}` error = `Failed to get screen id. Please restart playlet to use the cast function.\n${response.ErrorMessage()}`
@@ -106,6 +107,7 @@ class LoungeApi
DialogUtils.ShowDialog(error, "Lounge service setup error") DialogUtils.ShowDialog(error, "Lounge service setup error")
return invalid return invalid
end if end if
id = response.Text() id = response.Text()
if not IsString(id) if not IsString(id)
error = `Failed to get screen id. Please restart playlet to use the cast function.\nNot a valid screen id: ${id}` error = `Failed to get screen id. Please restart playlet to use the cast function.\nNot a valid screen id: ${id}`
@@ -113,6 +115,7 @@ class LoungeApi
DialogUtils.ShowDialog(error, "Lounge service setup error") DialogUtils.ShowDialog(error, "Lounge service setup error")
return invalid return invalid
end if end if
m.screenId = id m.screenId = id
return id return id
end function end function
@@ -122,9 +125,11 @@ class LoungeApi
return m.loungeToken return m.loungeToken
end if end if
form = UrlUtils.GetUrlEncodedForm({ "screen_ids": m.screenId }) request = HttpClient.Post("https://www.youtube.com/api/lounge/pairing/get_lounge_token_batch", "")
request = HttpClient.Post("https://www.youtube.com/api/lounge/pairing/get_lounge_token_batch", form) request.UrlEncodedForm({
request.Header("Content-Type", "application/x-www-form-urlencoded") "screen_ids": m.screenId
})
response = request.Await() response = request.Await()
if not response.IsSuccess() if not response.IsSuccess()
error = `Failed to get lounge token. Please restart playlet to use the cast function.\n${response.ErrorMessage()}` error = `Failed to get lounge token. Please restart playlet to use the cast function.\n${response.ErrorMessage()}`
@@ -148,7 +153,6 @@ class LoungeApi
end function end function
function GetSessionData() as boolean function GetSessionData() as boolean
baseUrl = "https://www.youtube.com/api/lounge/bc/bind"
queryParams = { queryParams = {
"device": "LOUNGE_SCREEN" "device": "LOUNGE_SCREEN"
"theme": "cl" "theme": "cl"
@@ -166,10 +170,11 @@ class LoungeApi
queryParams["loungeIdToken"] = m.loungeToken queryParams["loungeIdToken"] = m.loungeToken
queryParams["zx"] = m.Zx() queryParams["zx"] = m.Zx()
url = UrlUtils.BuildUrl(baseUrl, invalid, queryParams) request = HttpClient.Post("https://www.youtube.com/api/lounge/bc/bind", "")
request.QueryParams(queryParams)
request = HttpClient.Post(url, "count=0") request.UrlEncodedForm({
request.Header("Content-Type", "application/x-www-form-urlencoded") "count": 0
})
response = request.Await() response = request.Await()
if not response.IsSuccess() if not response.IsSuccess()
@@ -435,7 +440,6 @@ class LoungeApi
return return
end if end if
baseUrl = "https://www.youtube.com/api/lounge/bc/bind"
queryParams = { queryParams = {
"device": "LOUNGE_SCREEN" "device": "LOUNGE_SCREEN"
"theme": "cl" "theme": "cl"
@@ -462,8 +466,6 @@ class LoungeApi
queryParams["AID"] = messages[0].commandId queryParams["AID"] = messages[0].commandId
end if end if
url = UrlUtils.BuildUrl(baseUrl, invalid, queryParams)
postData = [ postData = [
{ key: "count", value: messages.Count() } { key: "count", value: messages.Count() }
{ key: "ofs", value: loungeData.ofs } { key: "ofs", value: loungeData.ofs }
@@ -487,9 +489,10 @@ class LoungeApi
LogDebug("Sending messages:", postData) LogDebug("Sending messages:", postData)
body = UrlUtils.GetUrlEncodedForm(postData) request = HttpClient.Post("https://www.youtube.com/api/lounge/bc/bind", "")
request = HttpClient.Post(url, body) request.QueryParams(queryParams)
request.Header("Content-Type", "application/x-www-form-urlencoded") request.UrlEncodedForm(postData)
request.Send() request.Send()
' we do not need to wait for requests to finish, we check on them later ' we do not need to wait for requests to finish, we check on them later
@@ -23,15 +23,15 @@ function LoungeGenerateManualPairingCodeTask(input as object) as object
input.loungeToken = loungeService.loungeToken input.loungeToken = loungeService.loungeToken
end if end if
bodyParams = { form = {
"lounge_token": input.loungeToken "lounge_token": input.loungeToken
"screen_id": input.screenId "screen_id": input.screenId
"screen_name": input.screenName "screen_name": input.screenName
"device_id": input.deviceId "device_id": input.deviceId
} }
for each key in bodyParams for each key in form
if StringUtils.IsNullOrEmpty(bodyParams[key]) if StringUtils.IsNullOrEmpty(form[key])
return { return {
"success": false "success": false
"error": key + " is empty" "error": key + " is empty"
@@ -39,14 +39,11 @@ function LoungeGenerateManualPairingCodeTask(input as object) as object
end if end if
end for end for
bodyParams["access_type"] = "permanent" form["access_type"] = "permanent"
bodyParams["app"] = "Playlet" form["app"] = "Playlet"
baseUrl = "https://www.youtube.com/api/lounge/pairing/get_pairing_code?ctx=pair" request = HttpClient.Post("https://www.youtube.com/api/lounge/pairing/get_pairing_code?ctx=pair", "")
request.UrlEncodedForm(form)
body = UrlUtils.GetUrlEncodedForm(bodyParams)
request = HttpClient.Post(baseUrl, body)
request.Header("Content-Type", "application/x-www-form-urlencoded")
response = request.Await() response = request.Await()
if not response.IsSuccess() if not response.IsSuccess()
@@ -77,18 +77,15 @@ namespace Http
end if end if
end if end if
registerUrl = "https://www.youtube.com/api/lounge/pairing/register_pairing_code" req = HttpClient.Post("https://www.youtube.com/api/lounge/pairing/register_pairing_code", "")
registerBody = { req.UrlEncodedForm({
"app": "Playlet" "app": "Playlet"
"pairing_code": pairingCode "pairing_code": pairingCode
"screen_id": m.loungeService.screenId "screen_id": m.loungeService.screenId
"screen_name": m.deviceName "screen_name": m.deviceName
"device_id": m.deviceId "device_id": m.deviceId
} })
body = UrlUtils.GetUrlEncodedForm(registerBody)
req = HttpClient.Post(registerUrl, body)
req.Header("Content-Type", "application/x-www-form-urlencoded")
res = req.Await() res = req.Await()
if not res.IsSuccess() if not res.IsSuccess()
@@ -211,6 +211,12 @@ namespace HttpClient
return m return m
end function end function
function UrlEncodedForm(form as object) as HttpRequest
m.Body(UrlUtils.GetUrlEncodedForm(form))
m.Header("Content-Type", "application/x-www-form-urlencoded")
return m
end function
function TimeoutSeconds(timeoutSeconds as integer) as HttpRequest function TimeoutSeconds(timeoutSeconds as integer) as HttpRequest
m._timeoutSeconds = timeoutSeconds m._timeoutSeconds = timeoutSeconds
return m return m