Skip to content

Commit cea0a8f

Browse files
committed
Update Api and add Tab menu
Signed-off-by: Siddhi sahu <himanisahu739@gmail.com>
1 parent 5687710 commit cea0a8f

1 file changed

Lines changed: 64 additions & 60 deletions

File tree

  • content/en/cloud/academy/rest-apis-examples
Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,61 @@
11
---
2-
title: "REST APIs EXAMPLES"
2+
title: "REST APIs Examples"
33
weight: 5
44
description: >
5-
An advanced guide to how to use our Academy REST API to retrieve various statistics / information.
5+
An advanced guide to how to use our Academy REST APIs to retrieve various statistics/information.
66
categories: [Academy]
77

88
---
99

10-
The following examples demonstrate how to retrieve statistics from the Academy REST API.
10+
The following examples demonstrate how to retrieve information from the Academy REST APIs.
1111

12-
## Get the number of active learners
12+
## Get the total number of registered learners in Academy
1313

14-
Use the Layer5 Cloud API to retrieve the number of *active* learners. Pass your [security token](https://docs.layer5.io/cloud/security/tokens/) as a Bearer token in the `Authorization` header (as shown in [Authenticating with API](https://docs.layer5.io/cloud/reference/api-reference/#authenticating-with-the-api)). The response JSON includes an array of user objects.
1514

16-
## Example: cURL Request
15+
Use the Layer5 Cloud API to retrieve the *total* number of registered learners. Pass your [Security Token](https://docs.layer5.io/cloud/security/tokens/) as a Bearer token in the `Authorization` header (as shown in [Authenticating with API](https://docs.layer5.io/cloud/reference/api-reference/#authenticating-with-the-api)). The response JSON includes an array of user objects.
1716

18-
```bash
19-
curl -s -X GET "https://cloud.layer5.io/api/identity/users/online" \
20-
-H "Authorization: Bearer <your-token>” \
21-
| jq 'length'
22-
```
2317

24-
This returns the number of active learners.
25-
```
26-
30
27-
```
18+
{{< tabpane >}}
19+
{{< tab header="cURL" >}}
20+
curl -s -X GET "https://cloud.layer5.io/api/academy/cirricula" \
21+
-H "Authorization: Bearer <Your-Token>" \
22+
| jq '[.data[].registration_count] | add'
23+
24+
{{< /tab >}}
2825

29-
## Example: JavaScript (Node.js)
26+
{{< tab header="JavaScript" >}}
3027

31-
```javascript
32-
const token = "your-token"
28+
const token = "Your-Token"
3329

34-
fetch("https://cloud.layer5.io/api/identity/users/online", {
35-
headers: { "Authorization": "Bearer " + token }
36-
})
37-
.then(res => res.json())
38-
.then(users => {
39-
console.log("Active learners:", users.length);
40-
// users is an array of {id, user_id, first_name, last_name, ...}
30+
async function getTotalLearners() {
31+
const res = await fetch("https://cloud.layer5.io/api/academy/cirricula", {
32+
headers: { Authorization: `Bearer ${token}` },
4133
});
42-
```
34+
const data = await res.json();
35+
const total = data.data.reduce((sum, path) => sum + path.registration_count, 0);
36+
console.log(total);
37+
}
4338

44-
This will print the number of active learners like this:
39+
getTotalLearners();
4540

46-
```
47-
30
48-
```
41+
{{< /tab >}}
4942

50-
## Example: Python Client
43+
{{< tab header="Python" >}}
5144

52-
```python
5345
import requests
5446

55-
url = "https://cloud.layer5.io/api/identity/users/online"
56-
headers = {"Authorization": "Bearer <your-token>"}
57-
58-
resp = requests.get(url, headers=headers)
59-
online_users = resp.json()
60-
print("Active Learners:", len(online_users))
61-
62-
```
63-
64-
This will output:
47+
url = "https://cloud.layer5.io/api/academy/cirricula"
48+
headers = {"Authorization": "Bearer <Your-Token>"}
6549

66-
```
67-
Active Learners: 30
68-
```
50+
res = requests.get(url, headers=headers)
51+
data = res.json()
52+
total = sum(item["registration_count"] for item in data["data"])
53+
print(total)
6954

55+
{{< /tab >}}
7056

71-
## Example: Go
57+
{{< tab header="Golang" >}}
7258

73-
```go
7459
package main
7560

7661
import (
@@ -80,28 +65,47 @@ import (
8065
"net/http"
8166
)
8267

68+
type Path struct {
69+
RegistrationCount int `json:"registration_count"`
70+
}
71+
72+
type Response struct {
73+
Data []Path `json:"data"`
74+
}
75+
8376
func main() {
84-
url := "https://cloud.layer5.io/api/identity/users/online"
77+
url := "https://cloud.layer5.io/api/academy/cirricula"
78+
8579
req, _ := http.NewRequest("GET", url, nil)
8680
req.Header.Set("Authorization", "Bearer <your-token>")
8781

8882
client := &http.Client{}
89-
resp, _ := client.Do(req)
90-
defer resp.Body.Close()
83+
res, err := client.Do(req)
84+
if err != nil {
85+
panic(err)
86+
}
87+
defer res.Body.Close()
9188

92-
body, _ := io.ReadAll(resp.Body)
93-
var users []map[string]interface{}
94-
json.Unmarshal(body, &users)
89+
body, _ := io.ReadAll(res.Body)
9590

96-
fmt.Println("Active users:", len(users))
97-
}
91+
var response Response
92+
if err := json.Unmarshal(body, &response); err != nil {
93+
panic(err)
94+
}
9895

99-
```
96+
total := 0
97+
for _, path := range response.Data {
98+
total += path.RegistrationCount
99+
}
100100

101-
This will output something like:
101+
fmt.Println(total)
102+
}
102103

103-
```
104-
Active users: 30
105-
```
104+
{{< /tab >}}
106105

106+
{{< /tabpane >}}
107107

108+
This returns the number of Total registered learners:
109+
```
110+
130
111+
```

0 commit comments

Comments
 (0)