diff options
Diffstat (limited to 'obsapi/httpapi.py')
| -rw-r--r-- | obsapi/httpapi.py | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/obsapi/httpapi.py b/obsapi/httpapi.py index db52050..b73b547 100644 --- a/obsapi/httpapi.py +++ b/obsapi/httpapi.py @@ -2,9 +2,11 @@ # import requests import re +import types +from functools import wraps +from obsapi.null import Null from obsapi.logger import logger from requests.auth import HTTPBasicAuth -from .null import Null try: import osc.conf as osc_conf @@ -33,6 +35,29 @@ class ObsHttpApi(object): self._response = Null() self.retries = 0 self.verify_ssl = True + self._callback_function = None + + def _with_callback(func): + @wraps(func) + def wrapper(inst, *args, **kwargs): + r = func(inst, *args, **kwargs) + inst._do_callback() + return r + return wrapper + + def _do_callback(self): + if self._callback_function is not None: + try: + self._callback_function(self) + except Exception as e: + raise e + + def set_callback(self, func): + if isinstance(func, (types.FunctionType, types.MethodType)): + self._callback_function = func + else: + raise ValueError('Callback expects function or method type, ' + 'got {}'.format(type(func))) @property def __auth(self): @@ -57,6 +82,7 @@ class ObsHttpApi(object): return self._auth[self.apiurl] + @_with_callback def __api_get(self, api, params=None): url = '{0}{1}{2}'.format(self.apiurl, self.rootapi, api) @@ -79,6 +105,7 @@ class ObsHttpApi(object): return r + @_with_callback def __api_put(self, api, data, params=None): url = '{0}{1}{2}'.format(self.apiurl, self.rootapi, api) @@ -91,6 +118,7 @@ class ObsHttpApi(object): return r + @_with_callback def __api_post(self, api, data, params=None): url = '{0}{1}{2}'.format(self.apiurl, self.rootapi, api) |
