From cbd10bcf4d615a6a341e4940894ec33b99edd0e2 Mon Sep 17 00:00:00 2001 From: Siddhi sahu Date: Sat, 23 Aug 2025 16:29:10 +0530 Subject: [PATCH 1/2] [WIP] example use of rest apis Signed-off-by: Siddhi sahu --- .../cloud/academy/rest-apis-examples/index.md | 107 ++++++++++++++++++ content/en/cloud/reference/api-reference.md | 3 + 2 files changed, 110 insertions(+) create mode 100644 content/en/cloud/academy/rest-apis-examples/index.md diff --git a/content/en/cloud/academy/rest-apis-examples/index.md b/content/en/cloud/academy/rest-apis-examples/index.md new file mode 100644 index 000000000..f4e48f4ea --- /dev/null +++ b/content/en/cloud/academy/rest-apis-examples/index.md @@ -0,0 +1,107 @@ +--- +title: "REST APIs EXAMPLES" +weight: 5 +description: > + An advanced guide to how to use our Academy REST API to retrieve various statistics / information. +categories: [Academy] + +--- + +The following examples demonstrate how to retrieve statistics from the Academy REST API. + +## Get the number of active learners + +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. + +## Example: cURL Request + +```bash +curl -s -X GET "https://cloud.layer5.io/api/identity/users/online" \ + -H "Authorization: Bearer ” \ + | jq 'length' +``` + +This returns the number of active learners. +``` +30 +``` + +## Example: JavaScript (Node.js) + +```javascript +const token = "your-token" + +fetch("https://cloud.layer5.io/api/identity/users/online", { + headers: { "Authorization": "Bearer " + token } +}) + .then(res => res.json()) + .then(users => { + console.log("Active learners:", users.length); + // users is an array of {id, user_id, first_name, last_name, ...} + }); +``` + +This will print the number of active learners like this: + +``` +30 +``` + +## Example: Python Client + +```python +import requests + +url = "https://cloud.layer5.io/api/identity/users/online" +headers = {"Authorization": "Bearer "} + +resp = requests.get(url, headers=headers) +online_users = resp.json() +print("Active Learners:", len(online_users)) + +``` + +This will output: + +``` +Active Learners: 30 +``` + + +## Example: Go + +```go +package main + +import ( + "encoding/json" + "fmt" + "io" + "net/http" +) + +func main() { + url := "https://cloud.layer5.io/api/identity/users/online" + req, _ := http.NewRequest("GET", url, nil) + req.Header.Set("Authorization", "Bearer ") + + client := &http.Client{} + resp, _ := client.Do(req) + defer resp.Body.Close() + + body, _ := io.ReadAll(resp.Body) + var users []map[string]interface{} + json.Unmarshal(body, &users) + + fmt.Println("Active users:", len(users)) +} + +``` + +This will output something like: + +``` +Active users: 30 +``` + + diff --git a/content/en/cloud/reference/api-reference.md b/content/en/cloud/reference/api-reference.md index e86e84b65..ce6b9c80e 100644 --- a/content/en/cloud/reference/api-reference.md +++ b/content/en/cloud/reference/api-reference.md @@ -30,3 +30,6 @@ curl :/// \ {{< alert type="info" >}} Open API Endpoints in new window {{< /alert >}} + +## API Examples +See [Academy REST API Examples](/cloud/academy/rest-apis-examples/) for working code snippets in cURL, JavaScript, Python, and Go. From cea0a8f3c52f8ce11c80cc48b7c78d3425e127d4 Mon Sep 17 00:00:00 2001 From: Siddhi sahu Date: Tue, 26 Aug 2025 15:52:19 +0530 Subject: [PATCH 2/2] Update Api and add Tab menu Signed-off-by: Siddhi sahu --- .../cloud/academy/rest-apis-examples/index.md | 124 +++++++++--------- 1 file changed, 64 insertions(+), 60 deletions(-) diff --git a/content/en/cloud/academy/rest-apis-examples/index.md b/content/en/cloud/academy/rest-apis-examples/index.md index f4e48f4ea..ed559929f 100644 --- a/content/en/cloud/academy/rest-apis-examples/index.md +++ b/content/en/cloud/academy/rest-apis-examples/index.md @@ -1,76 +1,61 @@ --- -title: "REST APIs EXAMPLES" +title: "REST APIs Examples" weight: 5 description: > - An advanced guide to how to use our Academy REST API to retrieve various statistics / information. + An advanced guide to how to use our Academy REST APIs to retrieve various statistics/information. categories: [Academy] --- -The following examples demonstrate how to retrieve statistics from the Academy REST API. +The following examples demonstrate how to retrieve information from the Academy REST APIs. -## Get the number of active learners +## Get the total number of registered learners in Academy -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. -## Example: cURL Request +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. -```bash -curl -s -X GET "https://cloud.layer5.io/api/identity/users/online" \ - -H "Authorization: Bearer ” \ - | jq 'length' -``` -This returns the number of active learners. -``` -30 -``` +{{< tabpane >}} +{{< tab header="cURL" >}} +curl -s -X GET "https://cloud.layer5.io/api/academy/cirricula" \ + -H "Authorization: Bearer " \ + | jq '[.data[].registration_count] | add' + +{{< /tab >}} -## Example: JavaScript (Node.js) +{{< tab header="JavaScript" >}} -```javascript -const token = "your-token" +const token = "Your-Token" -fetch("https://cloud.layer5.io/api/identity/users/online", { - headers: { "Authorization": "Bearer " + token } -}) - .then(res => res.json()) - .then(users => { - console.log("Active learners:", users.length); - // users is an array of {id, user_id, first_name, last_name, ...} +async function getTotalLearners() { + const res = await fetch("https://cloud.layer5.io/api/academy/cirricula", { + headers: { Authorization: `Bearer ${token}` }, }); -``` + const data = await res.json(); + const total = data.data.reduce((sum, path) => sum + path.registration_count, 0); + console.log(total); +} -This will print the number of active learners like this: +getTotalLearners(); -``` -30 -``` +{{< /tab >}} -## Example: Python Client +{{< tab header="Python" >}} -```python import requests -url = "https://cloud.layer5.io/api/identity/users/online" -headers = {"Authorization": "Bearer "} - -resp = requests.get(url, headers=headers) -online_users = resp.json() -print("Active Learners:", len(online_users)) - -``` - -This will output: +url = "https://cloud.layer5.io/api/academy/cirricula" +headers = {"Authorization": "Bearer "} -``` -Active Learners: 30 -``` +res = requests.get(url, headers=headers) +data = res.json() +total = sum(item["registration_count"] for item in data["data"]) +print(total) +{{< /tab >}} -## Example: Go +{{< tab header="Golang" >}} -```go package main import ( @@ -80,28 +65,47 @@ import ( "net/http" ) +type Path struct { + RegistrationCount int `json:"registration_count"` +} + +type Response struct { + Data []Path `json:"data"` +} + func main() { - url := "https://cloud.layer5.io/api/identity/users/online" + url := "https://cloud.layer5.io/api/academy/cirricula" + req, _ := http.NewRequest("GET", url, nil) req.Header.Set("Authorization", "Bearer ") client := &http.Client{} - resp, _ := client.Do(req) - defer resp.Body.Close() + res, err := client.Do(req) + if err != nil { + panic(err) + } + defer res.Body.Close() - body, _ := io.ReadAll(resp.Body) - var users []map[string]interface{} - json.Unmarshal(body, &users) + body, _ := io.ReadAll(res.Body) - fmt.Println("Active users:", len(users)) -} + var response Response + if err := json.Unmarshal(body, &response); err != nil { + panic(err) + } -``` + total := 0 + for _, path := range response.Data { + total += path.RegistrationCount + } -This will output something like: + fmt.Println(total) +} -``` -Active users: 30 -``` +{{< /tab >}} +{{< /tabpane >}} +This returns the number of Total registered learners: +``` +130 +``` \ No newline at end of file