Skip to content

Commit

Permalink
Merge pull request #332 from ralphbean/feature/http-sessions
Browse files Browse the repository at this point in the history
Use requests "sessions".
  • Loading branch information
ralphbean authored Jun 25, 2016
2 parents b8c6885 + 24f1de4 commit c884262
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
13 changes: 6 additions & 7 deletions bugwarrior/services/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
class GithubClient(ServiceClient):
def __init__(self, auth):
self.auth = auth
self.session = requests.Session()
if 'token' in self.auth:
authorization = 'token ' + self.auth['token']
self.session.headers = {'Authorization': authorization}

def get_repos(self, username):
user_repos = self._getter(
Expand Down Expand Up @@ -56,19 +60,14 @@ def _getter(self, url, subkey=None):
""" Pagination utility. Obnoxious. """

kwargs = {}

if 'token' in self.auth:
kwargs['headers'] = {
'Authorization': 'token ' + self.auth['token']
}
elif 'basic' in self.auth:
if 'basic' in self.auth:
kwargs['auth'] = self.auth['basic']

results = []
link = dict(next=url)

while 'next' in link:
response = requests.get(link['next'], **kwargs)
response = self.session.get(link['next'], **kwargs)
json_res = self.json_response(response)

if subkey is not None:
Expand Down
6 changes: 3 additions & 3 deletions bugwarrior/services/pagure.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class PagureService(IssueService):
def __init__(self, *args, **kw):
super(PagureService, self).__init__(*args, **kw)

self.auth = {}
self.session = requests.Session()

self.tag = self.config_get_default('tag')
self.repo = self.config_get_default('repo')
Expand Down Expand Up @@ -141,7 +141,7 @@ def get_issues(self, repo, keys):
key3 = key1[:-1] # Just the singular form of key1

url = self.base_url + "/api/0/" + repo + "/" + key1
response = requests.get(url)
response = self.session.get(url)

if not bool(response):
error = response.json()
Expand Down Expand Up @@ -189,7 +189,7 @@ def filter_repos(self, repo):
def issues(self):
if self.tag:
url = self.base_url + "/api/0/projects?tags=" + self.tag
response = requests.get(url)
response = self.session.get(url)
if not bool(response):
raise IOError('Failed to talk to %r %r' % (url, response))

Expand Down

0 comments on commit c884262

Please sign in to comment.