Created
January 26, 2012 13:20
-
-
Save mjohnsullivan/1682725 to your computer and use it in GitHub Desktop.
Uploading and Accessing KDMs in Locksmith
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment