From 4fe7816a5d6d9b1d9ee171c62c6111b95a925f1f Mon Sep 17 00:00:00 2001 From: Scott Bahling Date: Thu, 6 Aug 2015 17:16:48 +0200 Subject: Add some robustness to http connections - Add a retry loop. Shouldn't be required, but sometimes api.suse.com fails with authentication and a second retry works. - Add verify_ssl option to turn off SSL verification. Needed when facing temporary SSL certificate issues on the api server. --- obsapi/core.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'obsapi') diff --git a/obsapi/core.py b/obsapi/core.py index 174af85..74b8997 100644 --- a/obsapi/core.py +++ b/obsapi/core.py @@ -132,6 +132,8 @@ class ObsApi(object): self.apiurl = apiurl or DEFAULTAPIURL self.__get_auth() self._response = None + self.retries = 3 + self.verify_ssl = True def __get_auth(self): conf = {} @@ -149,9 +151,18 @@ class ObsApi(object): self.auth = None def __api_get(self, api, payload=None): - url = '{0}/{1}'.format(self.apiurl, api) - r = requests.get(url, auth=self.auth, params=payload) - self._response = r + + for attempt in range(self.retries): + url = '{0}/{1}'.format(self.apiurl, api) + r = requests.get(url, + auth=self.auth, + params=payload, + verify=self.verify_ssl) + self._response = r + if self.success: + return r + print("Failed: %s" % self.response) + print("retry: %s" % (attempt + 1)) return r @@ -169,6 +180,7 @@ class ObsApi(object): def get_xml(self, api, payload=None): r = self.__api_get(api, payload) if not self.success: + print self.response return self.default_xml return r.text -- cgit v1.2.3