Get IQI Summary

Get a summary (percentiles) of bandwidth, latency or DNS response time from the Radar Internet Quality Index (IQI).

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Get IQI Summary
8
 * Get a summary (percentiles) of bandwidth, latency or DNS response time from the Radar Internet Quality Index (IQI).
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  name: string | undefined,
13
  dateRange: string | undefined,
14
  dateStart: string | undefined,
15
  dateEnd: string | undefined,
16
  asn: string | undefined,
17
  location: string | undefined,
18
  continent: string | undefined,
19
  metric: "BANDWIDTH" | "DNS" | "LATENCY" | undefined,
20
  format: "JSON" | "CSV" | undefined
21
) {
22
  const url = new URL(
23
    `https://api.cloudflare.com/client/v4/radar/quality/iqi/summary`
24
  );
25
  for (const [k, v] of [
26
    ["name", name],
27
    ["dateRange", dateRange],
28
    ["dateStart", dateStart],
29
    ["dateEnd", dateEnd],
30
    ["asn", asn],
31
    ["location", location],
32
    ["continent", continent],
33
    ["metric", metric],
34
    ["format", format],
35
  ]) {
36
    if (v !== undefined && v !== "") {
37
      url.searchParams.append(k, v);
38
    }
39
  }
40
  const response = await fetch(url, {
41
    method: "GET",
42
    headers: {
43
      "X-AUTH-EMAIL": auth.email,
44
      "X-AUTH-KEY": auth.key,
45
      Authorization: "Bearer " + auth.token,
46
    },
47
    body: undefined,
48
  });
49
  if (!response.ok) {
50
    const text = await response.text();
51
    throw new Error(`${response.status} ${text}`);
52
  }
53
  return await response.json();
54
}
55