locksmith_client.py

""" Remote API wrapper for Locksmith """ import urllib import urllib2 import json LOCKSMITH_HOST = 'https://locksmith.artsalliancemedia.com' def setup_auth(uri, username, passwd): """ Set the basic auth handler for all calls """ passwd_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() passwd_mgr.add_password(None, uri, username, passwd) handler = urllib2.HTTPBasicAuthHandler(passwd_mgr) return urllib2.build_opener(handler) class AuthorisationError(Exception): """ Raised when authentication fails with Locksmith """ pass class Locksmith(object): """ Locksmith wrapper class for encapsulating communication with the Locksmith service """ def __init__(self, username, passwd, host=LOCKSMITH_HOST): self._host = host self._username = username self._passwd = passwd self.url_opener = setup_auth(host, self._username, self._passwd) @property def version(self): """ Returns the Locksmith service's current version """ response = self.url_opener.open(self._host + '/api') if response.code == 200: return json.loads(response.read())['version'] def get_kdm(self, kdm_uuid): """ Returns a dictionary of a KDM and its metadata from a UUID. If no KDM is found, then an empty dictionary is returned. """ response = self.url_opener.open(self._host + '/api/kdm/uuid/' + kdm_uuid) if response.code == 200: return json.loads(response.read()) def save_kdm(self, kdm_xml): """ Uploads a KDM to Locksmith """ data = urllib.urlencode({'kdm' : kdm_xml}) #response = urllib2.urlopen(url, data) response = self.url_opener.open(self._host + '/api/kdm/save/', data) if response.code == 200: return json.loads(response.read()) def get_kdms_from_thumbprint(self, thumbprint): """ Returns a list of KDM dictionaries who are both not expired and match to the given thumbprint """ response = self.url_opener.open(self._host + '/api/kdm/thumbprint/' + thumbprint) if response.code == 200: return json.loads(response.read()) if __name__ == '__main__': locksmith = Locksmith('username', 'password') print locksmith.version print locksmith.get_kdms_from_thumbprint('98a48a64c18f8e7f5df4c4036a188c1e5a8f59e4')
Uploading and Accessing KDMs in Locksmith

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.