From 5e77fe880893c8c001358c98dcc6d86ca55e6f9f Mon Sep 17 00:00:00 2001 From: Liraz Siri Date: Wed, 12 Dec 2012 01:18:16 +0200 Subject: [PATCH] refactored tklbam to use pycurl-wrapper's API class Conflicts: debian/control hub.py --- debian/control | 2 +- hub.py | 53 ++++++++++++++++++-------------------------------- 2 files changed, 20 insertions(+), 35 deletions(-) diff --git a/debian/control b/debian/control index 78186f5..1ab8e1f 100644 --- a/debian/control +++ b/debian/control @@ -7,6 +7,6 @@ Standards-Version: 3.6.1 Package: tklbam Architecture: all -Depends: python (>= 2.4), tklbam-squid, tklbam-duplicity (>= 0.6.18), tklbam-python-boto (>= 2.3.0), turnkey-pylib (>= 0.3), turnkey-version, python-crypto, pycurl-wrapper, python-simplejson, ca-certificates, ntpdate +Depends: python (>= 2.4), tklbam-squid, tklbam-duplicity (>= 0.6.18), tklbam-python-boto (>= 2.3.0), turnkey-pylib (>= 0.3), turnkey-version, python-crypto, pycurl-wrapper (>= 1.1), python-simplejson, ca-certificates, ntpdate Section: misc Description: TurnKey Linux Backup and Migration agent diff --git a/hub.py b/hub.py index 5938804..08987ad 100644 --- a/hub.py +++ b/hub.py @@ -82,11 +82,10 @@ import base64 import tempfile -import simplejson as json from datetime import datetime import executil -from pycurl_wrapper import Curl +from pycurl_wrapper import API as _API from utils import AttrDict class Error(Exception): @@ -103,30 +102,19 @@ def __init__(self, desc=DESC): class InvalidBackupError(Error): pass -class API: - ALL_OK = 200 - CREATED = 201 - DELETED = 204 +class API(_API): + def request(self, method, url, attrs={}, headers={}): + try: + return _API.request(self, method, url, attrs, headers) + except self.Error, e: + if e.name == "BackupRecord.NotFound": + raise InvalidBackupError(e.description) - @classmethod - def request(cls, method, url, attrs={}, headers={}): - c = Curl(url, headers) - func = getattr(c, method.lower()) - func(attrs) - - if not c.response_code in (cls.ALL_OK, cls.CREATED, cls.DELETED): - name, description = c.response_data.split(":", 1) - - if name == "BackupRecord.NotFound": - raise InvalidBackupError(description) - - if name in ("BackupAccount.NotSubscribed", - "BackupAccount.NotFound"): + if e.name in ("BackupAccount.NotSubscribed", + "BackupAccount.NotFound"): raise NotSubscribedError() - raise Error(c.response_code, name, description) - - return json.loads(c.response_data) + raise class BackupRecord(AttrDict): @staticmethod @@ -153,6 +141,8 @@ def __init__(self, response): # no interface for this in tklbam, so not returned from hub self.sessions = [] + AttrDict.__init__(self) + class Credentials(AttrDict): def __init__(self, response): self.accesskey = response['accesskey'] @@ -160,10 +150,10 @@ def __init__(self, response): self.usertoken = response['usertoken'] self.producttoken = response['producttoken'] + AttrDict.__init__(self) + class Backups: API_URL = os.getenv('TKLBAM_APIURL', 'https://hub.turnkeylinux.org/api/backup/') - API_HEADERS = {'Accept': 'application/json'} - Error = Error def __init__(self, subkey=None): @@ -171,20 +161,15 @@ def __init__(self, subkey=None): raise Error("no APIKEY - tklbam not initialized") self.subkey = subkey + self.api = API() def _api(self, method, uri, attrs={}): - headers = self.API_HEADERS.copy() - headers['subkey'] = str(self.subkey) - - # workaround: http://redmine.lighttpd.net/issues/1017 - if method == "PUT": - headers['Expect'] = '' - - return API.request(method, self.API_URL + uri, attrs, headers) + headers = { 'subkey': str(self.subkey) } + return self.api.request(method, self.API_URL + uri, attrs, headers) @classmethod def get_sub_apikey(cls, apikey): - response = API.request('GET', cls.API_URL + 'subkey/', {'apikey': apikey}, cls.API_HEADERS) + response = API().request('GET', cls.API_URL + 'subkey/', {'apikey': apikey}) return response['subkey'] def get_credentials(self):