-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.js
46 lines (40 loc) · 1.13 KB
/
query.js
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
const API_URL = 'http://192.168.1.22:8080';
const TOKEN_URL = `${API_URL}/token`;
const JOB_URL = `${API_URL}/job`;
async function getToken() {
const response = await fetch(TOKEN_URL);
const data = await response.json();
return data.token;
}
async function createJob(token, phrase) {
const response = await fetch(JOB_URL, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ phrase })
});
const data = await response.json();
return data.job_id;
}
async function getJobResult(token, jobId) {
const response = await fetch(`${JOB_URL}?job_id=${jobId}`, {
headers: {
'Authorization': `Bearer ${token}`
}
});
const data = await response.json();
return data;
}
async function run() {
try {
const token = await getToken();
const jobId = await createJob(token, 'Hello, world!');
const jobResult = await getJobResult(token, jobId);
console.log(jobResult);
} catch (error) {
console.error(error);
}
}
run();