Skip to content

Commit

Permalink
Fix request body compression
Browse files Browse the repository at this point in the history
- Url params are now omitted from the url passed to the api client
- This was breaking some startsWith logic for requests that we compress
based on both url params and the uri
- To fix this, we now match against both the url string and the param
object
  • Loading branch information
Luke-Sikina committed May 19, 2022
1 parent 7fed092 commit 30962be
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,24 @@ function cachePostMethods(
);
}

type UrlParamPair = {
url: string;
params: { [key: string]: string };
};

function pairMatchesPath(
pair: UrlParamPair,
url: string,
params: any
): boolean {
return (
url.startsWith(pair.url) &&
Object.keys(pair.params).filter(
k => params[k] && params[k] === pair.params[k]
).length == Object.keys(pair.params).length
);
}

export function initializeAPIClients() {
// we need to set the domain of our api clients
(client as any).domain = getCbioPortalApiUrl();
Expand Down Expand Up @@ -187,12 +205,18 @@ export function initializeAPIClients() {
compressRequestBodies(
CBioPortalAPI,
[
'/mutations/fetch',
'/patients/fetch',
'/molecular-data/fetch',
'/clinical-data/fetch?clinicalDataType=SAMPLE',
'/gene-panel-data/fetch',
'/clinical-data/fetch?clinicalDataType=PATIENT',
{ url: '/mutations/fetch', params: {} },
{ url: '/patients/fetch', params: {} },
{ url: '/molecular-data/fetch', params: {} },
{
url: '/clinical-data/fetch',
params: { clinicalDataType: 'SAMPLE' },
},
{ url: '/gene-panel-data/fetch', params: {} },
{
url: '/clinical-data/fetch',
params: { clinicalDataType: 'PATIENT' },
},
],
getCbioPortalApiUrl()
);
Expand All @@ -209,10 +233,15 @@ export function initializeAPIClients() {
*/
function compressRequestBodies(
apiClient: any,
urlsToCompress: string[],
urlsToCompress: UrlParamPair[],
domain: string
): void {
urlsToCompress = urlsToCompress.map(url => domain + url);
urlsToCompress = urlsToCompress.map(pair => {
return {
url: domain + pair.url,
params: pair.params,
};
});

const oldRequestFunc = apiClient.prototype.request;

Expand All @@ -229,7 +258,9 @@ function compressRequestBodies(
) => {
if (
method === 'POST' &&
urlsToCompress.filter(match => url.startsWith(match)).length > 0
urlsToCompress.filter(pair =>
pairMatchesPath(pair, url, queryParameters)
).length > 0
) {
headers['Content-Encoding'] = 'gzip';
body = pako.gzip(JSON.stringify(body)).buffer;
Expand Down

0 comments on commit 30962be

Please sign in to comment.