Get Top Locations By Operating System

Get the top locations, by HTTP traffic, of the requested operating systems. Values are a percentage out of the total traffic.

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 Top Locations By Operating System
8
 * Get the top locations, by HTTP traffic, of the requested operating systems. Values are a percentage out of the total traffic.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  os:
13
    | "WINDOWS"
14
    | "MACOSX"
15
    | "IOS"
16
    | "ANDROID"
17
    | "CHROMEOS"
18
    | "LINUX"
19
    | "SMART_TV",
20
  limit: string | undefined,
21
  name: string | undefined,
22
  dateRange: string | undefined,
23
  dateStart: string | undefined,
24
  dateEnd: string | undefined,
25
  asn: string | undefined,
26
  location: string | undefined,
27
  botClass: string | undefined,
28
  deviceType: string | undefined,
29
  httpProtocol: string | undefined,
30
  httpVersion: string | undefined,
31
  ipVersion: string | undefined,
32
  tlsVersion: string | undefined,
33
  format: "JSON" | "CSV" | undefined
34
) {
35
  const url = new URL(
36
    `https://api.cloudflare.com/client/v4/radar/http/top/locations/os/${os}`
37
  );
38
  for (const [k, v] of [
39
    ["limit", limit],
40
    ["name", name],
41
    ["dateRange", dateRange],
42
    ["dateStart", dateStart],
43
    ["dateEnd", dateEnd],
44
    ["asn", asn],
45
    ["location", location],
46
    ["botClass", botClass],
47
    ["deviceType", deviceType],
48
    ["httpProtocol", httpProtocol],
49
    ["httpVersion", httpVersion],
50
    ["ipVersion", ipVersion],
51
    ["tlsVersion", tlsVersion],
52
    ["format", format],
53
  ]) {
54
    if (v !== undefined && v !== "") {
55
      url.searchParams.append(k, v);
56
    }
57
  }
58
  const response = await fetch(url, {
59
    method: "GET",
60
    headers: {
61
      "X-AUTH-EMAIL": auth.email,
62
      "X-AUTH-KEY": auth.key,
63
      Authorization: "Bearer " + auth.token,
64
    },
65
    body: undefined,
66
  });
67
  if (!response.ok) {
68
    const text = await response.text();
69
    throw new Error(`${response.status} ${text}`);
70
  }
71
  return await response.json();
72
}
73