feat(ws): Refactor restYAML to restFILE (#478)
Signed-off-by: Charles Thao <cthao@redhat.com>
This commit is contained in:
parent
0e90e5da65
commit
3e7f44a7ae
|
@ -80,7 +80,7 @@ export const WorkspaceKindForm: React.FC = () => {
|
||||||
// TODO: Complete handleCreate with API call to create a new WS kind
|
// TODO: Complete handleCreate with API call to create a new WS kind
|
||||||
try {
|
try {
|
||||||
if (mode === 'create') {
|
if (mode === 'create') {
|
||||||
const newWorkspaceKind = await api.createWorkspaceKind({}, yamlValue);
|
const newWorkspaceKind = await api.createWorkspaceKind({ directYAML: true }, yamlValue);
|
||||||
// TODO: alert user about success
|
// TODO: alert user about success
|
||||||
console.info('New workspace kind created:', JSON.stringify(newWorkspaceKind));
|
console.info('New workspace kind created:', JSON.stringify(newWorkspaceKind));
|
||||||
navigate('workspaceKinds');
|
navigate('workspaceKinds');
|
||||||
|
|
|
@ -19,6 +19,7 @@ export const mergeRequestInit = (
|
||||||
type CallRestJSONOptions = {
|
type CallRestJSONOptions = {
|
||||||
queryParams?: Record<string, unknown>;
|
queryParams?: Record<string, unknown>;
|
||||||
parseJSON?: boolean;
|
parseJSON?: boolean;
|
||||||
|
directYAML?: boolean;
|
||||||
} & EitherOrNone<
|
} & EitherOrNone<
|
||||||
{
|
{
|
||||||
fileContents: string;
|
fileContents: string;
|
||||||
|
@ -32,7 +33,7 @@ const callRestJSON = <T>(
|
||||||
host: string,
|
host: string,
|
||||||
path: string,
|
path: string,
|
||||||
requestInit: RequestInit,
|
requestInit: RequestInit,
|
||||||
{ data, fileContents, queryParams, parseJSON = true }: CallRestJSONOptions,
|
{ data, fileContents, queryParams, parseJSON = true, directYAML = false }: CallRestJSONOptions,
|
||||||
): Promise<T> => {
|
): Promise<T> => {
|
||||||
const { method, ...otherOptions } = requestInit;
|
const { method, ...otherOptions } = requestInit;
|
||||||
|
|
||||||
|
@ -54,12 +55,17 @@ const callRestJSON = <T>(
|
||||||
let contentType: string | undefined;
|
let contentType: string | undefined;
|
||||||
let formData: FormData | undefined;
|
let formData: FormData | undefined;
|
||||||
if (fileContents) {
|
if (fileContents) {
|
||||||
formData = new FormData();
|
if (directYAML) {
|
||||||
formData.append(
|
requestData = fileContents;
|
||||||
'uploadfile',
|
contentType = 'application/yaml';
|
||||||
new Blob([fileContents], { type: 'application/x-yaml' }),
|
} else {
|
||||||
'uploadedFile.yml',
|
formData = new FormData();
|
||||||
);
|
formData.append(
|
||||||
|
'uploadfile',
|
||||||
|
new Blob([fileContents], { type: 'application/x-yaml' }),
|
||||||
|
'uploadedFile.yml',
|
||||||
|
);
|
||||||
|
}
|
||||||
} else if (data) {
|
} else if (data) {
|
||||||
// It's OK for contentType and requestData to BOTH be undefined for e.g. a GET request or POST with no body.
|
// It's OK for contentType and requestData to BOTH be undefined for e.g. a GET request or POST with no body.
|
||||||
contentType = 'application/json;charset=UTF-8';
|
contentType = 'application/json;charset=UTF-8';
|
||||||
|
@ -122,6 +128,7 @@ export const restFILE = <T>(
|
||||||
fileContents,
|
fileContents,
|
||||||
queryParams,
|
queryParams,
|
||||||
parseJSON: options?.parseJSON,
|
parseJSON: options?.parseJSON,
|
||||||
|
directYAML: options?.directYAML,
|
||||||
});
|
});
|
||||||
|
|
||||||
/** POST -- but no body data -- targets simple endpoints */
|
/** POST -- but no body data -- targets simple endpoints */
|
||||||
|
@ -173,48 +180,6 @@ export const restDELETE = <T>(
|
||||||
parseJSON: options?.parseJSON,
|
parseJSON: options?.parseJSON,
|
||||||
});
|
});
|
||||||
|
|
||||||
/** POST -- but with YAML content directly in body */
|
|
||||||
export const restYAML = <T>(
|
|
||||||
host: string,
|
|
||||||
path: string,
|
|
||||||
yamlContent: string,
|
|
||||||
queryParams?: Record<string, unknown>,
|
|
||||||
options?: APIOptions,
|
|
||||||
): Promise<T> => {
|
|
||||||
const { method, ...otherOptions } = mergeRequestInit(options, { method: 'POST' });
|
|
||||||
|
|
||||||
const sanitizedQueryParams = queryParams
|
|
||||||
? Object.entries(queryParams).reduce((acc, [key, value]) => {
|
|
||||||
if (value) {
|
|
||||||
return { ...acc, [key]: value };
|
|
||||||
}
|
|
||||||
return acc;
|
|
||||||
}, {})
|
|
||||||
: null;
|
|
||||||
|
|
||||||
const searchParams = sanitizedQueryParams
|
|
||||||
? new URLSearchParams(sanitizedQueryParams).toString()
|
|
||||||
: null;
|
|
||||||
|
|
||||||
return fetch(`${host}${path}${searchParams ? `?${searchParams}` : ''}`, {
|
|
||||||
...otherOptions,
|
|
||||||
headers: {
|
|
||||||
...otherOptions.headers,
|
|
||||||
...(DEV_MODE && { [AUTH_HEADER]: localStorage.getItem(AUTH_HEADER) }),
|
|
||||||
'Content-Type': 'application/vnd.kubeflow-notebooks.manifest+yaml',
|
|
||||||
},
|
|
||||||
method,
|
|
||||||
body: yamlContent,
|
|
||||||
}).then((response) =>
|
|
||||||
response.text().then((fetchedData) => {
|
|
||||||
if (options?.parseJSON !== false) {
|
|
||||||
return JSON.parse(fetchedData);
|
|
||||||
}
|
|
||||||
return fetchedData;
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const isNotebookResponse = <T>(response: unknown): response is ResponseBody<T> => {
|
export const isNotebookResponse = <T>(response: unknown): response is ResponseBody<T> => {
|
||||||
if (typeof response === 'object' && response !== null) {
|
if (typeof response === 'object' && response !== null) {
|
||||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import {
|
import {
|
||||||
restCREATE,
|
restCREATE,
|
||||||
restDELETE,
|
restDELETE,
|
||||||
|
restFILE,
|
||||||
restGET,
|
restGET,
|
||||||
restPATCH,
|
restPATCH,
|
||||||
restUPDATE,
|
restUPDATE,
|
||||||
restYAML,
|
|
||||||
wrapRequest,
|
wrapRequest,
|
||||||
} from '~/shared/api/apiUtils';
|
} from '~/shared/api/apiUtils';
|
||||||
import {
|
import {
|
||||||
|
@ -72,7 +72,7 @@ export const getWorkspaceKind: GetWorkspaceKindAPI = (hostPath) => (opts, kind)
|
||||||
wrapRequest(restGET(hostPath, `/workspacekinds/${kind}`, {}, opts));
|
wrapRequest(restGET(hostPath, `/workspacekinds/${kind}`, {}, opts));
|
||||||
|
|
||||||
export const createWorkspaceKind: CreateWorkspaceKindAPI = (hostPath) => (opts, data) =>
|
export const createWorkspaceKind: CreateWorkspaceKindAPI = (hostPath) => (opts, data) =>
|
||||||
wrapRequest(restYAML(hostPath, `/workspacekinds`, data, {}, opts));
|
wrapRequest(restFILE(hostPath, `/workspacekinds`, data, {}, opts));
|
||||||
|
|
||||||
export const updateWorkspaceKind: UpdateWorkspaceKindAPI = (hostPath) => (opts, kind, data) =>
|
export const updateWorkspaceKind: UpdateWorkspaceKindAPI = (hostPath) => (opts, kind, data) =>
|
||||||
wrapRequest(restUPDATE(hostPath, `/workspacekinds/${kind}`, data, {}, opts));
|
wrapRequest(restUPDATE(hostPath, `/workspacekinds/${kind}`, data, {}, opts));
|
||||||
|
|
|
@ -3,6 +3,7 @@ export type APIOptions = {
|
||||||
signal?: AbortSignal;
|
signal?: AbortSignal;
|
||||||
parseJSON?: boolean;
|
parseJSON?: boolean;
|
||||||
headers?: Record<string, string>;
|
headers?: Record<string, string>;
|
||||||
|
directYAML?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type APIState<T> = {
|
export type APIState<T> = {
|
||||||
|
|
Loading…
Reference in New Issue