Untitled

mail@pastecode.io avatar
unknown
python
a year ago
3.4 kB
20
Indexable
Never
import requests
import json

def get_users(api_url, bearer_token):
    headers = {"Authorization": f"Bearer {bearer_token}"}
    response = requests.get(f"{api_url}/admin/users", headers=headers)
    return response.json()

def create_user(api_url, bearer_token):
    headers = {"Authorization": f"Bearer {bearer_token}"}
    response = requests.post(f"{api_url}/admin/users", headers=headers)
    return response.json()

def upsert_user(api_url, bearer_token, token, user_data):
    headers = {"Authorization": f"Bearer {bearer_token}", "Content-Type": "application/json"}
    response = requests.put(f"{api_url}/admin/users/{token}", json=user_data, headers=headers)
    return response.json()

def bulk_upsert_users(api_url, bearer_token, users_data):
    headers = {"Authorization": f"Bearer {bearer_token}", "Content-Type": "application/json"}
    response = requests.put(f"{api_url}/admin/users", json={"users": users_data}, headers=headers)
    return response.json()

def get_user_by_token(api_url, bearer_token, token):
    headers = {"Authorization": f"Bearer {bearer_token}"}
    response = requests.get(f"{api_url}/admin/users/{token}", headers=headers)
    return response.json()

def disable_user(api_url, bearer_token, token, disabled_reason=None):
    headers = {"Authorization": f"Bearer {bearer_token}"}
    params = {"disabledReason": disabled_reason} if disabled_reason else None
    response = requests.delete(f"{api_url}/admin/users/{token}", headers=headers, params=params)
    return response.json()

if __name__ == "__main__":
    api_url = input("Enter your API URL: ")
    bearer_token = input("Enter your Bearer token: ")

    while True:
        print("\nAvailable functions:")
        print("1. List all users")
        print("2. Create a new user")
        print("3. Update a user by token")
        print("4. Bulk upsert users")
        print("5. Get a user by token")
        print("6. Disable a user by token")
        print("0. Exit")

        choice = input("Enter the number of the function you want to use (0 to exit): ")

        if choice == "1":
            users = get_users(api_url, bearer_token)
            print(users)
        elif choice == "2":
            create_user(api_url, bearer_token)
            print("User created successfully.")
        elif choice == "3":
            token = input("Enter the user's token: ")
            user_data = json.loads(input("Enter the user data as a JSON object: "))
            updated_user = upsert_user(api_url, bearer_token, token, user_data)
            print(updated_user)
        elif choice == "4":
            users_data = json.loads(input("Enter the users data as a JSON array: "))
            upserted_users = bulk_upsert_users(api_url, bearer_token, users_data)
            print(upserted_users)
        elif choice == "5":
            token = input("Enter the user's token: ")
            user = get_user_by_token(api_url, bearer_token, token)
            print(user)
        elif choice == "6":
            token = input("Enter the user's token: ")
            disabled_reason = input("Enter the reason for disabling the user (optional): ")
            disabled_user = disable_user(api_url, bearer_token, token, disabled_reason)
            print(disabled_user)
        elif choice == "0":
            break
        else:
            print("Invalid choice. Please try again.")