From da1e9fd7c1236b17590946aff5a7ae638d50c6df Mon Sep 17 00:00:00 2001 From: Scott Bahling Date: Wed, 1 Nov 2017 12:13:45 +0100 Subject: Only call osc.conf.get_config() when needed osc.conf.get_config() can cause calls to password managers like kwallet. Move this action from module init to the functions that actually need to access the OBS api and require authentication. We also cache the results in a local variable now. --- obsapi/core.py | 6 ------ obsapi/httpapi.py | 20 +++++++++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/obsapi/core.py b/obsapi/core.py index 5200efe..090c953 100644 --- a/obsapi/core.py +++ b/obsapi/core.py @@ -8,12 +8,6 @@ from obsapi.sourceapi import ObsSourceApi from obsapi.buildapi import ObsBuildApi from obsapi.formatter import Formatter -try: - import osc.conf as osc_conf - osc_conf.get_config() -except: - osc_conf = None - LSItem = namedtuple('LSItem', 'name md5 size mtime') Directory = namedtuple('Directory', 'name rev vrev srcmd5') diff --git a/obsapi/httpapi.py b/obsapi/httpapi.py index 8334224..8ca49e8 100644 --- a/obsapi/httpapi.py +++ b/obsapi/httpapi.py @@ -6,7 +6,6 @@ from null import Null try: import osc.conf as osc_conf - osc_conf.get_config() except: osc_conf = None @@ -20,15 +19,20 @@ class ObsHttpApi(object): def __init__(self, apiurl=None): self.apiurl = apiurl or DEFAULTAPIURL - self.__get_auth() + self._auth = {} self._response = Null() self.retries = 3 self.verify_ssl = True + @property + def __auth(self): + return self._auth.get(self.apiurl, self.__get_auth()) + def __get_auth(self): conf = {} if osc_conf: try: + osc_conf.get_config() conf = osc_conf.get_apiurl_api_host_options(self.apiurl) except: pass @@ -36,9 +40,11 @@ class ObsHttpApi(object): user = conf.get('user', None) password = conf.get('pass', None) if user and password: - self.auth = HTTPBasicAuth(user, password) + self._auth[self.apiurl] = HTTPBasicAuth(user, password) else: - self.auth = None + self._auth[self.apiurl] = None + + return self._auth[self.apiurl] def __api_get(self, api, params=None): @@ -46,7 +52,7 @@ class ObsHttpApi(object): def try_get(): r = requests.get(url, - auth=self.auth, + auth=self.__auth, params=params, verify=self.verify_ssl) self._response = r @@ -68,7 +74,7 @@ class ObsHttpApi(object): url = '{0}{1}{2}'.format(self.apiurl, self.rootapi, api) r = requests.put(url, - auth=self.auth, + auth=self.__auth, data=data, params=params, verify=self.verify_ssl) @@ -80,7 +86,7 @@ class ObsHttpApi(object): url = '{0}{1}{2}'.format(self.apiurl, self.rootapi, api) r = requests.post(url, - auth=self.auth, + auth=self.__auth, data=data, params=params, verify=self.verify_ssl) -- cgit v1.2.3