From 0acb7031d3a08af6693dc82a4c1c674f9134f048 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Sun, 26 Nov 2023 18:11:48 -0500 Subject: [PATCH] Refactor query params --- .../Screens/SearchScreen/SearchScreen.bs | 3 +- .../components/Services/Dash/DashManifest.bs | 3 +- .../Web/WebServer/Http/HttpRequest.bs | 3 +- .../Web/WebServer/Http/HttpUtils.bs | 51 ++----- playlet-lib/src/source/services/HttpClient.bs | 86 +---------- playlet-lib/src/source/utils/UrlUtils.bs | 144 ++++++++++++++++++ 6 files changed, 162 insertions(+), 128 deletions(-) create mode 100644 playlet-lib/src/source/utils/UrlUtils.bs diff --git a/playlet-lib/src/components/Screens/SearchScreen/SearchScreen.bs b/playlet-lib/src/components/Screens/SearchScreen/SearchScreen.bs index 63e321ee..7fb7fe57 100644 --- a/playlet-lib/src/components/Screens/SearchScreen/SearchScreen.bs +++ b/playlet-lib/src/components/Screens/SearchScreen/SearchScreen.bs @@ -6,6 +6,7 @@ import "pkg:/source/utils/ErrorUtils.bs" import "pkg:/source/utils/FocusManagement.bs" import "pkg:/source/utils/LoadingScreen.bs" import "pkg:/source/utils/Logging.bs" +import "pkg:/source/utils/UrlUtils.bs" function Init() m.top.focusable = true @@ -222,7 +223,7 @@ function Search(text as string) else if IsArray(value) and value.Count() > 0 feedSource.queryParams[filter] = { value: value, - arrayType: HttpClient.QueryParamArrayType.CommaSeparated + arrayType: UrlUtils.QueryParamArrayType.CommaSeparated } end if end for diff --git a/playlet-lib/src/components/Services/Dash/DashManifest.bs b/playlet-lib/src/components/Services/Dash/DashManifest.bs index bd1d4532..58c3de5d 100644 --- a/playlet-lib/src/components/Services/Dash/DashManifest.bs +++ b/playlet-lib/src/components/Services/Dash/DashManifest.bs @@ -1,6 +1,7 @@ import "pkg:/components/Web/WebServer/Http/HttpUtils.bs" import "pkg:/source/services/XmlObject.bs" import "pkg:/source/utils/StringUtils.bs" +import "pkg:/source/utils/UrlUtils.bs" class DashManifest @@ -349,7 +350,7 @@ class DashManifest function ParseAudioLanguage(url as string) as object result = { lang: "", acont: "" } - queryParams = m.utils.ParseUrlQueryComponents(url) + queryParams = UrlUtils.ParseQueryComponents(url) if StringUtils.IsNullOrEmpty(queryParams["xtags"]) return result end if diff --git a/playlet-lib/src/components/Web/WebServer/Http/HttpRequest.bs b/playlet-lib/src/components/Web/WebServer/Http/HttpRequest.bs index 0089745d..c2c8f608 100644 --- a/playlet-lib/src/components/Web/WebServer/Http/HttpRequest.bs +++ b/playlet-lib/src/components/Web/WebServer/Http/HttpRequest.bs @@ -1,6 +1,7 @@ import "HttpRequestState.bs" import "pkg:/components/Web/WebServer/Http/HttpUtils.bs" import "pkg:/source/utils/Types.bs" +import "pkg:/source/utils/UrlUtils.bs" namespace Http @@ -202,7 +203,7 @@ namespace Http split = m.route.Instr("?") if split <> -1 m.route = m.route.Left(split) - m.query = utils.ParseUrlQueryComponents(m.uri) + m.query = UrlUtils.ParseQueryComponents(m.uri) else m.query = {} end if diff --git a/playlet-lib/src/components/Web/WebServer/Http/HttpUtils.bs b/playlet-lib/src/components/Web/WebServer/Http/HttpUtils.bs index 6d97a72c..7c1748f2 100644 --- a/playlet-lib/src/components/Web/WebServer/Http/HttpUtils.bs +++ b/playlet-lib/src/components/Web/WebServer/Http/HttpUtils.bs @@ -3,39 +3,6 @@ import "pkg:/source/utils/Types.bs" namespace Http 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 map = m.MimeTypes if map = invalid @@ -50,22 +17,22 @@ namespace Http end if end function - function GetExtension(fn as string) as string - l = fn.tokenize(".") - if l.count() > 0 - return l.GetTail() + function GetExtension(filename as string) as string + parts = filename.tokenize(".") + if parts.count() > 0 + return parts.GetTail() else return "" end if end function function HttpTitle(code = 0 as integer) as string - hcm = m.HttpTitles - if hcm = invalid - hcm = ParseJson(ReadAsciiFile("libpkg:/config/http-codes.json5")) - m.HttpTitles = hcm + map = m.httpCodesMap + if map = invalid + map = ParseJson(ReadAsciiFile("libpkg:/config/http-codes.json5")) + m.httpCodesMap = map end if - return ValidString(hcm[`${code}`]) + return ValidString(map[`${code}`]) end function end class diff --git a/playlet-lib/src/source/services/HttpClient.bs b/playlet-lib/src/source/services/HttpClient.bs index 5ec28d57..bc32fb78 100644 --- a/playlet-lib/src/source/services/HttpClient.bs +++ b/playlet-lib/src/source/services/HttpClient.bs @@ -4,14 +4,10 @@ import "pkg:/source/utils/CryptoUtils.bs" import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/Types.bs" +import "pkg:/source/utils/UrlUtils.bs" namespace HttpClient - enum QueryParamArrayType - CommaSeparated = "CommaSeparated" - Repeated = "Repeated" - end enum - function Get(url as string) as HttpRequest return (new HttpRequest()).Method("GET").Url(url) end function @@ -85,7 +81,7 @@ namespace HttpClient return m 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 return m end if @@ -398,88 +394,12 @@ namespace HttpClient return urlTransfer end function - ' TODO:P2 refactor this monstrosity of a function function BuildUrl() as string if m._fullUrl <> invalid return m._fullUrl end if - url = m._url - 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 + m._fullUrl = UrlUtils.BuildUrl(m._url, m._pathParams, m._queryParams) return m._fullUrl end function end class diff --git a/playlet-lib/src/source/utils/UrlUtils.bs b/playlet-lib/src/source/utils/UrlUtils.bs new file mode 100644 index 00000000..4c387593 --- /dev/null +++ b/playlet-lib/src/source/utils/UrlUtils.bs @@ -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