Edits history of script submission #10801 for ' Get Droplet Bandwidth Metrics (digitalocean)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Digitalocean = {
      token: string;
    };
    /**
     * Get Droplet Bandwidth Metrics
     * To retrieve bandwidth metrics for a given Droplet, send a GET request to `/v2/monitoring/metrics/droplet/bandwidth`. Use the `interface` query parameter to specify if the results should be for the `private` or `public` interface. Use the `direction` query parameter to specify if the results should be for `inbound` or `outbound` traffic.
    The metrics in the response body are in megabits per second (Mbps).
     */
    export async function main(
      auth: Digitalocean,
      host_id: string | undefined,
      interfaceType: "private" | "public" | undefined,
      direction: "inbound" | "outbound" | undefined,
      start: string | undefined,
      end: string | undefined,
    ) {
      const url = new URL(
        `https://api.digitalocean.com/v2/monitoring/metrics/droplet/bandwidth`,
      );
      for (const [k, v] of [
        ["host_id", host_id],
        ["interface", interfaceType],
        ["direction", direction],
        ["start", start],
        ["end", end],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "Bearer " + auth.token,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 536 days ago