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

Percentage distribution of Internet traffic generated by different operating systems like Windows, macOS, Android, iOS, and others, over a given time period.

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