Skip to content

getoctane/octane-python

Repository files navigation

Octane Python Library

Version GitHub Actions status

Octane

The Octane Python library provides programmatic access to the Octane API for Python applications.


Getting started

First, install the package (octane):

pip install --upgrade octane

Next, obtain an API key from within the Octane portal, and set it in your environment:

export OCTANE_API_KEY="<insert_octane_api_key_here>"

Then, from within your application, import the module:

import os, octane
octane.api_key = os.getenv("OCTANE_API_KEY")

Example apps

The following demo applications found in the examples/ directory display how to use the Octane Python library in real-world settings:

Making API calls

The octane instance provides programmatic access to the Octane API.

Customers API

The Customer namespace on the octane instance provides the ability to make calls to the Octane Customers API.

Example: Creating a new customer

customer_name = "r2d2"

octane.Customer.create(
    name=customer_name,
    measurement_mappings=[{
        "label": "customer_name",
        "value_regex": customer_name
    }]
)

Example: Subscribe a customer to a price plan

customer_name = "r2d2"
price_plan_name = "droidplan"

octane.Customer.create_subscription(customer_name, price_plan_name=price_plan_name)

Meters API

The Meter namespace on the octane instance provides the ability to make calls to the Octane Meters API.

Example: Creating a new meter

meter_name = "droidrepairs"

octane.Meter.create(
    name=meter_name,
    meter_type="COUNTER",
    is_incremental=True,
    expected_labels=["customer_name"]
)

Price Plans API

The PricePlan namespace on the octane instance provides the ability to make calls to the Octane Price Plans API.

Example: Creating a new price plan

price_plan_name = "droidplan"
price_plan_rate = 10000  # $100.00
meter_name = "droidrepairs"

octane.PricePlan.create(
    name=price_plan_name,
    period="month",
    metered_components=[{
        "meter_name": meter_name,
        "price_scheme": {
            "prices": [{
                "price": price_plan_rate
            }],
            "scheme_type": "FLAT"
        }
    }]
)

Measurements API

The Measurement namespace on the octane instance provides the ability to make calls to the Octane Measurements API.

Example: Sending a measurement

meter_name = "droidrepairs"
customer_name = "r2d2"

octane.Measurement.create(
    meter_name=meter_name,
    customer_name=customer_name,
    value=1,
)

Example: Sending a batch of measurements

meter_name = "droidrepairs"
customer_name = "r2d2"

octane.Measurement.create_multi(
    measurements=[
        {
            "meter_name": meter_name,
            "customer_name": customer_name,
            "value": 1,
            "time": "2023-01-01T00:00:00",
        },
        {
            "meter_name": meter_name,
            "customer_name": customer_name,
            "value": 1,
            "time": "2023-01-02T00:00:00",
        },
    ]
)

Development

Run the following command to set up the development virtualenv:

make

Run all tests on all supported Python versions:

make test

Run all tests for a specific Python version (modify -e according to your Python target):

TOX_ARGS="-e py37" make test

Run all tests in a single file:

TOX_ARGS="-e py37 -- tests/api_resources/abstract/test_updateable_api_resource.py" make test

Run a single test suite:

TOX_ARGS="-e py37 -- tests/api_resources/abstract/test_updateable_api_resource.py::TestUpdateableAPIResource" make test

Run a single test:

TOX_ARGS="-e py37 -- tests/api_resources/abstract/test_updateable_api_resource.py::TestUpdateableAPIResource::test_save" make test

Run the linter with:

make lint

The library uses Black for code formatting. Code must be formatted with Black before PRs are submitted, otherwise CI will fail. Run the formatter with:

make fmt

Contributing

Contributions are welcome!

Prior to submitting a pull request, please check the list of open issues. If there is not an existing issue related to your changes, please open a new issue to first discuss your thoughts with the project maintainers.

About

Python library providing programatic access to the Octane API

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 5