-
Notifications
You must be signed in to change notification settings - Fork 0
angemilner documentation
class APIKeyLibrarian([db_name='api_keys' [, **kwargs ]])
Create a new APIKeyLibrarian.
parameters:
- db_name : The name of the MongoDB database where the API keys will be stored.
- **kwargs : Additional arguments to be passed to MongoClient. Useful if the MongoDB is anything besides localhost/default.
l= APIKeyLibrarian()
new_api_key( key , provider, max_per_day, s_between_use )
Add a new key to the library.
parameters:
- key : a bson encodable object to be returned when the key is fetched.
-
provider : a string naming the service this key is associated with. Precisely, this is the collections in MongoDB where the key is stored.
It is a best practice to store the same key under sperate providers. For example, the same Twitter tokens may be used to access the Search API and the Streaming API. The same tokens would be stored once under provider 'twitter_search' and once under 'twitter_stream' - max_per_day : the maxiumum number of times the API can be used during a 24 hour period.
- s_between_use : The minimum time that must elapse between key check outs.
check_out_api_key(provider)
Fetch the best API key to use for a certain service.
parameters:
- provider : a string naming the service this key is associated with. Precisely, this is the collections in MongoDB where the key is stored.
returns: If any key is available, it will return a dictionary with the key object that was originally stored via new_api_key
in the key 'key'. That's really confusing.
{ 'key' : 'XXXXXXYYYYZZZyourapikeyWWWWW' } # If there is a key available
OR
{} # If no key is available
set_value(provider, api_key, values)
Set parameter values in the api_key's mongo document
parameters:
-
provider : a string naming the service this key is associated with. Precisely, this is the collections in MongoDB where the key is stored.
*api_key : the api key associated with the document you wish to update *values: a dictionary contain the key-value pairs to be added to the keys information returns: nothing
from angemilner import APIKeyLibrarian
l= APIKeyLibrarian()
l.new_api_key('foo','bar',1,1)
l.set_value('lock':True)
summary()
Get useage statistics of keys in library.
returns: a dictionary with two keys: 'detailed' and 'compact' with useage statistics
from angemilner import APIKeyLibrarian
from pprint import pprint
l= APIKeyLibrarian()
pprint(l.summary()['compact'])
c The pymongo connection
db The pymongo database object