Skip to main content

No project description provided

Project description

Spackle Python Library

CI pypi

The Spackle Python library provides optimized access to billing aware flags created on the Spackle platform.

Documentation

See the Python API docs.

Setup

Install the Spackle library

pip install -U spackle-python

Configure your environment

In order to use Spackle, you need to configure your secret key on the spackle module. You can find your secret key in Spackle app settings page.

import spackle
spackle.api_key = "<api key>"

Usage

Pricing tables

Fetch a pricing table

pricing_table = spackle.PricingTable.retrieve("abcde123")

Pricing table object

{
  id: string
  name: string
  intervals: string[]
  products: {
    id: string
    description: string
    name: string
    features: {
      id: string
      name: string
      key: string
      type: number
      value_flag: boolean
      value_limit: number | null
    }[]
    prices: {
      month?: {
        id: string
        unit_amount: number
        currency: string
      }
      year?: {
        id: string
        unit_amount: number
        currency: string
      }
    }
  }[]
}

Entitlements

Fetch a customer

Spackle uses stripe ids as references to customer features.

customer = spackle.Customer.retrieve("cus_00000000")

Verify feature access

customer.enabled("feature_key")

Fetch a feature limit

customer.limit("feature_key")

Examine a customer's subscriptions

A customer's current subscriptions are available on the subscriptions property. These are valid stripe.Subscription objects as defined in the Stripe Python library.

customer.subscriptions

Waiters

There is a brief delay between when an action takes place in Stripe and when it is reflected in Spackle. To account for this, Spackle provides a waiters module that can be used to wait for a Stripe object to be updated and replicated.

  1. Wait for a customer to be created
    spackle.wait_for_customer("cus_00000000")
    
  2. Wait for a subscription to be created
    spackle.wait_for_subscription("cus_000000000", "sub_00000000")
    
  3. Wait for a subscription to be updated
    spackle.wait_for_subscription("cus_000000000", "sub_00000000", status="active")
    

These will block until Spackle is updated with the latest information from Stripe or until a timeout occurs.

Usage in development environments

In production, Spackle requires a valid Stripe customer. However, that is not development environments where state needs to be controlled. As an alternative, you can use a file store to test your application with seed data.

/app/spackle.json

{
  "cus_000000000": {
    "features": [
      {
        "type": 0,
        "key": "flag_feature",
        "value_flag": true
      },
      {
        "type": 1,
        "key": "limit_feature",
        "value_limit": 100
      }
    ],
    "subscriptions": [
      {
        "id": "sub_000000000",
        "status": "trialing",
        "quantity": 1
      }
    ]
  }
}

Then configure the file store in your application:

spackle.set_store(spackle.FileStore("/app/spackle.json"))

Usage in testing environments

In production, Spackle requires a valid Stripe customer. However, that is not ideal in testing or some development environments. As an alternative, you can use an in-memory store to test your application with seed data.

spackle.set_store(spackle.MemoryStore())
spackle.get_store().set_customer_data("cus_000000000", {
  "features": [
    {
      "type": 0,
      "key": "flag_feature",
      "value_flag": True,
    },
    {
      "type": 1,
      "key": "limit_feature",
      "value_limit": 100,
    },
  ],
  "subscriptions": [
     {
       "id": "sub_000000000",
       "status": "trialing",
       "quantity": 1,
     }
  ]
})

Note: The in-memory store is not thread-safe and state will reset on each application restart.

Logging

The Spackle Python library emits logs as it performs various internal tasks. You can control the verbosity of Spackle's logging a few different ways:

  1. Set the environment variable SPACKLE_LOG to the value debug, info, or warn

    $ export SPACKLE_LOG=debug
    
  2. Set spackle.log:

    import spackle
    spackle.log = 'debug'
    
  3. Enable it through Python's logging module:

    import logging
    logging.basicConfig()
    logging.getLogger('spackle').setLevel(logging.DEBUG)
    

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

spackle_python-0.0.45.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

spackle_python-0.0.45-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file spackle_python-0.0.45.tar.gz.

File metadata

  • Download URL: spackle_python-0.0.45.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.9 Darwin/23.3.0

File hashes

Hashes for spackle_python-0.0.45.tar.gz
Algorithm Hash digest
SHA256 c1fcc873851e3574207aaf2ad49ad7bf2020c592266d59401719ac91706d2118
MD5 645a397242d49086e37108978ff12026
BLAKE2b-256 d4d4e895a62d0d72bbd3999627f831d136c029f4f3564fb0ff9290dfccec55fd

See more details on using hashes here.

File details

Details for the file spackle_python-0.0.45-py3-none-any.whl.

File metadata

  • Download URL: spackle_python-0.0.45-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.9 Darwin/23.3.0

File hashes

Hashes for spackle_python-0.0.45-py3-none-any.whl
Algorithm Hash digest
SHA256 e7908cf2ba0d5129b376a9aa06fb79ad35372be205cefd0ec2ede86ef19085a7
MD5 c04736cf0fac43cf3a6f9bfdc0b2ea5b
BLAKE2b-256 ac17ba71a598a794539e19363762b5755dd70639c0b02d859ef22f2a5231d72b

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