-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractitestClient.ts
More file actions
63 lines (55 loc) · 1.78 KB
/
Copy pathpractitestClient.ts
File metadata and controls
63 lines (55 loc) · 1.78 KB
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
type PractiTestConfig = {
baseUrl: string;
email: string;
token: string;
projectId: string;
setId: string;
};
function getRequiredEnv(name: string): string {
const value = process.env[name];
if (!value) {
throw new Error(`Missing environment variable: ${name}`);
}
return value;
}
export function getConfig(): PractiTestConfig {
return {
baseUrl: getRequiredEnv("PT_BASE_URL").replace(/\/$/, ""),
email: getRequiredEnv("PT_EMAIL"),
token: getRequiredEnv("PT_TOKEN"),
projectId: getRequiredEnv("PT_PROJECT_ID"),
setId: getRequiredEnv("PT_SET_ID"),
};
}
function authHeader(email: string, token: string) {
const auth = Buffer.from(`${email}:${token}`).toString("base64");
return { Authorization: `Basic ${auth}` };
}
export async function autoCreateRun(payload: unknown, retries = 3) {
const cfg = getConfig();
const retryDelays = [15000, 30000];
const url = `${cfg.baseUrl}/api/v2/projects/${cfg.projectId}/runs/auto_create.json`;
for (let attempt = 1; attempt <= retries; attempt += 1) {
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
...authHeader(cfg.email, cfg.token),
},
body: JSON.stringify(payload),
});
if (response.ok) {
return response.json();
}
const body = await response.text();
if (response.status === 429 && attempt < retries) {
const delay = retryDelays[attempt - 1] ?? retryDelays[retryDelays.length - 1];
console.warn(
`PractiTest rate limited (429). Retrying in ${delay / 1000}s... (attempt ${attempt}/${retries})`
);
await new Promise((resolve) => setTimeout(resolve, delay));
continue;
}
throw new Error(`PractiTest auto_create failed ${response.status}: ${body}`);
}
}