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
feedSource.queryParams[filter] = value
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] = {
value: value,
__arrayType: HttpClient.QueryParamArrayType.CommaSeparated
arrayType: HttpClient.QueryParamArrayType.CommaSeparated
}
end if
end for
@@ -98,11 +98,7 @@ namespace Invidious
request = HttpClient.Get(url)
request.QueryParams(m.DefaultQueryParams())
request.QueryParam("q", q)
' 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.CacheSeconds(60 * 60 * 24)
request.Cancellation(cancellation)
@@ -116,7 +112,11 @@ namespace Invidious
request = HttpClient.Get(url)
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)
return request.Await()
@@ -232,12 +232,8 @@ namespace Invidious
else
request.QueryParam(queryParamKey, queryParam.default)
end if
else if IsArray(queryParam.default)
if queryParam.arrayType = "repeat"
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 if IsArray(queryParam.default) and queryParam.arrayType <> invalid
request.QueryParamArray(queryParamKey, queryParam.default, queryParam.arrayType)
else
request.QueryParam(queryParamKey, queryParam.default)
end if
@@ -13,7 +13,7 @@ trending:
default: GetUserCountryCode
fields:
type: []
arrayType: "comma-separated"
arrayType: "CommaSeparated"
default:
- title
- videoId
@@ -33,7 +33,7 @@ popular:
queryParams:
fields:
type: []
arrayType: "comma-separated"
arrayType: "CommaSeparated"
default:
- title
- videoId
@@ -77,7 +77,7 @@ search:
- playlist
- channel
- all
arrayType: comma-separated
arrayType: CommaSeparated
default:
- video
- playlist
@@ -94,7 +94,7 @@ search:
- 360
- location
- hdr
arrayType: comma-separated
arrayType: CommaSeparated
region:
type: "#ISO3166"
default: GetUserCountryCode
@@ -8,8 +8,8 @@ import "pkg:/source/utils/Types.bs"
namespace HttpClient
enum QueryParamArrayType
CommaSeparated
Repeated
CommaSeparated = "CommaSeparated"
Repeated = "Repeated"
end enum
function Get(url as string) as HttpRequest
@@ -95,7 +95,7 @@ namespace HttpClient
m._queryParams[key] = {
value: valueArray,
__arrayType: arrayType
arrayType: arrayType
}
return m
@@ -423,7 +423,7 @@ namespace HttpClient
if not IsString(value)
if IsAssociativeArray(value) and IsArray(value.value)
if value.__arrayType = HttpClient.QueryParamArrayType.Repeated
if value.arrayType = HttpClient.QueryParamArrayType.Repeated
valueArray = value.value
for each item in valueArray
itemString = item
@@ -437,7 +437,7 @@ namespace HttpClient
hasQueryParams = true
end if
end for
else if value.__arrayType = HttpClient.QueryParamArrayType.CommaSeparated
else if value.arrayType = HttpClient.QueryParamArrayType.CommaSeparated
valueArray = value.value
for i = 0 to valueArray.Count() - 1
itemString = valueArray[i]
+4 -3
View File
@@ -23,11 +23,12 @@ export class InvidiousApi {
}
public async searchSuggestions(query: string) {
const response = await fetch(`${this.instance}/api/v1/search/suggestions?q=${encodeURIComponent(query)}&region=${this.userCountryCode}`);
return await response.json();
const url = `${this.instance}/api/v1/search/suggestions?q=${encodeURIComponent(query)}&region=${this.userCountryCode}`;
return await this.cachedFetch(url, 60 * 60 * 24);
}
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}`;
for (let filter in filters) {
if (typeof filters[filter] === 'string') {
@@ -93,6 +94,7 @@ export class InvidiousApi {
return !!(this.instance && this.endpoints && Object.keys(this.endpoints).length);
}
// TODO:P1 handle QueryParamArrayType (CommaSeparated/Repeated)
public async makeRequest(feedSource: any) {
if (!feedSource || !this.instance || !this.endpoints) {
return null;
@@ -237,7 +239,6 @@ export class InvidiousApi {
}
}
try {
tryCount -= 1;
const response = await fetch(url);