2019-02-04 02:57:52 +00:00
|
|
|
' Functions for making requests to the API
|
2021-07-09 20:08:32 +00:00
|
|
|
function buildParams(params = {} as object) as string
|
|
|
|
' Take an object of parameters and construct the URL query
|
|
|
|
|
|
|
|
param_array = []
|
|
|
|
for each field in params.items()
|
|
|
|
item = ""
|
|
|
|
if type(field.value) = "String" or type(field.value) = "roString"
|
2022-08-21 09:30:18 +00:00
|
|
|
item = field.key + "=" + field.value.trim().EncodeUriComponent()
|
2021-07-09 20:08:32 +00:00
|
|
|
else if type(field.value) = "roInteger" or type(field.value) = "roInt"
|
|
|
|
item = field.key + "=" + stri(field.value).trim()
|
|
|
|
'item = field.key + "=" + str(field.value).trim()
|
|
|
|
else if type(field.value) = "roFloat"
|
|
|
|
item = field.key + "=" + stri(int(field.value)).trim()
|
|
|
|
else if type(field.value) = "LongInteger"
|
|
|
|
item = field.key + "=" + field.value.toStr().trim()
|
|
|
|
else if type(field.value) = "roArray"
|
|
|
|
' TODO handle array params
|
|
|
|
else if type(field.value) = "roBoolean"
|
|
|
|
if field.value
|
|
|
|
item = field.key + "=true"
|
|
|
|
else
|
|
|
|
item = field.key + "=false"
|
|
|
|
end if
|
|
|
|
else if field.value = invalid
|
|
|
|
item = field.key + "=null"
|
|
|
|
else if field <> invalid
|
|
|
|
print "Unhandled param type: " + type(field.value)
|
2022-08-21 09:30:18 +00:00
|
|
|
item = field.key + "=" + field.value.EncodeUriComponent()
|
2021-07-09 20:08:32 +00:00
|
|
|
end if
|
|
|
|
|
|
|
|
if item <> "" then param_array.push(item)
|
|
|
|
end for
|
|
|
|
|
|
|
|
return param_array.join("&")
|
2019-04-21 00:34:33 +00:00
|
|
|
end function
|
|
|
|
|
2023-04-19 19:12:50 +00:00
|
|
|
function buildURL(path as string, params = {} as object) as dynamic
|
|
|
|
serverURL = get_url()
|
|
|
|
if serverURL = invalid then return invalid
|
2021-05-01 10:10:44 +00:00
|
|
|
|
2021-07-09 20:08:32 +00:00
|
|
|
' Add intial '/' if path does not start with one
|
|
|
|
if path.Left(1) = "/"
|
2023-04-19 19:12:50 +00:00
|
|
|
full_url = serverURL + path
|
2021-07-09 20:08:32 +00:00
|
|
|
else
|
2023-04-19 19:12:50 +00:00
|
|
|
full_url = serverURL + "/" + path
|
2021-07-09 20:08:32 +00:00
|
|
|
end if
|
2021-05-01 10:10:44 +00:00
|
|
|
|
2021-07-09 20:08:32 +00:00
|
|
|
if params.count() > 0
|
|
|
|
full_url = full_url + "?" + buildParams(params)
|
|
|
|
end if
|
2019-03-05 04:59:31 +00:00
|
|
|
|
2021-07-09 20:08:32 +00:00
|
|
|
return full_url
|
2019-03-08 02:03:19 +00:00
|
|
|
end function
|
|
|
|
|
2023-04-19 19:12:50 +00:00
|
|
|
function APIRequest(url as string, params = {} as object) as dynamic
|
|
|
|
full_url = buildURL(url, params)
|
|
|
|
if full_url = invalid then return invalid
|
|
|
|
|
2021-07-09 20:08:32 +00:00
|
|
|
req = createObject("roUrlTransfer")
|
2023-04-19 19:12:50 +00:00
|
|
|
req.setUrl(full_url)
|
|
|
|
req = authorize_request(req)
|
|
|
|
' SSL cert
|
2023-03-05 18:37:42 +00:00
|
|
|
serverURL = get_setting("server")
|
|
|
|
if serverURL <> invalid and serverURL.left(8) = "https://"
|
2023-03-04 01:21:09 +00:00
|
|
|
req.setCertificatesFile("common:/certs/ca-bundle.crt")
|
|
|
|
end if
|
2019-03-08 02:03:19 +00:00
|
|
|
|
2021-07-09 20:08:32 +00:00
|
|
|
return req
|
2019-02-01 02:26:53 +00:00
|
|
|
end function
|
|
|
|
|
2019-03-13 01:43:06 +00:00
|
|
|
function getJson(req)
|
2021-07-09 20:08:32 +00:00
|
|
|
'req.retainBodyOnError(True)
|
|
|
|
data = req.GetToString()
|
|
|
|
if data = invalid or data = ""
|
|
|
|
return invalid
|
|
|
|
end if
|
|
|
|
json = ParseJson(data)
|
|
|
|
return json
|
2019-02-01 02:26:53 +00:00
|
|
|
end function
|
|
|
|
|
2021-11-13 10:47:13 +00:00
|
|
|
function postVoid(req, data = "" as string) as boolean
|
|
|
|
req.setMessagePort(CreateObject("roMessagePort"))
|
|
|
|
req.AddHeader("Content-Type", "application/json")
|
|
|
|
req.AsyncPostFromString(data)
|
|
|
|
resp = wait(30000, req.GetMessagePort())
|
|
|
|
if type(resp) <> "roUrlEvent"
|
2021-07-09 20:08:32 +00:00
|
|
|
return false
|
|
|
|
end if
|
2021-11-13 10:47:13 +00:00
|
|
|
|
|
|
|
if resp.GetResponseCode() = 200
|
|
|
|
return true
|
|
|
|
end if
|
|
|
|
|
|
|
|
return false
|
2019-03-13 01:43:06 +00:00
|
|
|
end function
|
|
|
|
|
2023-05-05 17:13:49 +00:00
|
|
|
function headVoid(req) as boolean
|
|
|
|
req.setMessagePort(CreateObject("roMessagePort"))
|
|
|
|
req.AddHeader("Content-Type", "application/json")
|
|
|
|
req.AsyncHead()
|
|
|
|
resp = wait(30000, req.GetMessagePort())
|
|
|
|
if type(resp) <> "roUrlEvent"
|
|
|
|
return false
|
|
|
|
end if
|
|
|
|
|
|
|
|
if resp.GetResponseCode() = 200
|
|
|
|
return true
|
|
|
|
end if
|
|
|
|
|
|
|
|
return false
|
|
|
|
end function
|
|
|
|
|
|
|
|
function getVoid(req) as boolean
|
|
|
|
req.setMessagePort(CreateObject("roMessagePort"))
|
|
|
|
req.AddHeader("Content-Type", "application/json")
|
|
|
|
req.AsyncGetToString()
|
|
|
|
resp = wait(30000, req.GetMessagePort())
|
|
|
|
|
|
|
|
if type(resp) <> "roUrlEvent"
|
|
|
|
return false
|
|
|
|
end if
|
|
|
|
|
|
|
|
if resp.GetResponseCode() = 200
|
|
|
|
return true
|
|
|
|
end if
|
|
|
|
|
|
|
|
return false
|
|
|
|
end function
|
|
|
|
|
2021-07-09 20:08:32 +00:00
|
|
|
function postJson(req, data = "" as string)
|
|
|
|
req.setMessagePort(CreateObject("roMessagePort"))
|
|
|
|
req.AddHeader("Content-Type", "application/json")
|
|
|
|
req.AsyncPostFromString(data)
|
|
|
|
resp = wait(30000, req.GetMessagePort())
|
|
|
|
if type(resp) <> "roUrlEvent"
|
|
|
|
return invalid
|
|
|
|
end if
|
2019-03-13 01:43:06 +00:00
|
|
|
|
2021-07-09 20:08:32 +00:00
|
|
|
if resp.getString() = ""
|
|
|
|
return invalid
|
|
|
|
end if
|
2019-03-13 01:43:06 +00:00
|
|
|
|
2021-07-09 20:08:32 +00:00
|
|
|
json = ParseJson(resp.GetString())
|
2019-03-13 01:43:06 +00:00
|
|
|
|
2021-07-09 20:08:32 +00:00
|
|
|
return json
|
2019-03-13 01:43:06 +00:00
|
|
|
end function
|
|
|
|
|
2022-01-09 05:43:17 +00:00
|
|
|
function deleteVoid(req)
|
|
|
|
req.setMessagePort(CreateObject("roMessagePort"))
|
|
|
|
req.AddHeader("Content-Type", "application/json")
|
|
|
|
req.SetRequest("DELETE")
|
|
|
|
req.GetToString()
|
|
|
|
|
|
|
|
return true
|
|
|
|
end function
|
|
|
|
|
2020-02-19 07:33:49 +00:00
|
|
|
function get_url()
|
2023-04-19 19:12:50 +00:00
|
|
|
serverURL = get_setting("server")
|
|
|
|
if serverURL = invalid then return invalid
|
2019-04-20 03:09:48 +00:00
|
|
|
|
2023-04-19 19:12:50 +00:00
|
|
|
if serverURL.right(1) = "/"
|
|
|
|
serverURL = serverURL.left(serverURL.len() - 1)
|
|
|
|
end if
|
|
|
|
|
|
|
|
' append http:// to the start if not specified
|
|
|
|
if serverURL.left(7) <> "http://" and serverURL.left(8) <> "https://"
|
|
|
|
serverURL = "http://" + serverURL
|
2021-07-09 20:08:32 +00:00
|
|
|
end if
|
2023-04-19 19:12:50 +00:00
|
|
|
return serverURL
|
2019-02-01 02:26:53 +00:00
|
|
|
end function
|
|
|
|
|
2023-05-05 17:13:49 +00:00
|
|
|
function getString(req)
|
|
|
|
data = req.GetToString()
|
|
|
|
return data
|
|
|
|
end function
|
|
|
|
|
|
|
|
function postString(req, data = "" as string)
|
|
|
|
req.setMessagePort(CreateObject("roMessagePort"))
|
|
|
|
req.AddHeader("Content-Type", "application/json")
|
|
|
|
req.AsyncPostFromString(data)
|
|
|
|
resp = wait(30000, req.GetMessagePort())
|
|
|
|
if type(resp) <> "roUrlEvent"
|
|
|
|
return invalid
|
|
|
|
end if
|
|
|
|
|
|
|
|
return resp.getString()
|
|
|
|
end function
|
|
|
|
|
2019-02-01 02:26:53 +00:00
|
|
|
function authorize_request(request)
|
2023-03-27 16:50:44 +00:00
|
|
|
user = get_setting("active_user")
|
2021-07-09 20:08:32 +00:00
|
|
|
|
2023-03-27 16:50:44 +00:00
|
|
|
auth = "MediaBrowser" + " Client=" + Chr(34) + "Jellyfin Roku" + Chr(34)
|
|
|
|
auth = auth + ", Device=" + Chr(34) + m.global.device.name + " (" + m.global.device.friendlyName + ")" + Chr(34)
|
2021-07-09 20:08:32 +00:00
|
|
|
|
|
|
|
if user <> invalid and user <> ""
|
2023-03-27 16:50:44 +00:00
|
|
|
auth = auth + ", DeviceId=" + Chr(34) + m.global.device.id + Chr(34)
|
2021-07-09 20:08:32 +00:00
|
|
|
auth = auth + ", UserId=" + Chr(34) + user + Chr(34)
|
2023-03-27 16:50:44 +00:00
|
|
|
else
|
|
|
|
auth = auth + ", DeviceId=" + Chr(34) + m.global.device.uuid + Chr(34)
|
2021-07-09 20:08:32 +00:00
|
|
|
end if
|
|
|
|
|
2023-03-27 16:50:44 +00:00
|
|
|
auth = auth + ", Version=" + Chr(34) + m.global.app.version + Chr(34)
|
|
|
|
|
2021-07-09 20:08:32 +00:00
|
|
|
token = get_user_setting("token")
|
|
|
|
if token <> invalid and token <> ""
|
|
|
|
auth = auth + ", Token=" + Chr(34) + token + Chr(34)
|
|
|
|
end if
|
|
|
|
|
2021-09-12 14:43:25 +00:00
|
|
|
request.AddHeader("Authorization", auth)
|
2021-07-09 20:08:32 +00:00
|
|
|
return request
|
2019-03-05 04:59:31 +00:00
|
|
|
end function
|