Skip to content

Commit

Permalink
refactor: add yaml/formdata helpers in api
Browse files Browse the repository at this point in the history
  • Loading branch information
arildm committed Sep 24, 2024
1 parent 6ad77ef commit 8eb6289
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
43 changes: 26 additions & 17 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,24 @@ import type {
CreateCorpusData,
ResourceInfoAllData,
ResourceInfoOneData,
JobState,
JobType,
ListExportsData,
AdminModeStatusData,
CreateMetadataData,
ProgressHandler,
JobStateMap,
} from "@/api/api.types";

/** Create a `text/yaml` file object with content */
const yamlAsFile = (filename: string, yaml: string): File =>
new File([yaml], filename, { type: "text/yaml" });

/** Create a form data object with one or more files under `"files[]"` */
function filesFormData(...files: File[]): FormData {
const formData = new FormData();
files.forEach((file) => formData.append("files[]", file));
return formData;
}

/** Handle an exception from an API call that may be encoded as Blob */
async function rethrowBlobError(error: any): Promise<never> {
if (error.response?.data instanceof Blob) {
Expand Down Expand Up @@ -94,9 +104,7 @@ class MinkApi {

/** @see https://ws.spraakbanken.gu.se/ws/mink/api-doc#tag/Manage-Config/operation/uploadconfig */
async uploadConfig(corpusId: string, config: string) {
const configFile = new File([config], "config.yaml", { type: "text/yaml" });
const formData = new FormData();
formData.append("files[]", configFile);
const formData = filesFormData(yamlAsFile("config.yaml", config));
const response = await this.axios.put<MinkResponse>(
"upload-config",
formData,
Expand All @@ -106,9 +114,13 @@ class MinkApi {
}

/** @see https://ws.spraakbanken.gu.se/ws/mink/api-doc#tag/Manage-Sources/operation/downloadsources */
async downloadSources(corpusId: string, filename: string, binary = false) {
async downloadSources<B extends boolean>(
corpusId: string,
filename: string,
binary: B,
) {
const response = await this.axios
.get<string | Blob>("download-sources", {
.get<B extends true ? Blob : string>("download-sources", {
params: { corpus_id: corpusId, file: filename, zip: false },
responseType: binary ? "blob" : "text",
})
Expand All @@ -130,8 +142,7 @@ class MinkApi {
files: File[],
onProgress?: ProgressHandler,
) {
const formData = new FormData();
files.forEach((file) => formData.append("files[]", file));
const formData = filesFormData(...files);
const response = await this.axios.put<MinkResponse>(
"upload-sources",
formData,
Expand Down Expand Up @@ -161,9 +172,7 @@ class MinkApi {

/** @see https://ws.spraakbanken.gu.se/ws/mink/api-doc#tag/Manage-Metadata/operation/uploadmetadatayaml */
async uploadMetadataYaml(resourceId: string, yaml: string) {
const file = new File([yaml], "metadata.yaml", { type: "text/yaml" });
const formData = new FormData();
formData.append("files[]", file);
const formData = filesFormData(yamlAsFile("metadata.yaml", yaml));
const response = await this.axios.put<MinkResponse>(
"upload-metadata-yaml",
formData,
Expand Down Expand Up @@ -210,11 +219,11 @@ class MinkApi {

/** @see https://ws.spraakbanken.gu.se/ws/mink/api-doc#tag/Process-Corpus/operation/abortjob */
async abortJob(corpusId: string) {
const response = await this.axios.post<
MinkResponse<Record<JobType, JobState>>
>("abort-job", null, {
params: { corpus_id: corpusId },
});
const response = await this.axios.post<MinkResponse<JobStateMap>>(
"abort-job",
null,
{ params: { corpus_id: corpusId } },
);
return response.data;
}

Expand Down
4 changes: 3 additions & 1 deletion src/api/api.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export type ResourceType = "corpus" | "metadata";
// There's more but we're not using everything.
export type CorpusStatus = {
current_process: JobType | null;
status: Record<JobType, JobState>;
status: JobStateMap;
warnings: string;
errors: string;
sparv_output: string;
Expand All @@ -107,6 +107,8 @@ export type CorpusStatus = {
progress: `${number}%` | "";
};

export type JobStateMap = Record<JobType, JobState>;

/** File metadata */
export type FileMeta = {
/** ISO 8601 date (with timezone offset) of last modification */
Expand Down

0 comments on commit 8eb6289

Please sign in to comment.