Skip to main content

Multiple API Key Manager

Project description

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

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

apipool-0.0.2.tar.gz (25.2 kB view details)

Uploaded Source

Built Distribution

apipool-0.0.2-py2-none-any.whl (43.5 kB view details)

Uploaded Python 2

File details

Details for the file apipool-0.0.2.tar.gz.

File metadata

  • Download URL: apipool-0.0.2.tar.gz
  • Upload date:
  • Size: 25.2 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for apipool-0.0.2.tar.gz
Algorithm Hash digest
SHA256 dcc8f01519f2fe6f9e06e560a976dc26c691d40efd088e69a1cf28d58f159613
MD5 fe51eb27c9c09a3a6861bf42bced5108
BLAKE2b-256 f6cde02256acb17d5692897f2b7b5b69ba863e3b926a1cd502402308905a8c18

See more details on using hashes here.

File details

Details for the file apipool-0.0.2-py2-none-any.whl.

File metadata

  • Download URL: apipool-0.0.2-py2-none-any.whl
  • Upload date:
  • Size: 43.5 kB
  • Tags: Python 2
  • Uploaded using 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

File hashes

Hashes for apipool-0.0.2-py2-none-any.whl
Algorithm Hash digest
SHA256 22de13f9edbe98e7bcfbcfb1eab5ac429bca6fbda4996f0d59f6df2a2503d8b6
MD5 1d8722a918c63119cb59501aa8647105
BLAKE2b-256 191431b3637c33a9b2374a0d708fb4b259edaa33b4a33f34e93c4eeba9d80d74

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page