Edits history of script submission #24089 for ' Get snapshot (ip_fabric)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Get snapshot
     * 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.
     */
    export async function main(auth: RT.IpFabric, snapshot_id: string) {
      const version = (auth.api_version ?? "").trim()
      // Same normalization as the official SDK: "7.2" / "v7.2" -> v7.2, "7" -> v7.0.
      const match = version.match(/^v?(\d+)(?:\.(\d+))?/)
      if (version && !match) {
        throw new Error(
          `api_version "${version}" is not valid, use v#.# (e.g. v7.2)`
        )
      }
      const prefix = match ? `api/v${match[1]}.${match[2] ?? "0"}/` : "api/"
      const url = new URL(
        `${prefix}snapshots/${encodeURIComponent(snapshot_id)}`,
        auth.base_url.replace(/\/*$/, "/")
      )
    
      const response = await fetch(url, {
        headers: {
          "X-API-Token": auth.token,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 7 hours ago