0

Get snapshot

by
Published today

Get the details of one IP Fabric snapshot by id.

Script ip_fabric Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 hours ago
1
//native
2

3
/**
4
 * Get snapshot
5
 * Get the details of one snapshot by id (GET /snapshots/{id}), including its version, licensed device count and errors. Use List snapshots to find ids.
6
 */
7
export async function main(auth: RT.IpFabric, snapshot_id: string) {
8
  const version = (auth.api_version ?? "").trim()
9
  // Same normalization as the official SDK: "7.2" / "v7.2" -> v7.2, "7" -> v7.0.
10
  const match = version.match(/^v?(\d+)(?:\.(\d+))?/)
11
  if (version && !match) {
12
    throw new Error(
13
      `api_version "${version}" is not valid, use v#.# (e.g. v7.2)`
14
    )
15
  }
16
  const prefix = match ? `api/v${match[1]}.${match[2] ?? "0"}/` : "api/"
17
  const url = new URL(
18
    `${prefix}snapshots/${encodeURIComponent(snapshot_id)}`,
19
    auth.base_url.replace(/\/*$/, "/")
20
  )
21

22
  const response = await fetch(url, {
23
    headers: {
24
      "X-API-Token": auth.token,
25
      Accept: "application/json",
26
    },
27
  })
28

29
  if (!response.ok) {
30
    throw new Error(`${response.status} ${await response.text()}`)
31
  }
32

33
  return await response.json()
34
}
35