0
Get details and aggregate metrics for an http test
One script reply has been approved by the moderators Verified

Get test details and aggregate performance metrics for an http test for a given time period between 1 hour and 7 days.

Created by hugo697 254 days ago Viewed 8902 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 254 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Get details and aggregate metrics for an http test
8
 * Get test details and aggregate performance metrics for an http test for a given time period between 1 hour and 7 days.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  test_id: string,
14
  deviceId: string | undefined,
15
  timeStart: string | undefined,
16
  timeEnd: string | undefined,
17
  interval: "minute" | "hour" | undefined,
18
  colo: string | undefined
19
) {
20
  const url = new URL(
21
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/dex/http-tests/${test_id}`
22
  );
23
  for (const [k, v] of [
24
    ["deviceId", deviceId],
25
    ["timeStart", timeStart],
26
    ["timeEnd", timeEnd],
27
    ["interval", interval],
28
    ["colo", colo],
29
  ]) {
30
    if (v !== undefined && v !== "") {
31
      url.searchParams.append(k, v);
32
    }
33
  }
34
  const response = await fetch(url, {
35
    method: "GET",
36
    headers: {
37
      "X-AUTH-EMAIL": auth.email,
38
      "X-AUTH-KEY": auth.key,
39
      Authorization: "Bearer " + auth.token,
40
    },
41
    body: undefined,
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49