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:
- Pass it directly when initializing the client:
from creduse import CreduseClient
client = CreduseClient(api_key="your-api-key")
- 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 useramount: 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 useramount: 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 useramount: 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file creduse-0.1.5.tar.gz.
File metadata
- Download URL: creduse-0.1.5.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c111e2d7bf16b852f3f27b2fce934051a29b5fb55209639f408435a4721a7398
|
|
| MD5 |
797bce29b0422ff6704d41123ddbe2f6
|
|
| BLAKE2b-256 |
db6d41b669f5fa8308de28e4c94d71530978219eabe522c198eee125ab331bce
|
File details
Details for the file creduse-0.1.5-py3-none-any.whl.
File metadata
- Download URL: creduse-0.1.5-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d96de72a507e85aac72c1532e2de6774fa6c0912cd42b0bbbf981337d76a52cb
|
|
| MD5 |
de4940196a78e6b22017495621ead5c4
|
|
| BLAKE2b-256 |
92a64ffe71ca4493e6effaad4485f47ac181320e43542f8e0cd2235381cd9518
|