Skip to content

Commit

Permalink
Fix query param encoding issue and optimize some api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
floogulinc committed Jun 21, 2024
1 parent df6265a commit 9d0967c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 17 deletions.
58 changes: 51 additions & 7 deletions src/app/hydrus-api.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { map, Observable } from 'rxjs';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { HttpClient, HttpParams, HttpHeaders, HttpParameterCodec } from '@angular/common/http';
import { HydrusSortType } from './hydrus-sort';
import { HydrusBasicFileFromAPI, HydrusFileFromAPI } from './hydrus-file';
import { HydrusSearchTags, HydrusTagSearchTag, ServiceNamesOrKeysToActionsToTags, ServiceNamesOrKeysToTags, TagDisplayType, TagsToServiceKeysToSiblingsAndParents } from './hydrus-tags';
Expand All @@ -16,11 +16,50 @@ import { HydrusJobStatus, HydrusJobStatusAddRequest, HydrusJobStatusUpdateReques
import { HydrusPage, HydrusPageListItem } from './hydrus-page';
import { HydrusClientOptions } from './hydrus-client-options';

type AngularHttpParams = HttpParams | {
type AngularHttpParams = {
[param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>;
}

/* eslint-disable @typescript-eslint/naming-convention, no-underscore-dangle, id-blacklist, id-match */

class CustomHttpUrlEncodingCodec implements HttpParameterCodec {
/**
* Encodes a key name for a URL parameter or query-string.
* @param key The key name.
* @returns The encoded key name.
*/
encodeKey(key: string): string {
return encodeURIComponent(key);
}

/**
* Encodes the value of a URL parameter or query-string.
* @param value The value.
* @returns The encoded value.
*/
encodeValue(value: string): string {
return encodeURIComponent(value);
}

/**
* Decodes an encoded URL parameter or query-string key.
* @param key The encoded key name.
* @returns The decoded key name.
*/
decodeKey(key: string): string {
return decodeURIComponent(key);
}

/**
* Decodes an encoded URL parameter or query-string value.
* @param value The encoded value.
* @returns The decoded value.
*/
decodeValue(value: string) {
return decodeURIComponent(value);
}
}



@Injectable({
providedIn: 'root'
Expand Down Expand Up @@ -58,8 +97,11 @@ export class HydrusApiService {
const cacheHeaders: Record<string, string> = noCache ? {
'Cache-Control': 'no-cache'
} : { };

const requestParams = new HttpParams({fromObject: params, encoder: new CustomHttpUrlEncodingCodec()});

return this.http.get<T & Partial<HydrusVersionResponse>>(this.getAPIUrl() + path, {
params,
params: requestParams,
headers: {...this.headers, ...cacheHeaders}
});
}
Expand Down Expand Up @@ -271,8 +313,10 @@ export class HydrusApiService {
* @param simple true or false (optional, defaulting to true)
*/
public getPageInfo(page_key: string, simple?: string) {
let httpParams: HttpParams = new HttpParams().set('page_key', page_key);
if (simple) { httpParams = httpParams.set('simple', simple); }
let httpParams: AngularHttpParams = {page_key}
if (simple) {
httpParams = {...httpParams, simple}
}
return this.apiGet<{page_info: HydrusPage}>('manage_pages/get_page_info', httpParams, true);
}

Expand Down Expand Up @@ -336,7 +380,7 @@ export class HydrusApiService {
tag_service_name?: string,
tag_display_type?: TagDisplayType
}) {
return this.apiGet<{tags: HydrusTagSearchTag[]}>('add_tags/search_tags', new HttpParams({fromObject: params}));
return this.apiGet<{tags: HydrusTagSearchTag[]}>('add_tags/search_tags', params);
}


Expand Down
21 changes: 11 additions & 10 deletions src/app/hydrus-files.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Injectable } from '@angular/core';
import { HydrusBasicFile, HydrusBasicFileFromAPI, HydrusFile, HydrusFileFromAPI, FileCategory, generateRatingsArray, getFileCategory } from './hydrus-file';
import { Observable, of, forkJoin } from 'rxjs';
import { Observable, of, forkJoin, from } from 'rxjs';
import { HydrusApiService } from './hydrus-api.service';
import { map, switchMap, tap } from 'rxjs/operators';
import { map, mergeMap, switchMap, tap, toArray } from 'rxjs/operators';
import { HydrusServices } from './hydrus-services';
import { HydrusFiletype, filetypeFromMime, hasThumbnail, isFileHydrusRenderable, mime_string_lookup } from './hydrus-file-mimes';

Expand Down Expand Up @@ -101,12 +101,6 @@ export class HydrusFilesService {
});
}

private getFileMetadataChunked(fileIds: number[]) {
return forkJoin(chunk(fileIds, QUERY_CHUNK_SIZE).map(ids => this.getAndProcessFileMetadataChunk(ids))).pipe(
map(files => files.flat())
);
}

private getAndProcessFileMetadataChunk(fileIds: number[]) {
return this.getBasicFileMetadataAPI(fileIds).pipe(
// fix for bug in hydrus v556
Expand All @@ -129,11 +123,18 @@ export class HydrusFilesService {
)
}

private getAndAddMetadata(fileIds: number[]): Observable<HydrusBasicFile[]> {
private getAndAddMetadata(fileIds: number[], concurrency = 8): Observable<HydrusBasicFile[]> {
if (fileIds.length === 0) {
return of([]);
} else if (fileIds.length <= QUERY_CHUNK_SIZE) {
return this.getAndProcessFileMetadataChunk(fileIds);
}
return this.getFileMetadataChunked(fileIds);

return from(chunk(fileIds, QUERY_CHUNK_SIZE)).pipe(
mergeMap(ids => this.getAndProcessFileMetadataChunk(ids), concurrency),
toArray(),
map(arr => arr.flat())
);
}

private addFilesAndTags(files: HydrusBasicFile[]) {
Expand Down

0 comments on commit 9d0967c

Please sign in to comment.