Skip to content

Commit

Permalink
update put request
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianWhitneyAI committed Nov 18, 2024
1 parent 31d4abf commit 6abbb7c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
13 changes: 8 additions & 5 deletions packages/core/services/FileService/HttpFileService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface Config extends ConnectionConfig {
* Service responsible for fetching file related metadata.
*/
export default class HttpFileService extends HttpServiceBase implements FileService {
private static readonly CACHE_ENDPOINT_VERSION = "3.0";
private static readonly CACHE_ENDPOINT_VERSION = "v3.0";
private static readonly ENDPOINT_VERSION = "3.0";
public static readonly BASE_FILES_URL = `file-explorer-service/${HttpFileService.ENDPOINT_VERSION}/files`;
public static readonly BASE_FILE_COUNT_URL = `${HttpFileService.BASE_FILES_URL}/count`;
Expand Down Expand Up @@ -138,13 +138,16 @@ export default class HttpFileService extends HttpServiceBase implements FileServ
): Promise<{ cacheFileStatuses: { [fileId: string]: string } }> {
const requestUrl = `${this.aicsLoadBalancerBaseUrl}/${HttpFileService.BASE_FILE_CACHE_URL}${this.pathSuffix}`;
const requestBody = JSON.stringify({ fileIds });
const headers = {
"Content-Type": "application/json",
"X-User-Id": "brian.whitney", // TODO: Make this not my user
};

try {
const cacheStatuses = await this.rawPost<{
const cacheStatuses = await this.rawPut<{
cacheFileStatuses: { [fileId: string]: string };
}>(requestUrl, requestBody);

return cacheStatuses; // Return the entire response object
}>(requestUrl, requestBody, headers);
return cacheStatuses;
} catch (error) {
console.error("Failed to cache files:", error);
throw new Error("Unable to complete the caching request.");
Expand Down
26 changes: 26 additions & 0 deletions packages/core/services/HttpServiceBase/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,32 @@ export default class HttpServiceBase {
return response.data;
}

public async rawPut<T>(
url: string,
body: string,
headers: { [key: string]: string } = {}
): Promise<T> {
const encodedUrl = HttpServiceBase.encodeURI(url);
const config = { headers: { ...headers } };

let response;
try {
// Retry policy wrapped around axios PUT
response = await retry.execute(() => this.httpClient.put(encodedUrl, body, config));
} catch (err) {
if (axios.isAxiosError(err) && err?.response?.data?.message) {
throw new Error(JSON.stringify(err.response.data.message));
}
throw err;
}

if (response.status >= 400 || response.data === undefined) {
throw new Error(`Request for ${encodedUrl} failed`);
}

return response.data;
}

public async post<T>(url: string, body: string): Promise<RestServiceResponse<T>> {
const encodedUrl = HttpServiceBase.encodeURI(url);
const config = { headers: { "Content-Type": "application/json" } };
Expand Down

0 comments on commit 6abbb7c

Please sign in to comment.