-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock-visitor.mjs
executable file
·78 lines (66 loc) · 2.3 KB
/
mock-visitor.mjs
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
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env node
// Deposit a mock interaction and wait for all updates until token is received
import { readFileSync } from 'fs';
import { sleep, rndint } from './util.mjs';
import { request } from './test_util.mjs';
import * as make_jwt from './make_jwt.mjs';
const AUTH_TOKEN = make_jwt.make('exhibition'); // create valid auth token with subject 'exhibition'
const KEYWORDS = JSON.parse(readFileSync('./config/keywords.json'));
export const CONFIG = {
deposit_delay: 1000, // how long to wait before depositing a newly requested interaction
update_timeout: 5000, // timeout for /interaction_updates
};
function log_error(e) {
if (e.code) {
console.log(e.code);
} else if (e.response) {
console.log(e.response.statusCode);
console.log(e.response.body);
} else {
console.log(e);
}
}
async function interact() {
console.log('requesting interaction')
try {
const res = await request('/request_interaction', {
headers: { Authorization: `Bearer ${AUTH_TOKEN}`},
responseType: 'json',
});
const int = res.body;
console.log('got interaction', int);
console.log('selecting keywords...');
const keywords = [ KEYWORDS[rndint(KEYWORDS.length)], KEYWORDS[rndint(KEYWORDS.length)], KEYWORDS[rndint(KEYWORDS.length)] ];
const deposit = { id: int.id, keywords: keywords.join(',') };
await sleep(2000);
console.log('depositing interaction', deposit);
const res2 = await request('/deposit_interaction', {
headers: { Authorization: `Bearer ${AUTH_TOKEN}`},
searchParams: deposit,
responseType: 'json',
});
console.log('listening for updates...');
let seq = 0;
let done = false;
while (!done) {
try {
const res3 = await request('/interaction_updates', {
headers: { Authorization: `Bearer ${AUTH_TOKEN}`},
searchParams: { id: deposit.id, since: seq, timeout:CONFIG.update_timeout },
responseType: 'json',
});
const update = res3.body;
console.log('update:', update);
seq = update.seq;
if (update.queue_position == 0) done = true;
} catch (e) {
if (e.response?.statusCode == 504) console.log('waiting...');
// 504 timeout
}
}
console.log('done');
} catch (e) {
log_error(e);
}
}
interact();