Package: credici
Credici LibraryThe 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.FeaturesUser 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 StartedInstallationTo 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 pippipinstallcrediciUsing PoetrypoetryaddcrediciQuick StartAfter 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:InitializationFirst, initialize the storage and create aCrediciClient:fromcredici.sync_.client.clientimportCrediciClientfromcredici.storage.postgres_storageimportPostgresStorage# Initialize storagestorage=PostgresStorage('your_database_url')# Create a CrediciClientcredici=CrediciClient(storage=storage)Adding Credits to a UserTo add credits to a user's account:# Add credits to a usercredici.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 UserTo subtract credits from a user's account:# Subtract credits from a usercredici.subtract(user_id="user UUID",amount=50)Retrieving the Current Balance of a UserTo retrieve the current balance of a user:# Retrieve the current balance of a userbalance=credici.get_balance(user_id="user UUID")Starting a New Cycle for a UserThe library supports various types of business models, such as subscription-based and pay-as-you-go systems. Here are examples for each:Subscription ModelFor 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 modelcredici.start_cycle(amount=200,time_delta=30,user_id="user UUID")Pay-As-You-Go ModelFor 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 UserTo retrieve all transactions for a user:# Retrieve all transactions for a usertransactions=credici.get_all_user_transactions(user_id="user UUID")fortransactionintransactions:print(f"Transaction ID:{transaction.transaction_id}, Amount:{transaction.amount}")ContributingWe welcome contributions from the community! If you're interested in helping improve the Credici library, please check out ourcontributing guidelinesfor more information on how to get started.LicenseThe Credici library is licensed under the MIT License. See the LICENSE file for more details.