Skip to content

Commit

Permalink
Add ServiceClient base class.
Browse files Browse the repository at this point in the history
This seems to be the direction the abstraction is going in and
facilitates DRYing up the json response handling.
  • Loading branch information
ryneeverett committed Apr 13, 2016
1 parent 39f949d commit 4a42012
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 85 deletions.
17 changes: 17 additions & 0 deletions bugwarrior/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 3 additions & 17 deletions bugwarrior/services/activecollab2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
20 changes: 3 additions & 17 deletions bugwarrior/services/bitbucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -55,7 +55,7 @@ def get_default_description(self):
)


class BitbucketService(IssueService):
class BitbucketService(IssueService, ServiceClient):
ISSUE_CLASS = BitbucketIssue
CONFIG_PREFIX = 'bitbucket'

Expand Down Expand Up @@ -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):
Expand Down
16 changes: 3 additions & 13 deletions bugwarrior/services/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -180,7 +180,7 @@ def get_default_description(self):
)


class GitlabService(IssueService):
class GitlabService(IssueService, ServiceClient):
ISSUE_CLASS = GitlabIssue
CONFIG_PREFIX = 'gitlab'

Expand Down Expand Up @@ -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 = {
Expand Down
20 changes: 3 additions & 17 deletions bugwarrior/services/redmine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
9 changes: 3 additions & 6 deletions bugwarrior/services/taiga.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -50,7 +50,7 @@ def get_default_description(self):
)


class TaigaService(IssueService):
class TaigaService(IssueService, ServiceClient):
ISSUE_CLASS = TaigaIssue
CONFIG_PREFIX = 'taiga'

Expand Down Expand Up @@ -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'])
Expand Down
18 changes: 3 additions & 15 deletions bugwarrior/services/teamlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 4a42012

Please sign in to comment.