Skip to main content
https://travis-ci.org/MacHu-GWU/apipool-project.svg?branch=master https://codecov.io/gh/MacHu-GWU/apipool-project/branch/master/graph/badge.svg https://img.shields.io/pypi/v/apipool.svg https://img.shields.io/pypi/l/apipool.svg https://img.shields.io/pypi/pyversions/apipool.svg https://img.shields.io/badge/Star_Me_on_GitHub!--None.svg?style=social

Welcome to apipool Documentation

apipool allows developer manipulate multi api key at same time. For example, if single api key has 1k/day quota, then you can register 10 api keys, and let apipool to automatically rotate the key.

Features:

  • automatically rotate apikey.

  • built-in usage statistics, easy to search by time, status, apikey. You can deploy stats collector on any cloud relational database.

  • clean api, minimal code is required to implement complex feature.

Example:

there’s a google geocoding example at: https://github.com/MacHu-GWU/apipool-project/blob/master/examples/google_geocoding.py

Tutorial

Let’s walk through with a twitter api example, the api client we use is python-twitter: https://github.com/bear/python-twitter.

The python-twitter usage:

import twitter

# create api client
api = twitter.Api(
    consumer_key="consumer_key"z,
    consumer_secret="consumer_secret",
    access_token_key="access_token",
    access_token_secret="access_token_secret",
)

# make api call
statuses = api.GetUserTimeline(screen_name="trump")

The apipool usage:

import twitter
from apipool import ApiKey, ApiKeyManager

class TwitterApiKey(ApiKey):
    def __init__(self,
                 consumer_key,
                 consumer_secret,
                 access_token_key,
                 access_token_secret)
        self.consumer_key = consumer_key
        self.consumer_secret = consumer_secret
        self.access_token_key = access_token_key
        self.access_token_secret = access_token_secret

    def user_01_get_primary_key(self):
        return self.access_token_key

    def user_02_create_client(self):
        return twitter.Api(
            consumer_key=self.consumer_key,
            consumer_secret=self.consumer_secret,
            access_token_key=self.access_token_key,
            access_token_secret=self.access_token_secret,
        )

    def user_03_test_usable(self, client):
        statuses = client.GetUserTimeline(screen_name="trump")
        if len(statuses) >= 5:
            return True
        else:
            return False

apikey_data_list = [
    {
        "consumer_key": xxx,
        "consumer_secret": xxx,
        "access_token_key": xxx,
        "access_token_secret": xxx,
    },
    {...},
    {...},
]

apikey_list = [
    TwitterApiKey(**apikey_data)
    for apikey_data in apikey_data_list
]

manager = ApiKeyManager(apikey_list=apikey_list)

DummyClient:

now we can use the manager.dummyclient object like how we use the twitter.Api() object. However, the apikey is automatically rotated, and usage events are also automatically recorded.

manager.check_usable()

# the api key is automatically rotated under the hood
statuses = manager.dummyclient.GetUserTimeline(screen_name="trump")

for _ in range(10):
    manager.dummyclient.GetUserTimeline(screen_name="trump")

StatsCollector:

now we can use manager.stats object to access usage stats, and also query usage events.

>>> manager.stats.usage_count_stats_in_recent_n_seconds()
{"xxx access_token_key": 3, "xxx access_token_key": 4, "xxx access_token_key": 3}

>>> from apipool import StatusCollection
>>> events_list = list(manager.stats.query_event_in_recent_n_seconds(
    n_seconds=24*3600,
    status_id=StatusCollection.c1_Success.id,
))
>>> events_list
[
    Event(apikey_id=xxx, finished_at=datetime(xxx), status_id=xxx),
    Event(...),
    ...
]

Install

apipool is released on PyPI, so all you need is:

$ pip install apipool

To upgrade to latest version:

$ pip install --upgrade apipool

Metadata

Release files for apipool 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for apipool 0.0.2
File Size Uploaded
apipool-0.0.2.tar.gz 25.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for apipool 0.0.2
File Interpreter ABI Platform
apipool-0.0.2-py2-none-any.whl Python 2 none any Details

Total release size: 68.7 kB

Release files / apipool-0.0.2.tar.gz

Download URL apipool-0.0.2.tar.gz
Size 25.2 kB
Tags Source
SHA-256 checksum
How to use checksums
dcc8f01519f2fe6f9e06e560a976dc26c691d40efd088e69a1cf28d58f159613
BLAKE2b-256 checksum
How to use checksums
f6cde02256acb17d5692897f2b7b5b69ba863e3b926a1cd502402308905a8c18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.13

Release files / apipool-0.0.2-py2-none-any.whl

Download URL apipool-0.0.2-py2-none-any.whl
Size 43.5 kB
Tags Python 2
SHA-256 checksum
How to use checksums
22de13f9edbe98e7bcfbcfb1eab5ac429bca6fbda4996f0d59f6df2a2503d8b6
BLAKE2b-256 checksum
How to use checksums
191431b3637c33a9b2374a0d708fb4b259edaa33b4a33f34e93c4eeba9d80d74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.13

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page