0
Get Operating Systems Time Series
One script reply has been approved by the moderators Verified

Get a time series of the percentage distribution of traffic of the top operating systems.

Created by hugo697 103 days ago Viewed 4 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 103 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Get Operating Systems Time Series
8
 * Get a time series of the percentage distribution of traffic of the top operating systems.
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
  botClass: string | undefined,
20
  deviceType: string | undefined,
21
  httpProtocol: string | undefined,
22
  httpVersion: string | undefined,
23
  ipVersion: string | undefined,
24
  tlsVersion: string | undefined,
25
  format: "JSON" | "CSV" | undefined
26
) {
27
  const url = new URL(
28
    `https://api.cloudflare.com/client/v4/radar/http/timeseries_groups/os`
29
  );
30
  for (const [k, v] of [
31
    ["aggInterval", aggInterval],
32
    ["name", name],
33
    ["dateRange", dateRange],
34
    ["dateStart", dateStart],
35
    ["dateEnd", dateEnd],
36
    ["asn", asn],
37
    ["location", location],
38
    ["botClass", botClass],
39
    ["deviceType", deviceType],
40
    ["httpProtocol", httpProtocol],
41
    ["httpVersion", httpVersion],
42
    ["ipVersion", ipVersion],
43
    ["tlsVersion", tlsVersion],
44
    ["format", format],
45
  ]) {
46
    if (v !== undefined && v !== "") {
47
      url.searchParams.append(k, v);
48
    }
49
  }
50
  const response = await fetch(url, {
51
    method: "GET",
52
    headers: {
53
      "X-AUTH-EMAIL": auth.email,
54
      "X-AUTH-KEY": auth.key,
55
      Authorization: "Bearer " + auth.token,
56
    },
57
    body: undefined,
58
  });
59
  if (!response.ok) {
60
    const text = await response.text();
61
    throw new Error(`${response.status} ${text}`);
62
  }
63
  return await response.json();
64
}
65