Skip to main content

RTB House SDK

Project description

Overview

This library provides an easy-to-use Python interface to RTB House API. It allows you to read and manage your campaigns settings, browse offers, download statistics etc.

API docs: https://api.panel.rtbhouse.com/api/docs

Installation

RTB House SDK can be installed with pip:

$ pip install rtbhouse_sdk

Usage example

Let’s write a script which fetches campaign stats (imps, clicks, postclicks) and shows the result as a table (using tabulate library).

First set up virtualenv and install requirements:

$ pip install rtbhouse_sdk tabulate

Create an API token in the RTB House Clients Panel (https://panel.rtbhouse.com/user/api-tokens) and copy it to a safe place.

Now you can initialize the API token in local file storage. Paste your API token in prompt after running the command below:

$ python -m rtbhouse_sdk.api_tokens init-json
Paste your token: PASTE_YOUR_TOKEN_HERE

The authentication token is now ready to use. As long as you use it with the client frequently enough, the SDK will keep the token valid and rotate it automatically (for details see API Token Authentication below):

from datetime import date, timedelta
from operator import attrgetter

from rtbhouse_sdk.api_tokens import ApiTokenManager, JsonFileApiTokenStorage
from rtbhouse_sdk.client import Client
from rtbhouse_sdk.schema import CountConvention, StatsGroupBy, StatsMetric
from tabulate import tabulate

if __name__ == "__main__":
    storage = JsonFileApiTokenStorage()
    auth = ApiTokenManager(storage)

    with Client(auth=auth) as api:
        advertisers = api.get_advertisers()
        day_to = date.today()
        day_from = day_to - timedelta(days=30)
        group_by = [StatsGroupBy.DAY]
        metrics = [
            StatsMetric.IMPS_COUNT,
            StatsMetric.CLICKS_COUNT,
            StatsMetric.CAMPAIGN_COST,
            StatsMetric.CONVERSIONS_COUNT,
            StatsMetric.CTR
        ]
        stats = api.get_rtb_stats(
            advertisers[0].hash,
            day_from,
            day_to,
            group_by,
            metrics,
            count_convention=CountConvention.ATTRIBUTED_POST_CLICK,
        )
    columns = group_by + metrics
    data_frame = [
        [getattr(row, c.name.lower()) for c in columns]
        for row in reversed(sorted(stats, key=attrgetter("day")))
    ]
    print(tabulate(data_frame, headers=columns))

Authentication methods

The SDK supports several authentication methods.

API Token Authentication

API Tokens provide a secure and manageable way to grant programmatic access without using a login and password. This allows you to connect your integrations to the API, without specific user credentials.

API tokens have a limited lifetime and must be periodically rotated and actively used to prevent expiration. For more details on the API token lifecycle, see the LEARN MORE link in the Clients Panel API Tokens section.

For automatic token lifecycle management, use ApiTokenManager / AsyncApiTokenManager as authentication classes.

These classes support per-request token resolution and allow the token to be stored/retrieved with a storage backend. Currently, for production use, SDK provides a JSON file storage backend.

When used with a storage backend, the SDK can:

  • rotate the token automatically when it enters the rotation window and overwrite the stored token with the new one

  • keep the token valid without manual maintenance

Rotation eligibility is checked on every request, which means that for typical integrations with regular traffic, you can configure the token once and let the SDK manage it automatically.

For integrations that do not make requests frequently enough to trigger automatic rotation during the rotation window (e.g. at least once a day), use the keep-alive-json CLI command (see CLI for API Tokens below) scheduled with cron or a similar tool.

CLI for API Tokens

A CLI interface is available to manage API tokens from the command line:

$ python -m rtbhouse_sdk.api_tokens <command> [options]

init-json

Initialize JSON file storage with API Token.

First create your API token in the Clients Panel (https://panel.rtbhouse.com/user/api-tokens).

Then provide the token via stdin or interactively when prompted:

$ python -m rtbhouse_sdk.api_tokens init-json
Paste your token: PASTE_YOUR_TOKEN_HERE
$ python -m rtbhouse_sdk.api_tokens init-json <<< "$API_TOKEN"
$ python -m rtbhouse_sdk.api_tokens init-json < token.txt
$ python -m rtbhouse_sdk.api_tokens init-json --path /custom/path/to/token.json

keep-alive-json

Keep alive a token stored in JSON file storage.

This command refreshes the token’s last activity timestamp and optionally rotates the token, if it is in the rotation window.

While you can run this command manually whenever needed, it is recommended to schedule it to run periodically at least once a day, to ensure the token remains active and does not expire:

$ python -m rtbhouse_sdk.api_tokens keep-alive-json
$ python -m rtbhouse_sdk.api_tokens keep-alive-json --skip-auto-rotate
$ python -m rtbhouse_sdk.api_tokens keep-alive-json --path /custom/path/to/token.json

API Token Storage Backends

Storage backends are responsible for persisting API tokens.

Storage backends must be initialized before use. API token manager classes provide a configure(token) method to initialize the token programmatically. This method will fetch the token details and save them to storage.

JSON File Storage

Persist tokens on disk using a JSON file. Use sync JsonFileApiTokenStorage or async AsyncJsonFileApiTokenStorage classes.

Storage can be initialized by manager.configure(token) method call or by using the init-json CLI command.

Sync example:

from rtbhouse_sdk.api_tokens import ApiTokenManager, JsonFileApiTokenStorage
from rtbhouse_sdk.client import Client

storage = JsonFileApiTokenStorage()
auth = ApiTokenManager(storage)

with Client(auth=auth) as api:
    info = api.get_user_info()

Async example:

from rtbhouse_sdk.api_tokens import AsyncApiTokenManager, AsyncJsonFileApiTokenStorage
from rtbhouse_sdk.client import AsyncClient

storage = AsyncJsonFileApiTokenStorage()
auth = AsyncApiTokenManager(storage)

async with AsyncClient(auth=auth) as api:
    info = await api.get_user_info()

In-Memory Storage

For simple scenarios where true persistence is not required.

Use sync InMemoryApiTokenStorage or async AsyncInMemoryApiTokenStorage classes.

from rtbhouse_sdk.api_tokens import ApiTokenManager, InMemoryApiTokenStorage
from rtbhouse_sdk.client import Client

storage = InMemoryApiTokenStorage(None)
manager = ApiTokenManager(storage)
manager.configure("your_api_token")  # fetches token details and saves to storage

with Client(auth=manager) as api:
    info = api.get_user_info()

Custom Storage Backend

You can implement your own storage backend by subclassing ApiTokenStorage (sync) or AsyncApiTokenStorage (async). Each backend must implement three methods:

  • acquire_exclusive_for_update() — a context manager ensuring exclusive access to the storage

  • load() — load and return the current ApiToken

  • save(api_token) — persist the given ApiToken

API Token Auth (without automatic rotation and storage management)

Use ApiTokenAuth if you want to authenticate with a fixed API token without automatic rotation or storage management.

from rtbhouse_sdk.client import ApiTokenAuth, Client

auth = ApiTokenAuth("your_api_token")

with Client(auth=auth) as api:
    info = api.get_user_info()
from rtbhouse_sdk.client import ApiTokenAuth, AsyncClient

auth = ApiTokenAuth("your_api_token")

async with AsyncClient(auth=auth) as api:
    info = await api.get_user_info()

Basic Auth

Username and password authentication using BasicAuth.

from rtbhouse_sdk.client import BasicAuth, Client

auth = BasicAuth(username="jdoe", password="abcd1234")

with Client(auth=auth) as api:
    info = api.get_user_info()

Basic Token Auth

Token-based authentication using BasicTokenAuth.

from rtbhouse_sdk.client import BasicTokenAuth, Client

auth = BasicTokenAuth("your_basic_token")

with Client(auth=auth) as api:
    info = api.get_user_info()

License

MIT

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

rtbhouse_sdk-15.1.0.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

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

rtbhouse_sdk-15.1.0-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file rtbhouse_sdk-15.1.0.tar.gz.

File metadata

  • Download URL: rtbhouse_sdk-15.1.0.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for rtbhouse_sdk-15.1.0.tar.gz
Algorithm Hash digest
SHA256 59964cf0fd58cb1210b0dc0aa8bea0e8a1666440d4448883616bf14171e1bc2d
MD5 5f054f61a2371c2862487b18a6ff5f7c
BLAKE2b-256 4726b47e7ddc904d997e3c7a994f952aad9b193be007dd75a23ccbfb840a9889

See more details on using hashes here.

Provenance

The following attestation bundles were made for rtbhouse_sdk-15.1.0.tar.gz:

Publisher: ci.yaml on rtbhouse-apps/rtbhouse-python-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rtbhouse_sdk-15.1.0-py3-none-any.whl.

File metadata

  • Download URL: rtbhouse_sdk-15.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for rtbhouse_sdk-15.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b40d79684b6386f79c05848f969475dcba9cf37d03a1bedcf58a8b4ef07d2183
MD5 944765cbd59332c8ea314fc89bdc70ba
BLAKE2b-256 37939e3ba771691a440ee9f5b05fdd81c4e9a160217c0654984578ab318f5610

See more details on using hashes here.

Provenance

The following attestation bundles were made for rtbhouse_sdk-15.1.0-py3-none-any.whl:

Publisher: ci.yaml on rtbhouse-apps/rtbhouse-python-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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