Skip to content

Commit

Permalink
Encode query parameters in H5GroveProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
loichuder committed Aug 5, 2021
1 parent 6ea9269 commit e631c03
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions src/h5web/providers/h5grove/h5grove-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ import type {
H5GroveAttribute,
} from './models';
import { assertDataset } from '../../guards';
import { convertDtype, flattenValue, handleAxiosError } from '../utils';
import {
convertDtype,
flattenValue,
handleAxiosError,
encodeQueryParams,
} from '../utils';
import { buildEntityPath } from '../../utils';

export class H5GroveApi extends ProviderApi {
Expand Down Expand Up @@ -50,7 +55,7 @@ export class H5GroveApi extends ProviderApi {
const { data } = await handleAxiosError(
() =>
this.client.get<H5GroveEntityResponse>(
`/meta/?file=${this.filepath}&path=${path}`
`/meta/?${encodeQueryParams({ file: this.filepath, path })}`
),
404,
ProviderError.NotFound
Expand All @@ -69,7 +74,7 @@ export class H5GroveApi extends ProviderApi {
}

const { data } = await this.client.get<H5GroveAttrValuesResponse>(
`/attr/?file=${this.filepath}&path=${path}`
`/attr/?${encodeQueryParams({ file: this.filepath, path })}`
);

this.attrValuesCache.set(path, data);
Expand All @@ -79,11 +84,8 @@ export class H5GroveApi extends ProviderApi {
private async fetchData(
params: ValueRequestParams
): Promise<H5GroveDataResponse> {
const { path, selection = '' } = params;
const { data } = await this.cancellableFetchValue<H5GroveDataResponse>(
`/data/?file=${this.filepath}&path=${path}${
selection && `&selection=${selection}`
}`,
`/data/?${encodeQueryParams({ file: this.filepath, ...params })}`,
params
);
return data;
Expand Down
16 changes: 15 additions & 1 deletion src/h5web/providers/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Endianness, DTypeClass } from './models';
import { convertDtype } from './utils';
import { convertDtype, encodeQueryParams } from './utils';

describe('convertDtype', () => {
it('should convert integer dtypes', () => {
Expand Down Expand Up @@ -75,3 +75,17 @@ describe('convertDtype', () => {
expect(convertDtype('>notAType')).toEqual({ class: DTypeClass.Unknown });
});
});

describe('encodeQueryParams', () => {
it('should convert a record into a query string', () => {
expect(encodeQueryParams({ file: 'a_file.h5', format: 'json' })).toEqual(
'file=a_file.h5&format=json'
);
});

it('should encode special characters', () => {
expect(encodeQueryParams({ file: 'a+file.h5', path: '/' })).toEqual(
'file=a%2Bfile.h5&path=%2F'
);
});
});
4 changes: 4 additions & 0 deletions src/h5web/providers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,7 @@ export async function handleAxiosError<T>(
throw error;
}
}

export function encodeQueryParams(params: Record<string, string>) {
return new URLSearchParams(params).toString();
}

0 comments on commit e631c03

Please sign in to comment.