Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: using put method for files manifest requests #3694 #481

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import {
FileManifestType,
FILE_MANIFEST_TYPE,
} from "../../../../hooks/useFileManifest/common/entities";
import { useFileManifest } from "../../../../hooks/useFileManifest/useFileManifest";
import { useRequestFileManifest } from "../../../../hooks/useFileManifest/useRequestFileManifest";
import {
FileLocation,
useRequestFileLocation,
} from "../../../../hooks/useRequestFileLocation";
import { FileLocation } from "../../../../hooks/useRequestFileLocation";
import { FileManifestState } from "../../../../providers/fileManifestState";
import {
BULK_DOWNLOAD_EXECUTION_ENVIRONMENT,
Expand Down Expand Up @@ -52,8 +50,8 @@ export const DownloadCurlCommand = ({
const {
exploreState: { tabValue: entityList },
} = useExploreState();
const { requestParams, requestURL } = fileManifestState;
const { data, isLoading, run } = useRequestFileLocation(requestURL);
const { requestParams } = fileManifestState;
const { data, isLoading, run } = useFileManifest();
const curlCommand = getBulkDownloadCurlCommand(data, executionEnvironment);
return curlCommand ? (
<DownloadCurlCommandReady
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React, { ElementType } from "react";
import { Filters } from "../../../../common/entities";
import { useExportToTerraResponseURL } from "../../../../hooks/useExportToTerraResponseURL";
import { FileManifestType } from "../../../../hooks/useFileManifest/common/entities";
import { useFileManifest } from "../../../../hooks/useFileManifest/useFileManifest";
import { useRequestFileManifest } from "../../../../hooks/useFileManifest/useRequestFileManifest";
import { useRequestFileLocation } from "../../../../hooks/useRequestFileLocation";
import { FileManifestState } from "../../../../providers/fileManifestState";
import { FormFacet, ManifestDownloadFormat } from "../../common/entities";
import { ExportToTerraNotStarted } from "./components/ExportToTerraNotStarted/exportToTerraNotStarted";
Expand Down Expand Up @@ -34,8 +34,8 @@ export const ExportToTerra = ({
manifestDownloadFormats,
}: ExportToTerraProps): JSX.Element => {
useRequestFileManifest(manifestDownloadFormat, filters, fileSummaryFacetName);
const { requestParams, requestURL } = fileManifestState;
const { data, isLoading, run } = useRequestFileLocation(requestURL);
const { requestParams } = fileManifestState;
const { data, isLoading, run } = useFileManifest();
const exportURL = useExportToTerraResponseURL(requestParams, data);
return exportURL ? (
<ExportToTerraReady
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import React, { ElementType } from "react";
import { MANIFEST_DOWNLOAD_FORMAT } from "../../../../apis/azul/common/entities";
import { Filters } from "../../../../common/entities";
import { FileManifestType } from "../../../../hooks/useFileManifest/common/entities";
import { useFileManifest } from "../../../../hooks/useFileManifest/useFileManifest";
import { useRequestFileManifest } from "../../../../hooks/useFileManifest/useRequestFileManifest";
import {
FileLocation,
useRequestFileLocation,
} from "../../../../hooks/useRequestFileLocation";
import { FileLocation } from "../../../../hooks/useRequestFileLocation";
import { FileManifestState } from "../../../../providers/fileManifestState";
import { FormFacet } from "../../common/entities";
import { ManifestDownloadNotStarted } from "./components/ManifestDownloadNotStarted/manifestDownloadNotStarted";
Expand Down Expand Up @@ -37,8 +35,7 @@ export const ManifestDownload = ({
filters,
fileSummaryFacetName
);
const { requestURL } = fileManifestState;
const { data, isLoading, run } = useRequestFileLocation(requestURL);
const { data, isLoading, run } = useFileManifest();
const manifestURL = getManifestDownloadURL(data);
return manifestURL ? (
<ManifestDownloadReady
Expand Down
1 change: 0 additions & 1 deletion packages/data-explorer-ui/src/entity/api/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { api } from "./client";
function createFetchOptions(
accessToken: string | undefined
): AxiosRequestConfig {
// TODO https://github.com/clevercanary/data-browser/issues/545
return {
headers: accessToken ? { Authorization: "Bearer " + accessToken } : {},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useFileManifestState } from "../useFileManifestState";
import {
METHOD,
useRequestFileLocation,
UseRequestFileLocationResult,
} from "../useRequestFileLocation";

/**
* Requests file manifest.
* @param requestURL - Request URL.
* @returns file manifest request response, function and status.
*/
export const useFileManifest = (
requestURL?: string
): UseRequestFileLocationResult => {
const { fileManifestState } = useFileManifestState();
const url = requestURL || fileManifestState.requestURL;
return useRequestFileLocation(url, METHOD.PUT);
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { MANIFEST_DOWNLOAD_FORMAT } from "../../apis/azul/common/entities";
import { Filters } from "../../common/entities";
import { BULK_DOWNLOAD_EXECUTION_ENVIRONMENT } from "../../components/Export/common/entities";
import { useCatalog } from "../useCatalog";
import {
FileLocation,
useRequestFileLocation,
} from "../useRequestFileLocation";
import { FileLocation } from "../useRequestFileLocation";
import { buildFileManifestRequestURL } from "./common/buildFileManifestRequestURL";
import { useFileManifest } from "./useFileManifest";
import { useFileManifestURL } from "./useFileManifestURL";

export interface ManifestDownload {
Expand Down Expand Up @@ -35,7 +33,7 @@ export const useFileManifestDownload = (filters: Filters): ManifestDownload => {
catalog,
MANIFEST_DOWNLOAD_FORMAT.COMPACT
) || {};
const { data, isIdle, isLoading, run } = useRequestFileLocation(requestURL);
const { data, isIdle, isLoading, run } = useFileManifest(requestURL);
const manifestURL = getManifestDownloadURL(data);
const fileName = getManifestDownloadFileName(data);

Expand Down
34 changes: 26 additions & 8 deletions packages/data-explorer-ui/src/hooks/useRequestFileLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,45 @@ export interface UseRequestFileLocationResult {
run: () => void;
}

export type Method = METHOD;

export enum METHOD {
GET = "GET",
PUT = "PUT",
}

type ResolveFn = (file: FileLocation | PromiseLike<FileLocation>) => void;
type RejectFn = (reason: FileLocation) => void;

/**
* Returns fetch request options.
* @param accessToken - Access token.
* @param method - Method to be used by the request
* @returns fetch request options.
*/
function createFetchOptions(accessToken: string | undefined): RequestInit {
function createFetchOptions(
accessToken: string | undefined,
method: Method
): RequestInit {
return {
headers: accessToken ? { Authorization: "Bearer " + accessToken } : {},
method,
};
}

/**
* Function to make a get request and map the result to camelCase
* @param url - url for the get request
* @param accessToken - Access token.
* @param method - Method to be used by the request
* @returns @see FileLocation
*/
export const getFileLocation = async (
url: string,
accessToken: string | undefined
accessToken: string | undefined,
method: Method
): Promise<FileLocation> => {
const options = createFetchOptions(accessToken);
const options = createFetchOptions(accessToken, method);
const res = await fetch(url, options);
const jsonRes: FileLocationResponse = await res.json();
return {
Expand All @@ -65,17 +79,19 @@ export const getFileLocation = async (
* @param reject - function to reject the running promise
* @param active - Mutable object used to check if the page is still mounted and the requests should keep executing
* @param retryAfter - timeout value
* @param method - Method to be used by the request
*/
const scheduleFileLocation = (
url: string,
accessToken: string | undefined,
resolve: ResolveFn,
reject: RejectFn,
active: MutableRefObject<boolean>,
retryAfter = 0
retryAfter = 0,
method: Method = METHOD.GET
): void => {
setTimeout(() => {
getFileLocation(url, accessToken).then((result: FileLocation) => {
getFileLocation(url, accessToken, method).then((result: FileLocation) => {
if (result.status === FILE_LOCATION_PENDING) {
if (!active.current) {
reject({
Expand Down Expand Up @@ -104,10 +120,12 @@ const scheduleFileLocation = (
/**
* Hook to get a file location using a retry-after approach
* @param url - to be used on the get request
* @param method - Method to be used by the request
* @returns data object with the file location
*/
export const useRequestFileLocation = (
url?: string
url?: string,
method?: Method
): UseRequestFileLocationResult => {
// Grab token from authentication.
const { token } = useAuthentication();
Expand All @@ -131,11 +149,11 @@ export const useRequestFileLocation = (
if (url) {
runAsync(
new Promise<FileLocation>((resolve, reject) => {
scheduleFileLocation(url, token, resolve, reject, active);
scheduleFileLocation(url, token, resolve, reject, active, 0, method);
})
);
}
}, [runAsync, token, url]);
}, [runAsync, token, url, method]);

return { data, isIdle, isLoading, isSuccess, run };
};
Loading