Skip to main content

Python client for RADIUSdesk API - manage vouchers, users, and balances

Project description

RadiusDesk API Client

CI/CD PyPI version Python Support License: GPL v3

A production-ready Python client for interacting with the cake4 RADIUSdesk API. This package provides a simple interface for managing vouchers, permanent users, and user balances in your RadiusDesk instance.

Features

  • Token-based authentication with automatic token management
  • Voucher management: Create, list, and query voucher usage details
  • User management: Create permanent users and add data/time top-ups
  • Automatic token refresh to handle expired tokens seamlessly

Installation

Install the package from PyPI using pip:

pip install radiusdesk-api

Requirements

  • Python 3.10 or higher
  • A RadiusDesk instance with API access
  • Valid credentials (username, password, cloud ID)

Note: The package automatically handles the RADIUSdesk API URL structure (/cake4/rd_cake/). You can provide the base URL with or without this path - it will be added automatically if not present.

Quick Start

from radiusdesk_api import RadiusDeskClient

# Initialize the client
# Note: The /cake4/rd_cake path is added automatically if not present
client = RadiusDeskClient(
    base_url="https://radiusdesk.example.com",  # or include /cake4/rd_cake
    username="admin",
    password="your-password",
    cloud_id="1"
)

# Create a voucher
voucher = client.vouchers.create(
    realm_id=1,
    profile_id=2,
    quantity=1
)
print(f"Created voucher: {voucher}")

# List all vouchers
vouchers = client.vouchers.list(limit=100)
print(f"Total vouchers: {vouchers['totalCount']}")

# Create a permanent user
user = client.users.create(
    username="john.doe",
    password="secure-password",
    profile_id=2,
    name="John",
    surname="Doe",
    email="john.doe@example.com"
)
print(f"Created user: {user}")

Usage Examples

Authentication

The client handles authentication automatically, but you can also manage tokens manually:

from radiusdesk_api import RadiusDeskClient

# Auto-login on initialization (default)
client = RadiusDeskClient(
    base_url="https://radiusdesk.example.com",
    username="admin",
    password="password",
    cloud_id="1"
)

# Defer authentication until first API call
client = RadiusDeskClient(
    base_url="https://radiusdesk.example.com",
    username="admin",
    password="password",
    cloud_id="1",
    auto_login=False
)

# Check connection
if client.check_connection():
    print("Connected to RadiusDesk!")

# Manually refresh token
new_token = client.refresh_token()

Voucher Operations

Create Vouchers

# Create a single voucher
voucher_code = client.vouchers.create(
    realm_id=1,
    profile_id=2,
    quantity=1,
    never_expire=True
)
print(f"Voucher code: {voucher_code}")

# Create multiple vouchers
vouchers = client.vouchers.create(
    realm_id=1,
    profile_id=2,
    quantity=10,
    never_expire=False
)
print(f"Created {len(vouchers['data'])} vouchers")

List Vouchers

# List all vouchers
result = client.vouchers.list(limit=100)
for voucher in result['items']:
    print(f"Voucher: {voucher['name']}")

# List with pagination
result = client.vouchers.list(limit=50, page=2, start=50)

Get Voucher Details

# Get detailed usage information about a voucher
# (includes connection history, data usage, session info)
details = client.vouchers.get_details("VOUCHER-CODE-123")
print(details)

User Operations

Create Permanent Users

# Create a user with all details
user = client.users.create(
    username="john.doe",
    password="secure-password",
    realm_id=1,
    profile_id=2,
    name="John",
    surname="Doe",
    email="john.doe@example.com",
    phone="+1234567890",
    address="123 Main St"
)

# Create a user with minimal information (required params only)
user = client.users.create(
    username="jane.doe",
    password="secure-password",
    realm_id=1,
    profile_id=2
)

List Users

# List all permanent users
users = client.users.list(limit=100)
for user in users['items']:
    print(f"User: {user['username']}")

# List with pagination
users = client.users.list(limit=50, page=2)

Add Top-Ups (Data/Time Balance)

# Add data balance to a user
result = client.users.add_data(
    user_id=123,
    amount=2,
    unit="gb",
    comment="Monthly data allocation"
)

# Add time balance to a user
result = client.users.add_time(
    user_id=123,
    amount=60,
    unit="minutes",
    comment="Bonus time"
)

# Supported units:
# Data: "mb", "gb"
# Time: "minutes", "hours", "days"

Delete User

# Delete a permanent user
result = client.users.delete(user_id=123)
print(f"User deleted: {result}")

Error Handling

The package provides custom exceptions for different error scenarios:

from radiusdesk_api import (
    RadiusDeskClient,
    AuthenticationError,
    APIError,
    TokenExpiredError
)

try:
    client = RadiusDeskClient(
        base_url="https://radiusdesk.example.com",
        username="admin",
        password="wrong-password",
        cloud_id="1"
    )
except AuthenticationError as e:
    print(f"Authentication failed: {e}")

try:
    voucher = client.vouchers.create(
        realm_id=999,  # Invalid realm
        profile_id=999  # Invalid profile
    )
except APIError as e:
    print(f"API error: {e}")
    print(f"Status code: {e.status_code}")

Logging

The package uses Python's built-in logging module. Enable logging to see detailed information:

import logging

# Enable debug logging
logging.basicConfig(level=logging.DEBUG)

client = RadiusDeskClient(
    base_url="https://radiusdesk.example.com",
    username="admin",
    password="password",
    cloud_id="1"
)

Development

Setup Development Environment

# Clone the repository
git clone https://github.com/keeganwhite/radiusdesk-api.git
cd radiusdesk-api

# Create a virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install development dependencies
pip install -r requirements-dev.txt

# Install package in editable mode
pip install -e .

# Create .env file from template
cp .env.example .env
# Edit .env with your credentials

Running Tests

The package uses integration tests that require a live RadiusDesk instance.

Option 1: Using .env file (Recommended)

# Create .env file from template
cp .env.example .env
# Edit .env with your credentials

# Run tests
./run_tests.sh

Option 2: Manual environment variables

export RADIUSDESK_URL="https://radiusdesk.example.com"
export RADIUSDESK_USERNAME="admin"
export RADIUSDESK_PASSWORD="password"
export RADIUSDESK_CLOUD_ID="1"
export RADIUSDESK_REALM_ID="1"
export RADIUSDESK_PROFILE_ID="2"

pytest tests/ -v

Code Quality

# Run linting
flake8 radiusdesk_api tests

# Format code
black radiusdesk_api tests

Building the Package

# Install build tools
pip install build twine

# Build the package
python -m build

# Check the build
twine check dist/*

Publishing to PyPI

# Upload to PyPI
twine upload dist/*

GitHub Actions

The package includes a GitHub Actions workflow that:

  • Runs linting with flake8
  • Runs integration tests on Python 3.10, 3.11, and 3.12
  • Builds the package
  • Publishes to PyPI when a commit message contains [release]

Required GitHub Secrets

Set these secrets in your GitHub repository settings:

  • RADIUSDESK_URL: URL of your RadiusDesk instance
  • RADIUSDESK_USERNAME: Username for authentication
  • RADIUSDESK_PASSWORD: Password for authentication
  • RADIUSDESK_CLOUD_ID: Cloud ID
  • RADIUSDESK_REALM_ID: Realm ID for testing
  • RADIUSDESK_PROFILE_ID: Profile ID for testing
  • PYPI_API_TOKEN: Your PyPI API token for publishing

Contributing

Contributions are welcome! Please follow these guidelines:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Run tests and linting
  5. Commit your changes (git commit -m 'Add amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

License

This project is licensed under the GNU General Public License v3.0 or later - see the LICENSE file for details.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

radiusdesk_api-0.1.0.tar.gz (28.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

radiusdesk_api-0.1.0-py3-none-any.whl (25.3 kB view details)

Uploaded Python 3

File details

Details for the file radiusdesk_api-0.1.0.tar.gz.

File metadata

  • Download URL: radiusdesk_api-0.1.0.tar.gz
  • Upload date:
  • Size: 28.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for radiusdesk_api-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cc40fedfd7c07027a1109bdf7c6c5abfb04e16c6ff640e968ea98a468e17baf6
MD5 2b4b06a249679e8a39b99623effe1a12
BLAKE2b-256 bbed0ee78334cc93412099f5692ba6d26c37977bcf319dd71b7889b17a56a605

See more details on using hashes here.

File details

Details for the file radiusdesk_api-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: radiusdesk_api-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 25.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for radiusdesk_api-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 abc81f6aa37b7c2ba6ca7204c2b3a2f9f24d0a57d8661888acd5a5dbdf96a205
MD5 72ca86eba6ebe93c87b0ed4bfbe62698
BLAKE2b-256 e8ebec06705f45836a4b52fafbb6d19db25c5b75ebf75a37dfcdf20498019947

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page