File size: 2,485 Bytes
d69ace5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b09d2d
d69ace5
 
 
4b09d2d
 
 
 
 
 
 
d69ace5
 
 
 
 
 
 
 
 
 
1a28176
 
4b09d2d
 
 
 
 
d69ace5
 
 
 
 
 
 
 
4b09d2d
 
d69ace5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Thin API client. Same-origin in production (FastAPI serves the build);
// the Vite dev server proxies /api to :8000.

async function request(path, options = {}) {
  const resp = await fetch(path, options);
  if (!resp.ok) {
    let detail = resp.statusText;
    try {
      const body = await resp.json();
      detail = body.detail || detail;
    } catch {
      /* non-JSON error body */
    }
    throw new Error(detail);
  }
  return resp.json();
}

const json = (method, body) => ({
  method,
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify(body),
});

export const api = {
  health: () => request("/api/health"),
  models: () => request("/api/models"),
  languages: () => request("/api/languages"),

  listProjects: () => request("/api/projects"),
  createProject: (body) => request("/api/projects", json("POST", body)),
  patchProject: (projectId, body) => request(`/api/projects/${projectId}`, json("PATCH", body)),
  deleteProject: (projectId) => fetch(`/api/projects/${projectId}`, { method: "DELETE" }),

  authMe: () => request("/api/auth/me"),
  register: (body) => request("/api/auth/register", json("POST", body)),
  login: (body) => request("/api/auth/login", json("POST", body)),
  logout: () => request("/api/auth/logout", { method: "POST" }),

  listCorpora: (projectId) => request(`/api/projects/${projectId}/corpora`),
  uploadCorpus: (projectId, file) => {
    const form = new FormData();
    form.append("file", file);
    return request(`/api/projects/${projectId}/corpora`, { method: "POST", body: form });
  },

  listConstructs: () => request("/api/constructs"),
  createConstruct: (body) => request("/api/constructs", json("POST", body)),
  generateConstructItems: (body) =>
    request("/api/constructs/generate-items", json("POST", body)),
  parseConstructFile: (file) => {
    const form = new FormData();
    form.append("file", file);
    return request("/api/constructs/parse-file", { method: "POST", body: form });
  },

  createJob: (body) => request("/api/jobs", json("POST", body)),
  listJobs: (projectId) => request(`/api/jobs?project_id=${projectId}`),
  getJob: (jobId) => request(`/api/jobs/${jobId}`),
  jobResults: (jobId) => request(`/api/jobs/${jobId}/results`),

  exportUrl: (jobId) => `/api/jobs/${jobId}/export`,
  metadataUrl: (jobId) => `/api/jobs/${jobId}/metadata`,
  scriptUrl: (jobId) => `/api/jobs/${jobId}/script`,
  scriptRequirementsUrl: (jobId) => `/api/jobs/${jobId}/script-requirements`,
};