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.6.tar.gz (64.2 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.6-py3-none-any.whl (36.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for creduse-0.1.6.tar.gz
Algorithm Hash digest
SHA256 14e8a9f3d6ba5338ecf4ebe39d449b9958df2ed4e15baef1067813f9ec2ecfd8
MD5 7d2fd31addecd019aaa3cfb6cf102b35
BLAKE2b-256 9af162c5530d14b6f4366e72ca2336e6759cd59f08b6bd554290245f9a0006a2

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for creduse-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 ea33a7dcd1b73e083d4f9b0539746c6f1646dcbc7d4b47cc378b6532c17e7ec5
MD5 2c1990e415641550894b85b73d675a1a
BLAKE2b-256 f509b1a37e3976fab8c0916ecf2535972551db7d50322010d8cc7a4a66b843fb

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