0

Get AS112 DNS Protocol Time Series

by
Published Nov 16, 2023

Percentage distribution of AS112 dns requests classified per Protocol over time.

Script cloudflare Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 403 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Get AS112 DNS Protocol Time Series
8
 * Percentage distribution of AS112 dns requests classified per Protocol over time.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  aggInterval: "15m" | "1h" | "1d" | "1w" | undefined,
13
  name: string | undefined,
14
  dateRange: string | undefined,
15
  dateStart: string | undefined,
16
  dateEnd: string | undefined,
17
  asn: string | undefined,
18
  location: string | undefined,
19
  format: "JSON" | "CSV" | undefined
20
) {
21
  const url = new URL(
22
    `https://api.cloudflare.com/client/v4/radar/as112/timeseries_groups/protocol`
23
  );
24
  for (const [k, v] of [
25
    ["aggInterval", aggInterval],
26
    ["name", name],
27
    ["dateRange", dateRange],
28
    ["dateStart", dateStart],
29
    ["dateEnd", dateEnd],
30
    ["asn", asn],
31
    ["location", location],
32
    ["format", format],
33
  ]) {
34
    if (v !== undefined && v !== "") {
35
      url.searchParams.append(k, v);
36
    }
37
  }
38
  const response = await fetch(url, {
39
    method: "GET",
40
    headers: {
41
      "X-AUTH-EMAIL": auth.email,
42
      "X-AUTH-KEY": auth.key,
43
      Authorization: "Bearer " + auth.token,
44
    },
45
    body: undefined,
46
  });
47
  if (!response.ok) {
48
    const text = await response.text();
49
    throw new Error(`${response.status} ${text}`);
50
  }
51
  return await response.json();
52
}
53