GitHub user dosubot[bot] added a comment to the discussion: How to get list of 
roles or users which have more than 100 datas using API Superset 5.0.0?

<!-- Answer -->
Here's a Python example that logs in to Superset 5.0.0, fetches all roles (or 
users) using paginated API requests, and aggregates the results. This uses the 
`/api/v1/security/login` endpoint for authentication and 
`/api/v1/security/roles/search/` for roles (or `/api/v1/security/users/` for 
users). The API enforces a maximum of 100 results per request, so the code 
loops through pages until all results are fetched 
([source](https://github.com/apache/superset/issues/34467), 
[source](https://github.com/apache/superset/discussions/33878)).

```python
import requests
import json

SUPERSET_URL = "https://your-superset-instance";
USERNAME = "your_username"
PASSWORD = "your_password"

def get_access_token():
    login_url = f"{SUPERSET_URL}/api/v1/security/login"
    payload = {
        "username": USERNAME,
        "password": PASSWORD,
        "provider": "db"
    }
    resp = requests.post(login_url, json=payload)
    resp.raise_for_status()
    return resp.json()["access_token"]

def fetch_all(endpoint):
    headers = {"Authorization": f"Bearer {get_access_token()}"}
    all_results = []
    page = 0
    page_size = 100
    while True:
        params = {
            "q": json.dumps({"page": page, "page_size": page_size})
        }
        resp = requests.get(f"{SUPERSET_URL}{endpoint}", headers=headers, 
params=params)
        resp.raise_for_status()
        data = resp.json()
        all_results.extend(data["result"])
        if len(all_results) >= data["count"]:
            break
        page += 1
    return all_results

# For roles:
roles = fetch_all("/api/v1/security/roles/search/")
print(f"Total roles: {len(roles)}")

# For users, use:
# users = fetch_all("/api/v1/security/users/")
# print(f"Total users: {len(users)}")
```

Replace `SUPERSET_URL`, `USERNAME`, and `PASSWORD` with your actual values. If 
you want to fetch users, just change the endpoint as shown in the comment.

Let me know if you need this in another language or want to filter results!


<!-- Dosu Comment Footer -->
*To reply, just mention [@dosu](https://go.dosu.dev/dosubot).*

---
<sup>How did I do? 
[Good](https://app.dosu.dev/response-feedback/30b397a2-5989-40fc-b51d-825b4a350806?feedback_type=great_response)
 | 
[Irrelevant](https://app.dosu.dev/response-feedback/30b397a2-5989-40fc-b51d-825b4a350806?feedback_type=irrelevant_answer)
 | 
[Incorrect](https://app.dosu.dev/response-feedback/30b397a2-5989-40fc-b51d-825b4a350806?feedback_type=incorrect_sources)
 | 
[Verbose](https://app.dosu.dev/response-feedback/30b397a2-5989-40fc-b51d-825b4a350806?feedback_type=too_verbose)
 | 
[Hallucination](https://app.dosu.dev/response-feedback/30b397a2-5989-40fc-b51d-825b4a350806?feedback_type=hallucination)
 | [Report 
🐛](https://app.dosu.dev/response-feedback/30b397a2-5989-40fc-b51d-825b4a350806?feedback_type=bug_report)
 | 
[Other](https://app.dosu.dev/response-feedback/30b397a2-5989-40fc-b51d-825b4a350806?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat
 with 
Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/a28d3c7e-a9d3-459e-9fb6-3a6f9ff4f357/ask?utm_source=github)&
 nbsp;[![Join 
Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share
 on 
X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/apache/superset/discussions/34590)

GitHub link: 
https://github.com/apache/superset/discussions/34590#discussioncomment-14030539

----
This is an automatically sent email for notifications@superset.apache.org.
To unsubscribe, please send an email to: 
notifications-unsubscr...@superset.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org
For additional commands, e-mail: notifications-h...@superset.apache.org

Reply via email to