copyright
(c) 2016 by Steelhive, LLC
license
MIT, see LICENSE for more details
The core python bindings for kvp.io
Accessing account information:
import kvpio
kvpio.api_key = '123abc'
account = kvpio.Account().get()
# {"id": "kvp.io", "email": "support@kvp.io", "reads": 87, "size": 124}
Writing to your bucket:
data = {
'foo': 123,
'bar': True,
'baz': {
'boo': 321,
'far': False,
'faz': [1, 2, 3]
}
}
bucket = kvpio.Bucket()
bucket.set('my_key', data)
Reading nested data from your bucket:
data = bucket.get('my_key/baz/faz')
# [1, 2, 3]
imports
import os
import requests
import json as JSON
The API key to use for authentication
api_key = None
The API version in use, e.g., v1
api_ver = 'v1'
The base url of the API, e.g., https://api.kvp.io
api_base = 'https://api.kvp.io'
Toggle auto-parsing of JSON, default: False
load_json = False
The supported HTTP methods
http_methods = [
'get',
'post',
'delete'
]
Represents an API endpoint from the client-side's perspective.
This class should not need to be instantiated manually. It is essentially a convenience wrapper for the requests library with a few properties.
class Endpoint(object):
name = None
def __init__(self, name):
self.name = name
Gets a requests
compatible Basic Auth tuple.
@property
def auth(self):
return (api_key, '')
Gets the compiled target url of the endpoint.
@property
def url(self):
return '/'.join([api_base, api_ver, self.name])
A simple request wrapper. Used by get, post, etc. methods.
params:
returns:
def request(self, method, key='', params=None, json=None):
if method not in http_methods:
raise Exception('Unsupported method attempted: {}'.format(method))
cause an exception if the data can't be jsonified
JSON.dumps(json)
response = getattr(requests, method)(
'{}/{}'.format(self.url, key),
params=params,
json=json,
auth=self.auth
)
if load_json:
try:
return (response.status_code, JSON.loads(response.text))
except:
pass
return (response.status_code, response.text)
Provides access to account information.
class Account(Endpoint):
def __init__(self):
super(Account, self).__init__('account')
Requests the current account information as a JSON string, e.g.,
{
'id': 'Account Name',
'email': 'you@domain.tld',
'reads': 154,
'size': 140762
}
def get(self):
return self.request('get')
Provides access to bucket (key/value pair) storage.
class Bucket(Endpoint):
def __init__(self):
super(Bucket, self).__init__('bucket')
def list(self):
return self.request('get')
Retrieves either the value stored at the key.
params:
returns:
def get(self, key):
return self.request('get', key)
Assigns a value to the key path.
params:
returns:
def set(self, key, data):
return self.request('post', key, json=data)
Deletes a key, it's value, and all values beneath it.
params:
returns:
def delete(self, key):
return self.request('delete', key)
Provides access to templates storage.
class Templates(Endpoint):
def __init__(self):
super(Templates, self).__init__('templates')
Retrieves the current list of templates.
def list(self):
return self.request('get')
Retrieves the template stored at key.
The template is rendered with data pulled from the account's bucket using the Jinja2 engine.
If raw
is `True, returns the template un-rendered.
If data
is provided, it is used to override bucket values.
params:
returns:
def get(self, key, data=None, raw=False):
return self.request('get', key, params={'raw': raw}, json=data)
Stores the template document under key.
params:
returns:
def set(self, key, template):
return self.request('post', key, json=template)
Deletes a key and it's template document.
params:
returns:
def delete(self, key):
return self.request('delete', key)