Refactor query params
This commit is contained in:
1 parent
0ddc9a48aa
commit
0acb7031d3
6 files changed
+162
-128
No files matched your search
@@ -6,6 +6,7 @@ import "pkg:/source/utils/ErrorUtils.bs"
|
|||||||
import "pkg:/source/utils/FocusManagement.bs"
|
import "pkg:/source/utils/FocusManagement.bs"
|
||||||
import "pkg:/source/utils/LoadingScreen.bs"
|
import "pkg:/source/utils/LoadingScreen.bs"
|
||||||
import "pkg:/source/utils/Logging.bs"
|
import "pkg:/source/utils/Logging.bs"
|
||||||
|
import "pkg:/source/utils/UrlUtils.bs"
|
||||||
|
|
||||||
function Init()
|
function Init()
|
||||||
m.top.focusable = true
|
m.top.focusable = true
|
||||||
@@ -222,7 +223,7 @@ function Search(text as string)
|
|||||||
else if IsArray(value) and value.Count() > 0
|
else if IsArray(value) and value.Count() > 0
|
||||||
feedSource.queryParams[filter] = {
|
feedSource.queryParams[filter] = {
|
||||||
value: value,
|
value: value,
|
||||||
arrayType: HttpClient.QueryParamArrayType.CommaSeparated
|
arrayType: UrlUtils.QueryParamArrayType.CommaSeparated
|
||||||
}
|
}
|
||||||
end if
|
end if
|
||||||
end for
|
end for
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import "pkg:/components/Web/WebServer/Http/HttpUtils.bs"
|
import "pkg:/components/Web/WebServer/Http/HttpUtils.bs"
|
||||||
import "pkg:/source/services/XmlObject.bs"
|
import "pkg:/source/services/XmlObject.bs"
|
||||||
import "pkg:/source/utils/StringUtils.bs"
|
import "pkg:/source/utils/StringUtils.bs"
|
||||||
|
import "pkg:/source/utils/UrlUtils.bs"
|
||||||
|
|
||||||
class DashManifest
|
class DashManifest
|
||||||
|
|
||||||
@@ -349,7 +350,7 @@ class DashManifest
|
|||||||
|
|
||||||
function ParseAudioLanguage(url as string) as object
|
function ParseAudioLanguage(url as string) as object
|
||||||
result = { lang: "", acont: "" }
|
result = { lang: "", acont: "" }
|
||||||
queryParams = m.utils.ParseUrlQueryComponents(url)
|
queryParams = UrlUtils.ParseQueryComponents(url)
|
||||||
if StringUtils.IsNullOrEmpty(queryParams["xtags"])
|
if StringUtils.IsNullOrEmpty(queryParams["xtags"])
|
||||||
return result
|
return result
|
||||||
end if
|
end if
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import "HttpRequestState.bs"
|
import "HttpRequestState.bs"
|
||||||
import "pkg:/components/Web/WebServer/Http/HttpUtils.bs"
|
import "pkg:/components/Web/WebServer/Http/HttpUtils.bs"
|
||||||
import "pkg:/source/utils/Types.bs"
|
import "pkg:/source/utils/Types.bs"
|
||||||
|
import "pkg:/source/utils/UrlUtils.bs"
|
||||||
|
|
||||||
namespace Http
|
namespace Http
|
||||||
|
|
||||||
@@ -202,7 +203,7 @@ namespace Http
|
|||||||
split = m.route.Instr("?")
|
split = m.route.Instr("?")
|
||||||
if split <> -1
|
if split <> -1
|
||||||
m.route = m.route.Left(split)
|
m.route = m.route.Left(split)
|
||||||
m.query = utils.ParseUrlQueryComponents(m.uri)
|
m.query = UrlUtils.ParseQueryComponents(m.uri)
|
||||||
else
|
else
|
||||||
m.query = {}
|
m.query = {}
|
||||||
end if
|
end if
|
||||||
|
|||||||
@@ -3,39 +3,6 @@ import "pkg:/source/utils/Types.bs"
|
|||||||
namespace Http
|
namespace Http
|
||||||
|
|
||||||
class Utils
|
class Utils
|
||||||
|
|
||||||
function ParseUrlQueryComponents(url as string) as object
|
|
||||||
query = {}
|
|
||||||
index = Instr(0, url, "?")
|
|
||||||
url = Mid(url, index + 1)
|
|
||||||
components = url.split("&")
|
|
||||||
for each component in components
|
|
||||||
key = component
|
|
||||||
value = ""
|
|
||||||
hasValue = false
|
|
||||||
if Instr(0, component, "=")
|
|
||||||
keyValue = component.split("=")
|
|
||||||
key = keyValue[0]
|
|
||||||
value = keyValue[1].DecodeUriComponent()
|
|
||||||
hasValue = true
|
|
||||||
end if
|
|
||||||
|
|
||||||
if query.DoesExist(key) and hasValue
|
|
||||||
oldValue = query[key]
|
|
||||||
if GetInterface(oldValue, "ifArray") = invalid
|
|
||||||
oldValue = [oldValue]
|
|
||||||
end if
|
|
||||||
oldValue.push(value.DecodeUriComponent())
|
|
||||||
value = oldValue
|
|
||||||
end if
|
|
||||||
|
|
||||||
if key <> ""
|
|
||||||
query[key] = value
|
|
||||||
end if
|
|
||||||
end for
|
|
||||||
return query
|
|
||||||
end function
|
|
||||||
|
|
||||||
function MimeType(uri = "" as string) as string
|
function MimeType(uri = "" as string) as string
|
||||||
map = m.MimeTypes
|
map = m.MimeTypes
|
||||||
if map = invalid
|
if map = invalid
|
||||||
@@ -50,22 +17,22 @@ namespace Http
|
|||||||
end if
|
end if
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function GetExtension(fn as string) as string
|
function GetExtension(filename as string) as string
|
||||||
l = fn.tokenize(".")
|
parts = filename.tokenize(".")
|
||||||
if l.count() > 0
|
if parts.count() > 0
|
||||||
return l.GetTail()
|
return parts.GetTail()
|
||||||
else
|
else
|
||||||
return ""
|
return ""
|
||||||
end if
|
end if
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function HttpTitle(code = 0 as integer) as string
|
function HttpTitle(code = 0 as integer) as string
|
||||||
hcm = m.HttpTitles
|
map = m.httpCodesMap
|
||||||
if hcm = invalid
|
if map = invalid
|
||||||
hcm = ParseJson(ReadAsciiFile("libpkg:/config/http-codes.json5"))
|
map = ParseJson(ReadAsciiFile("libpkg:/config/http-codes.json5"))
|
||||||
m.HttpTitles = hcm
|
m.httpCodesMap = map
|
||||||
end if
|
end if
|
||||||
return ValidString(hcm[`${code}`])
|
return ValidString(map[`${code}`])
|
||||||
end function
|
end function
|
||||||
|
|
||||||
end class
|
end class
|
||||||
|
|||||||
@@ -4,14 +4,10 @@ import "pkg:/source/utils/CryptoUtils.bs"
|
|||||||
import "pkg:/source/utils/Logging.bs"
|
import "pkg:/source/utils/Logging.bs"
|
||||||
import "pkg:/source/utils/StringUtils.bs"
|
import "pkg:/source/utils/StringUtils.bs"
|
||||||
import "pkg:/source/utils/Types.bs"
|
import "pkg:/source/utils/Types.bs"
|
||||||
|
import "pkg:/source/utils/UrlUtils.bs"
|
||||||
|
|
||||||
namespace HttpClient
|
namespace HttpClient
|
||||||
|
|
||||||
enum QueryParamArrayType
|
|
||||||
CommaSeparated = "CommaSeparated"
|
|
||||||
Repeated = "Repeated"
|
|
||||||
end enum
|
|
||||||
|
|
||||||
function Get(url as string) as HttpRequest
|
function Get(url as string) as HttpRequest
|
||||||
return (new HttpRequest()).Method("GET").Url(url)
|
return (new HttpRequest()).Method("GET").Url(url)
|
||||||
end function
|
end function
|
||||||
@@ -85,7 +81,7 @@ namespace HttpClient
|
|||||||
return m
|
return m
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function QueryParamArray(key as string, valueArray as dynamic, arrayType as HttpClient.QueryParamArrayType) as HttpRequest
|
function QueryParamArray(key as string, valueArray as dynamic, arrayType as UrlUtils.QueryParamArrayType) as HttpRequest
|
||||||
if valueArray = invalid
|
if valueArray = invalid
|
||||||
return m
|
return m
|
||||||
end if
|
end if
|
||||||
@@ -398,88 +394,12 @@ namespace HttpClient
|
|||||||
return urlTransfer
|
return urlTransfer
|
||||||
end function
|
end function
|
||||||
|
|
||||||
' TODO:P2 refactor this monstrosity of a function
|
|
||||||
function BuildUrl() as string
|
function BuildUrl() as string
|
||||||
if m._fullUrl <> invalid
|
if m._fullUrl <> invalid
|
||||||
return m._fullUrl
|
return m._fullUrl
|
||||||
end if
|
end if
|
||||||
|
|
||||||
url = m._url
|
m._fullUrl = UrlUtils.BuildUrl(m._url, m._pathParams, m._queryParams)
|
||||||
if m._pathParams <> invalid
|
|
||||||
for each key in m._pathParams
|
|
||||||
value = m._pathParams[key]
|
|
||||||
if value = invalid
|
|
||||||
continue for
|
|
||||||
end if
|
|
||||||
if not IsString(value)
|
|
||||||
value = `${value}`
|
|
||||||
end if
|
|
||||||
|
|
||||||
url = url.Replace(`{${key}}`, value.EncodeUriComponent())
|
|
||||||
end for
|
|
||||||
end if
|
|
||||||
if m._queryParams <> invalid
|
|
||||||
hasQueryParams = url.InStr("?") <> -1
|
|
||||||
for each key in m._queryParams
|
|
||||||
value = m._queryParams[key]
|
|
||||||
if value = invalid
|
|
||||||
continue for
|
|
||||||
end if
|
|
||||||
|
|
||||||
if not IsString(value)
|
|
||||||
if IsAssociativeArray(value) and IsArray(value.value)
|
|
||||||
if value.arrayType = HttpClient.QueryParamArrayType.Repeated
|
|
||||||
valueArray = value.value
|
|
||||||
for each item in valueArray
|
|
||||||
itemString = item
|
|
||||||
if not IsString(itemString)
|
|
||||||
itemString = `${itemString}`
|
|
||||||
end if
|
|
||||||
if hasQueryParams
|
|
||||||
url += "&" + key.EncodeUriComponent() + "=" + itemString.EncodeUriComponent()
|
|
||||||
else
|
|
||||||
url += "?" + key.EncodeUriComponent() + "=" + itemString.EncodeUriComponent()
|
|
||||||
hasQueryParams = true
|
|
||||||
end if
|
|
||||||
end for
|
|
||||||
else if value.arrayType = HttpClient.QueryParamArrayType.CommaSeparated
|
|
||||||
valueArray = value.value
|
|
||||||
for i = 0 to valueArray.Count() - 1
|
|
||||||
itemString = valueArray[i]
|
|
||||||
if not IsString(itemString)
|
|
||||||
itemString = `${itemString}`
|
|
||||||
end if
|
|
||||||
itemString = itemString.EncodeUriComponent()
|
|
||||||
valueArray[i] = itemString
|
|
||||||
end for
|
|
||||||
joined = valueArray.join(",")
|
|
||||||
if hasQueryParams
|
|
||||||
url += "&" + key.EncodeUriComponent() + "=" + joined
|
|
||||||
else
|
|
||||||
url += "?" + key.EncodeUriComponent() + "=" + joined
|
|
||||||
hasQueryParams = true
|
|
||||||
end if
|
|
||||||
end if
|
|
||||||
else
|
|
||||||
value = `${value}`
|
|
||||||
if hasQueryParams
|
|
||||||
url += "&" + key.EncodeUriComponent() + "=" + value.EncodeUriComponent()
|
|
||||||
else
|
|
||||||
url += "?" + key.EncodeUriComponent() + "=" + value.EncodeUriComponent()
|
|
||||||
hasQueryParams = true
|
|
||||||
end if
|
|
||||||
end if
|
|
||||||
else
|
|
||||||
if hasQueryParams
|
|
||||||
url += "&" + key.EncodeUriComponent() + "=" + value.EncodeUriComponent()
|
|
||||||
else
|
|
||||||
url += "?" + key.EncodeUriComponent() + "=" + value.EncodeUriComponent()
|
|
||||||
hasQueryParams = true
|
|
||||||
end if
|
|
||||||
end if
|
|
||||||
end for
|
|
||||||
end if
|
|
||||||
m._fullUrl = url
|
|
||||||
return m._fullUrl
|
return m._fullUrl
|
||||||
end function
|
end function
|
||||||
end class
|
end class
|
||||||
|
|||||||
@@ -0,0 +1,144 @@
|
|||||||
|
import "pkg:/source/utils/Types.bs"
|
||||||
|
|
||||||
|
namespace UrlUtils
|
||||||
|
enum QueryParamArrayType
|
||||||
|
CommaSeparated = "CommaSeparated"
|
||||||
|
Repeated = "Repeated"
|
||||||
|
end enum
|
||||||
|
|
||||||
|
function BuildUrl(url as string, pathParams as object, queryParams as object) as string
|
||||||
|
url = SetPathParams(url, pathParams)
|
||||||
|
url = SetQueryParams(url, queryParams)
|
||||||
|
return url
|
||||||
|
end function
|
||||||
|
|
||||||
|
function SetPathParams(url as string, pathParams as object) as string
|
||||||
|
if pathParams = invalid
|
||||||
|
return url
|
||||||
|
end if
|
||||||
|
|
||||||
|
for each key in pathParams
|
||||||
|
value = pathParams[key]
|
||||||
|
if value = invalid
|
||||||
|
continue for
|
||||||
|
end if
|
||||||
|
|
||||||
|
if not IsString(value)
|
||||||
|
value = `${value}`
|
||||||
|
end if
|
||||||
|
|
||||||
|
url = url.Replace(`{${key}}`, value.EncodeUriComponent())
|
||||||
|
end for
|
||||||
|
|
||||||
|
return url
|
||||||
|
end function
|
||||||
|
|
||||||
|
function SetQueryParams(url as string, queryParams as object) as string
|
||||||
|
if queryParams = invalid or queryParams.Count() = 0
|
||||||
|
return url
|
||||||
|
end if
|
||||||
|
|
||||||
|
hasQueryParams = url.InStr("?") <> -1
|
||||||
|
|
||||||
|
for each key in queryParams
|
||||||
|
value = queryParams[key]
|
||||||
|
if value = invalid
|
||||||
|
continue for
|
||||||
|
end if
|
||||||
|
|
||||||
|
if hasQueryParams
|
||||||
|
url += "&" + GetEncodedQueryParam(key, value)
|
||||||
|
else
|
||||||
|
url += "?" + GetEncodedQueryParam(key, value)
|
||||||
|
hasQueryParams = true
|
||||||
|
end if
|
||||||
|
end for
|
||||||
|
|
||||||
|
return url
|
||||||
|
end function
|
||||||
|
|
||||||
|
function GetEncodedQueryParam(key as string, value as dynamic) as string
|
||||||
|
if IsString(value)
|
||||||
|
return key.EncodeUriComponent() + "=" + value.EncodeUriComponent()
|
||||||
|
else if IsAssociativeArray(value) and IsArray(value.value)
|
||||||
|
return GetEncodedQueryParamArray(key, value)
|
||||||
|
else
|
||||||
|
value = `${value}`
|
||||||
|
return key.EncodeUriComponent() + "=" + value.EncodeUriComponent()
|
||||||
|
end if
|
||||||
|
end function
|
||||||
|
|
||||||
|
function GetEncodedQueryParamArray(key as string, value as object) as string
|
||||||
|
if value.arrayType = UrlUtils.QueryParamArrayType.Repeated
|
||||||
|
return GetEncodedQueryParamArrayRepeated(key, value)
|
||||||
|
else if value.arrayType = UrlUtils.QueryParamArrayType.CommaSeparated
|
||||||
|
return GetEncodedQueryParamArrayCommaSeparated(key, value)
|
||||||
|
else
|
||||||
|
throw "Invalid query param array type"
|
||||||
|
end if
|
||||||
|
end function
|
||||||
|
|
||||||
|
function GetEncodedQueryParamArrayRepeated(key as string, value as object) as string
|
||||||
|
repeatedParams = []
|
||||||
|
valueArray = value.value
|
||||||
|
key = key.EncodeUriComponent()
|
||||||
|
|
||||||
|
for each item in valueArray
|
||||||
|
if not IsString(item)
|
||||||
|
item = `${item}`
|
||||||
|
end if
|
||||||
|
|
||||||
|
repeatedParams.push(key + "=" + item.EncodeUriComponent())
|
||||||
|
end for
|
||||||
|
|
||||||
|
return repeatedParams.join("&")
|
||||||
|
end function
|
||||||
|
|
||||||
|
function GetEncodedQueryParamArrayCommaSeparated(key as string, value as object) as string
|
||||||
|
valueArray = value.value
|
||||||
|
key = key.EncodeUriComponent()
|
||||||
|
|
||||||
|
for i = 0 to valueArray.Count() - 1
|
||||||
|
item = valueArray[i]
|
||||||
|
if not IsString(item)
|
||||||
|
item = `${item}`
|
||||||
|
end if
|
||||||
|
|
||||||
|
valueArray[i] = item.EncodeUriComponent()
|
||||||
|
end for
|
||||||
|
|
||||||
|
return key + "=" + valueArray.join(",")
|
||||||
|
end function
|
||||||
|
|
||||||
|
function ParseQueryComponents(url as string) as object
|
||||||
|
query = {}
|
||||||
|
index = Instr(0, url, "?")
|
||||||
|
url = Mid(url, index + 1)
|
||||||
|
components = url.split("&")
|
||||||
|
for each component in components
|
||||||
|
key = component
|
||||||
|
value = ""
|
||||||
|
hasValue = false
|
||||||
|
if Instr(0, component, "=")
|
||||||
|
keyValue = component.split("=")
|
||||||
|
key = keyValue[0]
|
||||||
|
value = keyValue[1].DecodeUriComponent()
|
||||||
|
hasValue = true
|
||||||
|
end if
|
||||||
|
|
||||||
|
if query.DoesExist(key) and hasValue
|
||||||
|
oldValue = query[key]
|
||||||
|
if GetInterface(oldValue, "ifArray") = invalid
|
||||||
|
oldValue = [oldValue]
|
||||||
|
end if
|
||||||
|
oldValue.push(value.DecodeUriComponent())
|
||||||
|
value = oldValue
|
||||||
|
end if
|
||||||
|
|
||||||
|
if key <> ""
|
||||||
|
query[key] = value
|
||||||
|
end if
|
||||||
|
end for
|
||||||
|
return query
|
||||||
|
end function
|
||||||
|
end namespace
|
||||||
Reference in new issue
Block a user