Skip to main content

Python SDK for Creduse

Project description

Creduse SDK

A Python SDK for interacting with the Creduse credit management API. This SDK allows you to manage credits for end users with operations like adding, subtracting, and checking credit balances.

Installation

pip install creduse

Authentication

The SDK requires an API key for authentication. To get an API key, login to the Creduse dashboard and navigate to the API keys section.

You can provide the API key in two ways:

  1. Pass it directly when initializing the client:
from creduse import CreduseClient

client = CreduseClient(api_key="your-api-key")
  1. Set it as an environment variable:
export CREDUSE_API_KEY="your-api-key"
from creduse import CreduseClient

client = CreduseClient()  # Will use CREDUSE_API_KEY from environment

Usage

The SDK provides both synchronous and asynchronous clients for interacting with the Creduse API.

Synchronous Client

from creduse import CreduseClient
from uuid import UUID

# Initialize the client
client = CreduseClient(api_key="your-api-key")

# Add credits for an end user
end_user_id = UUID("12345678-1234-5678-1234-567812345678")
add_result = client.add(
    end_user_id=end_user_id,
    amount=100,            # Number of credits to add
    validity_days=31       # How long the credits are valid for (default: 31)
)

# Subtract credits from an end user
subtract_result = client.subtract(
    end_user_id=end_user_id,
    amount=50              # Number of credits to subtract
)

# Start a credit cycle for an end user
start_cycle_result = client.start_cycle(
    end_user_id=end_user_id,
    amount=1000,           # Number of credits for the cycle
    validity_days=31       # How long the cycle is valid for (default: 31)
)

# Stop a credit cycle for an end user
stop_cycle_result = client.stop_cycle(
    end_user_id=end_user_id
)

# Check the active balance for an end user
balance = client.get_balance(
    end_user_id=end_user_id
)
print(f"Current balance: {balance.active_balance}")

Asynchronous Client

import asyncio
from creduse import AsyncCreduseClient
from uuid import UUID

async def manage_credits():
    # Initialize the async client
    client = AsyncCreduseClient(api_key="your-api-key")
    
    # Add credits for an end user
    end_user_id = UUID("12345678-1234-5678-1234-567812345678")
    add_result = await client.add(
        end_user_id=end_user_id,
        amount=100,
        validity_days=31
    )
    
    # Subtract credits
    subtract_result = await client.subtract(
        end_user_id=end_user_id,
        amount=50
    )
    
    # Check balance
    balance = await client.get_balance(
        end_user_id=end_user_id
    )
    print(f"Current balance: {balance.active_balance}")

# Run the async function
asyncio.run(manage_credits())

API Reference

CreduseClient / AsyncCreduseClient

Both clients provide the same methods, with the async client using async/await syntax.

add(end_user_id, amount, validity_days=31)

Add credits to an end user's account.

  • end_user_id: UUID4 - The unique identifier for the end user
  • amount: int - The number of credits to add (must be positive)
  • validity_days: int - How long the credits are valid for (must be positive)

Returns: AddCreditModel

subtract(end_user_id, amount)

Subtract credits from an end user's account.

  • end_user_id: UUID4 - The unique identifier for the end user
  • amount: int - The number of credits to subtract (must be positive)

Returns: SubtractCreditModel

start_cycle(end_user_id, amount, validity_days=31)

Start a new credit cycle for an end user.

  • end_user_id: UUID4 - The unique identifier for the end user
  • amount: int - The number of credits for the cycle (must be positive)
  • validity_days: int - How long the cycle is valid for (must be positive)

Returns: StartCycleModel / CycleModelResponse

stop_cycle(end_user_id)

Stop an active credit cycle for an end user.

  • end_user_id: UUID4 - The unique identifier for the end user

Returns: StopCycleModel / CycleModelResponse

get_balance(end_user_id)

Get the current active balance for an end user.

  • end_user_id: UUID4 - The unique identifier for the end user

Returns: ActiveBalance

Error Handling

The SDK validates input parameters and will raise appropriate exceptions:

  • ValueError: Raised when invalid parameters are provided (e.g., negative amounts)
  • TypeError: Raised when parameters are of the wrong type

API errors will be propagated from the underlying HTTP client.

License

MIT License

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

creduse-0.1.4.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

creduse-0.1.4-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file creduse-0.1.4.tar.gz.

File metadata

  • Download URL: creduse-0.1.4.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.5

File hashes

Hashes for creduse-0.1.4.tar.gz
Algorithm Hash digest
SHA256 17e95cba10e46ead0f80346e3f0e150c78864b7584cd650e41f768636054668f
MD5 2883a1c797ad66529ccc6a54d2c772ca
BLAKE2b-256 67c0d3ad28db86238a0c1e06d18f6656538f486ec4be9266d4610dd98141323a

See more details on using hashes here.

File details

Details for the file creduse-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: creduse-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.5

File hashes

Hashes for creduse-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4de9714cbbbcd52b81522f321498393718544a2cde7ee28d0223d6a56dfbf736
MD5 12ba4ae9aeba1ca7a79a22102a55891d
BLAKE2b-256 115eed096923eb1835493541a71e5224dfba81792634f1ec78424121e1554802

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