0

List sessions

by
Published today

List the sessions on the IxNetwork API server.

Script ixia_ixnetwork Verified

The script

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

3
// X-Api-Key header: the resource's api_key, or one obtained from username/password
4
async function authHeaders(
5
  auth: RT.IxiaIxnetwork,
6
  base: string
7
): Promise<Record<string, string>> {
8
  if (auth.api_key) return { "X-Api-Key": auth.api_key }
9
  if (!auth.username) return {}
10
  const response = await fetch(`${base}/api/v1/auth/session`, {
11
    method: "POST",
12
    headers: { "Content-Type": "application/json", Accept: "application/json" },
13
    body: JSON.stringify({ username: auth.username, password: auth.password }),
14
  })
15
  if (!response.ok) {
16
    throw new Error(`${response.status} ${await response.text()}`)
17
  }
18
  const { apiKey } = (await response.json()) as { apiKey: string }
19
  return { "X-Api-Key": apiKey }
20
}
21

22
/**
23
 * List sessions
24
 * List the sessions on the IxNetwork API server, with their id, state, application type and owner.
25
 */
26
export async function main(auth: RT.IxiaIxnetwork) {
27
  const base = auth.base_url.replace(/\/+$/, "")
28
  const response = await fetch(`${base}/api/v1/sessions`, {
29
    headers: { ...(await authHeaders(auth, base)), Accept: "application/json" },
30
  })
31

32
  if (!response.ok) {
33
    throw new Error(`${response.status} ${await response.text()}`)
34
  }
35

36
  return await response.json()
37
}
38