Get Top Locations By HTTP Protocol

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