Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

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.

Files for apipool, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size apipool-0.0.2-py2-none-any.whl (43.5 kB) File type Wheel Python version py2 Upload date Hashes View hashes
Filename, size apipool-0.0.2.tar.gz (25.2 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page