Skip to main content

Python library for Credici

Project description

Credici Library

The Credici library is a comprehensive solution designed for developers to manage credit-based subscriptions or pay-as-you-go systems efficiently. It provides a robust set of tools to handle user balances, transactions, and cycles with ease, both synchronously and asynchronously.

Features

  • User Balance Management: Track and manage the balance of users in your system.
  • Credit Transactions: Add or subtract credits from a user's balance, with support for expiration dates.
  • Cycles: Implement subscription cycles or billing periods, with the ability to start new cycles for users.
  • Transaction History: Retrieve all transactions for a user, or transactions within a specific period.
  • Storage Flexibility: Utilize the provided storage interface to integrate with your preferred storage solution.
  • Asynchronous Support: Perform operations asynchronously for improved performance in I/O-bound applications.

Getting Started

Installation

To use the Credici library, first, ensure you have Python installed on your system. Then, install the library using pip or Poetry, depending on your project's setup.

Using pip

pip install credici

Using Poetry

poetry add credici

Quick Start

After installing the Credici library, you can start using it by importing the necessary components and initializing them with your storage solution. Below are the steps to perform common actions:

Initialization

First, initialize the storage and create a CrediciClient:

from credici.sync_.client.client import CrediciClient
from credici.storage.postgres_storage import PostgresStorage

# Initialize storage
storage = PostgresStorage('your_database_url')

# Create a CrediciClient
credici = CrediciClient(storage=storage)

Adding Credits to a User

To add credits to a user's account:

# Add credits to a user
credici.add(user_id="user UUID", amount=100, expire_at=datetime(2025, 1, 1))

Credits can have an expiration date, enabling you to implement time-sensitive promotions or subscription models.

Subtracting Credits from a User

To subtract credits from a user's account:

# Subtract credits from a user
credici.subtract(user_id="user UUID", amount=50)

Retrieving the Current Balance of a User

To retrieve the current balance of a user:

# Retrieve the current balance of a user
balance = credici.get_balance(user_id="user UUID")

Starting a New Cycle for a User

The library supports various types of business models, such as subscription-based and pay-as-you-go systems. Here are examples for each:

Subscription Model

For a subscription model, where users are allocated a specific amount of credits at the beginning of each cycle:

# Start a new cycle for a user with a specific amount of credits for a monthly subscription model
credici.start_cycle(amount=200, time_delta=30, user_id="user UUID")
Pay-As-You-Go Model

For a pay-as-you-go model, where users are only charged based on their usage you can simply start a new monthly cycle with 0 credits and add credits per use:

credici.start_cycle(amount=0, time_delta=30, user_id="user UUID")
# To add credits per use, simply use the add method as needed:
credici.add(user_id="user UUID", amount=1)

Retrieving All Transactions for a User

To retrieve all transactions for a user:

# Retrieve all transactions for a user
transactions = credici.get_all_user_transactions(user_id="user UUID")
for transaction in transactions:
    print(f"Transaction ID: {transaction.transaction_id}, Amount: {transaction.amount}")

Contributing

We welcome contributions from the community! If you're interested in helping improve the Credici library, please check out our contributing guidelines for more information on how to get started.

License

The Credici library is licensed under the MIT License. See the LICENSE file for more details.

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

credici-0.1.5.tar.gz (42.6 kB view details)

Uploaded Source

Built Distribution

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

credici-0.1.5-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

Details for the file credici-0.1.5.tar.gz.

File metadata

  • Download URL: credici-0.1.5.tar.gz
  • Upload date:
  • Size: 42.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.4 Darwin/23.3.0

File hashes

Hashes for credici-0.1.5.tar.gz
Algorithm Hash digest
SHA256 e523402756c574f928c0116dff4aee14fa8bf64868d2731614b90c0c3adc908a
MD5 e8559fa5125b595b3ba023e0f5de5df6
BLAKE2b-256 ed6367720d0a29bff725e6f24a32721da8116bcb6f0dc2dbcc9ee1050cb70258

See more details on using hashes here.

File details

Details for the file credici-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: credici-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 20.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.4 Darwin/23.3.0

File hashes

Hashes for credici-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 89eedd1da60690a8b2174d16c3cc52b95c8bb04bc7749189f6f60f6348af8328
MD5 e9ad295085102474b95d2f23355bac57
BLAKE2b-256 d6c4a6b4c7f1a52905bdf7e0634a258bbf3503b4bcfb25bd2999c519f320f5b2

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