diff --git a/bugwarrior/services/__init__.py b/bugwarrior/services/__init__.py index ec0c528d6..21862bd9d 100644 --- a/bugwarrior/services/__init__.py +++ b/bugwarrior/services/__init__.py @@ -488,6 +488,23 @@ def __repr__(self): return '<%s>' % self.__unicode__() +class ServiceClient(object): + @staticmethod + def json_response(response, url): + # If we didn't get good results, just bail. + if response.status_code != 200: + raise IOError( + "Non-200 status code %r; %r; %r" % ( + response.status_code, url, response.text, + )) + if callable(response.json): + # Newer python-requests + return response.json() + else: + # Older python-requests + return response.json + + def _aggregate_issues(conf, main_section, target, queue, service_name): """ This worker function is separated out from the main :func:`aggregate_issues` func only so that we can use multiprocessing diff --git a/bugwarrior/services/activecollab2.py b/bugwarrior/services/activecollab2.py index a6cbb13ac..c92a80958 100644 --- a/bugwarrior/services/activecollab2.py +++ b/bugwarrior/services/activecollab2.py @@ -5,11 +5,11 @@ import requests from twiggy import log -from bugwarrior.services import IssueService, Issue +from bugwarrior.services import IssueService, Issue, ServiceClient from bugwarrior.config import die -class ActiveCollab2Client(object): +class ActiveCollab2Client(ServiceClient): def __init__(self, url, key, user_id, projects): self.url = url self.key = key @@ -71,21 +71,7 @@ def call_api(self, uri): 'path_info': uri, 'format': 'json'} - response = requests.get(url, params=params) - - # And.. if we didn't get good results, just bail. - if response.status_code != 200: - raise IOError( - "Non-200 status code %r; %r; %r" % ( - response.status_code, url, response.text, - ) - ) - if callable(response.json): - # Newer python-requests - return response.json() - else: - # Older python-requests - return response.json + return self.json_response(requests.get(url, params=params), url) class ActiveCollab2Issue(Issue): diff --git a/bugwarrior/services/bitbucket.py b/bugwarrior/services/bitbucket.py index eef2b7b28..e969d8960 100644 --- a/bugwarrior/services/bitbucket.py +++ b/bugwarrior/services/bitbucket.py @@ -2,7 +2,7 @@ from twiggy import log from bugwarrior import data -from bugwarrior.services import IssueService, Issue +from bugwarrior.services import IssueService, Issue, ServiceClient from bugwarrior.config import asbool, die @@ -55,7 +55,7 @@ def get_default_description(self): ) -class BitbucketService(IssueService): +class BitbucketService(IssueService, ServiceClient): ISSUE_CLASS = BitbucketIssue CONFIG_PREFIX = 'bitbucket' @@ -145,21 +145,7 @@ def get_data(self, url, **kwargs): elif 'basic' in self.auth: kwargs['auth'] = self.auth['basic'] - response = requests.get(api + url, **kwargs) - - # And.. if we didn't get good results, just bail. - if response.status_code != 200: - raise IOError( - "Non-200 status code %r; %r; %r" % ( - response.status_code, url, response.text, - ) - ) - if callable(response.json): - # Newer python-requests - return response.json() - else: - # Older python-requests - return response.json + return self.json_response(requests.get(api + url, **kwargs), url) @classmethod def validate_config(cls, config, target): diff --git a/bugwarrior/services/gitlab.py b/bugwarrior/services/gitlab.py index d4cc4ac52..9861075f4 100644 --- a/bugwarrior/services/gitlab.py +++ b/bugwarrior/services/gitlab.py @@ -7,7 +7,7 @@ from twiggy import log from bugwarrior.config import asbool, die -from bugwarrior.services import IssueService, Issue +from bugwarrior.services import IssueService, Issue, ServiceClient class GitlabIssue(Issue): @@ -180,7 +180,7 @@ def get_default_description(self): ) -class GitlabService(IssueService): +class GitlabService(IssueService, ServiceClient): ISSUE_CLASS = GitlabIssue CONFIG_PREFIX = 'gitlab' @@ -281,17 +281,7 @@ def _fetch(self, tmpl, **kwargs): requests.packages.urllib3.disable_warnings() response = requests.get(url, headers=headers, verify=self.verify_ssl, **kwargs) - if callable(response.json): - json_res = response.json() - else: - json_res = response.json - - if response.status_code != 200: - raise IOError( - "Non-200 status code %r; %r; %r" %( - response.status_code, url, json_res)) - - return json_res + return self.json_response(response, url) def _fetch_paged(self, tmpl): params = { diff --git a/bugwarrior/services/redmine.py b/bugwarrior/services/redmine.py index 7c60df899..45a27c82f 100644 --- a/bugwarrior/services/redmine.py +++ b/bugwarrior/services/redmine.py @@ -3,10 +3,10 @@ from twiggy import log from bugwarrior.config import die -from bugwarrior.services import Issue, IssueService +from bugwarrior.services import Issue, IssueService, ServiceClient -class RedMineClient(object): +class RedMineClient(ServiceClient): def __init__(self, url, key, auth): self.url = url self.key = key @@ -27,21 +27,7 @@ def call_api(self, uri, params): if self.auth: kwargs['auth'] = self.auth - response = requests.get(url, **kwargs) - - # And.. if we didn't get good results, just bail. - if response.status_code != 200: - raise IOError( - "Non-200 status code %r; %r; %r" % ( - response.status_code, url, response.text, - ) - ) - if callable(response.json): - # Newer python-requests - return response.json() - else: - # Older python-requests - return response.json + return self.json_response(requests.get(url, **kwargs), url) class RedMineIssue(Issue): diff --git a/bugwarrior/services/taiga.py b/bugwarrior/services/taiga.py index 6ef6836b9..f89236dc9 100644 --- a/bugwarrior/services/taiga.py +++ b/bugwarrior/services/taiga.py @@ -5,7 +5,7 @@ from bugwarrior.db import CACHE_REGION as cache from bugwarrior.config import die -from bugwarrior.services import IssueService, Issue +from bugwarrior.services import IssueService, Issue, ServiceClient class TaigaIssue(Issue): @@ -50,7 +50,7 @@ def get_default_description(self): ) -class TaigaService(IssueService): +class TaigaService(IssueService, ServiceClient): ISSUE_CLASS = TaigaIssue CONFIG_PREFIX = 'taiga' @@ -108,10 +108,7 @@ def issues(self): @cache.cache_on_arguments() def get_project(self, project_id): url = '%s/api/v1/projects/%i' % (self.url, project_id) - response = self.session.get(url) - if not bool(response): - raise IOError("Failed to talk to %r, %r" % (url, response)) - return response.json() + return self.json_response(self.session.get(url)) def build_url(self, story, project): return '%s/project/%s/us/%i' % (self.url, project['slug'], story['ref']) diff --git a/bugwarrior/services/teamlab.py b/bugwarrior/services/teamlab.py index e96207df2..a69f42fa9 100644 --- a/bugwarrior/services/teamlab.py +++ b/bugwarrior/services/teamlab.py @@ -3,10 +3,10 @@ from twiggy import log from bugwarrior.config import die -from bugwarrior.services import Issue, IssueService +from bugwarrior.services import Issue, IssueService, ServiceClient -class TeamLabClient(object): +class TeamLabClient(ServiceClient): def __init__(self, hostname, verbose=False): self.hostname = hostname self.verbose = verbose @@ -34,19 +34,7 @@ def call_api(self, uri, post=None, params=None): response = (requests.post(uri, data=post, **kwargs) if post else requests.get(uri, **kwargs)) - # And.. if we didn't get good results, just bail. - if response.status_code != 200: - raise IOError( - "Non-200 status code %r; %r; %r" % ( - response.status_code, uri, response.text, - ) - ) - if callable(response.json): - # Newer python-requests - return response.json() - else: - # Older python-requests - return response.json + return self.json_response(response, uri) class TeamLabIssue(Issue):