Small refactors to http client and caching

This commit is contained in:
Brahim Hadriche committed 2023-11-19 19:05:46 -05:00
1 parent 61327401b0
commit 9ae8601a32
5 files changed
+22 -27

No files matched your search

@@ -220,11 +220,9 @@ function Search(text as string)
if IsString(value) and value.len() > 0 if IsString(value) and value.len() > 0
feedSource.queryParams[filter] = value feedSource.queryParams[filter] = value
else if IsArray(value) and value.Count() > 0 else if IsArray(value) and value.Count() > 0
' TODO:P1 this is http client implementation details leaking
' We need a declarative way to set array types
feedSource.queryParams[filter] = { feedSource.queryParams[filter] = {
value: value, value: value,
__arrayType: HttpClient.QueryParamArrayType.CommaSeparated arrayType: HttpClient.QueryParamArrayType.CommaSeparated
} }
end if end if
end for end for
@@ -98,11 +98,7 @@ namespace Invidious
request = HttpClient.Get(url) request = HttpClient.Get(url)
request.QueryParams(m.DefaultQueryParams()) request.QueryParams(m.DefaultQueryParams())
request.QueryParam("q", q) request.QueryParam("q", q)
' 5 hours cache. Videos should expire in 6 hours, but since we don't request.CacheSeconds(60 * 60 * 24)
' have the exact expiration time, we'll use 5 hours. Invidious currently
' Does a fresh fetch if the cache is older than 10 minutes.
' https://github.com/iv-org/invidious/blob/069e91d2a609b1df94bdfbb693335969b2a281d8/src/invidious/videos.cr#L349
request.CacheSeconds(60 * 60 * 5)
request.Cancellation(cancellation) request.Cancellation(cancellation)
@@ -116,7 +112,11 @@ namespace Invidious
request = HttpClient.Get(url) request = HttpClient.Get(url)
request.QueryParams(m.DefaultQueryParams()) request.QueryParams(m.DefaultQueryParams())
request.CacheSeconds(60 * 60 * 6)' 6 hours ' 5 hours cache. Videos should expire in 6 hours, but since we don't
' have the exact expiration time, we'll use 5 hours. Invidious currently
' Does a fresh fetch if the cache is older than 10 minutes.
' https://github.com/iv-org/invidious/blob/069e91d2a609b1df94bdfbb693335969b2a281d8/src/invidious/videos.cr#L349
request.CacheSeconds(60 * 60 * 5)
request.Cancellation(cancellation) request.Cancellation(cancellation)
return request.Await() return request.Await()
@@ -232,12 +232,8 @@ namespace Invidious
else else
request.QueryParam(queryParamKey, queryParam.default) request.QueryParam(queryParamKey, queryParam.default)
end if end if
else if IsArray(queryParam.default) else if IsArray(queryParam.default) and queryParam.arrayType <> invalid
if queryParam.arrayType = "repeat" request.QueryParamArray(queryParamKey, queryParam.default, queryParam.arrayType)
request.QueryParamArray(queryParamKey, queryParam.default, HttpClient.QueryParamArrayType.Repeated)
else if queryParam.arrayType = "comma-separated"
request.QueryParamArray(queryParamKey, queryParam.default, HttpClient.QueryParamArrayType.CommaSeparated)
end if
else else
request.QueryParam(queryParamKey, queryParam.default) request.QueryParam(queryParamKey, queryParam.default)
end if end if
@@ -13,7 +13,7 @@ trending:
default: GetUserCountryCode default: GetUserCountryCode
fields: fields:
type: [] type: []
arrayType: "comma-separated" arrayType: "CommaSeparated"
default: default:
- title - title
- videoId - videoId
@@ -33,7 +33,7 @@ popular:
queryParams: queryParams:
fields: fields:
type: [] type: []
arrayType: "comma-separated" arrayType: "CommaSeparated"
default: default:
- title - title
- videoId - videoId
@@ -77,7 +77,7 @@ search:
- playlist - playlist
- channel - channel
- all - all
arrayType: comma-separated arrayType: CommaSeparated
default: default:
- video - video
- playlist - playlist
@@ -94,7 +94,7 @@ search:
- 360 - 360
- location - location
- hdr - hdr
arrayType: comma-separated arrayType: CommaSeparated
region: region:
type: "#ISO3166" type: "#ISO3166"
default: GetUserCountryCode default: GetUserCountryCode
@@ -8,8 +8,8 @@ import "pkg:/source/utils/Types.bs"
namespace HttpClient namespace HttpClient
enum QueryParamArrayType enum QueryParamArrayType
CommaSeparated CommaSeparated = "CommaSeparated"
Repeated Repeated = "Repeated"
end enum end enum
function Get(url as string) as HttpRequest function Get(url as string) as HttpRequest
@@ -95,7 +95,7 @@ namespace HttpClient
m._queryParams[key] = { m._queryParams[key] = {
value: valueArray, value: valueArray,
__arrayType: arrayType arrayType: arrayType
} }
return m return m
@@ -423,7 +423,7 @@ namespace HttpClient
if not IsString(value) if not IsString(value)
if IsAssociativeArray(value) and IsArray(value.value) if IsAssociativeArray(value) and IsArray(value.value)
if value.__arrayType = HttpClient.QueryParamArrayType.Repeated if value.arrayType = HttpClient.QueryParamArrayType.Repeated
valueArray = value.value valueArray = value.value
for each item in valueArray for each item in valueArray
itemString = item itemString = item
@@ -437,7 +437,7 @@ namespace HttpClient
hasQueryParams = true hasQueryParams = true
end if end if
end for end for
else if value.__arrayType = HttpClient.QueryParamArrayType.CommaSeparated else if value.arrayType = HttpClient.QueryParamArrayType.CommaSeparated
valueArray = value.value valueArray = value.value
for i = 0 to valueArray.Count() - 1 for i = 0 to valueArray.Count() - 1
itemString = valueArray[i] itemString = valueArray[i]
+4 -3
View File
@@ -23,11 +23,12 @@ export class InvidiousApi {
} }
public async searchSuggestions(query: string) { public async searchSuggestions(query: string) {
const response = await fetch(`${this.instance}/api/v1/search/suggestions?q=${encodeURIComponent(query)}&region=${this.userCountryCode}`); const url = `${this.instance}/api/v1/search/suggestions?q=${encodeURIComponent(query)}&region=${this.userCountryCode}`;
return await response.json(); return await this.cachedFetch(url, 60 * 60 * 24);
} }
public async search(query: string, filters: any, page: number = 1) { public async search(query: string, filters: any, page: number = 1) {
// TODO:P2 use a FeedSource similar to brightscript version
let url = `${this.instance}/api/v1/search?q=${encodeURIComponent(query)}&region=${this.userCountryCode}`; let url = `${this.instance}/api/v1/search?q=${encodeURIComponent(query)}&region=${this.userCountryCode}`;
for (let filter in filters) { for (let filter in filters) {
if (typeof filters[filter] === 'string') { if (typeof filters[filter] === 'string') {
@@ -93,6 +94,7 @@ export class InvidiousApi {
return !!(this.instance && this.endpoints && Object.keys(this.endpoints).length); return !!(this.instance && this.endpoints && Object.keys(this.endpoints).length);
} }
// TODO:P1 handle QueryParamArrayType (CommaSeparated/Repeated)
public async makeRequest(feedSource: any) { public async makeRequest(feedSource: any) {
if (!feedSource || !this.instance || !this.endpoints) { if (!feedSource || !this.instance || !this.endpoints) {
return null; return null;
@@ -237,7 +239,6 @@ export class InvidiousApi {
} }
} }
try { try {
tryCount -= 1; tryCount -= 1;
const response = await fetch(url); const response = await fetch(url);