Skip to content

Commit

Permalink
Merge pull request #193 from psykzz/patch-1
Browse files Browse the repository at this point in the history
Cache api instance
  • Loading branch information
rafaelpivato authored Dec 27, 2020
2 parents 6fb8638 + edad36c commit 5972a1b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
14 changes: 11 additions & 3 deletions pyup/providers/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@ def __init__(self, bundle, integration=False, url=None, ignore_ssl=False):
self.integration = integration
self.url = url
self.ignore_ssl = ignore_ssl
self.__api = None
self.__token = ''

@classmethod
def is_same_user(cls, this, that):
return this.login == that.login

def _api(self, token):
if self.__api and token == self.__token:
return self.__api

# If we don't already have an instance or for some reason the token has changed
# we create a new instance
verify = not self.ignore_ssl
if self.url:
return Github(token, base_url=self.url, timeout=50, verify=verify)
return Github(token, timeout=50, verify=verify)
self.__token = token
self.__api = Github(self.__token, base_url=self.url, timeout=50, verify=verify)
return self.__api


def get_user(self, token):
return self._api(token).get_user()
Expand Down
15 changes: 13 additions & 2 deletions tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_is_same_user(self):
def test_api(self, github_mock):
prov = Provider(bundle=RequirementsBundle())
prov._api("foo")
github_mock.assert_called_once_with("foo", timeout=50, verify=True)
github_mock.assert_called_once_with("foo", base_url=None, timeout=50, verify=True)

@patch("pyup.providers.github.Github")
def test_api_different_host_in_provider_url(self, github_mock):
Expand All @@ -43,6 +43,17 @@ def test_api_different_host_in_provider_url(self, github_mock):
prov._api(token)
github_mock.assert_called_once_with(token, base_url=url, timeout=50, verify=True)

@patch("pyup.providers.github.Github")
def test_api_different_token_new_instance(self, github_mock):
token1, token2 = 'foo', 'foo2'

prov = Provider(bundle=RequirementsBundle())
prov._api(token1)
prov._api(token1)
github_mock.assert_called_once_with(token1, base_url=None, timeout=50, verify=True)
prov._api(token2)
github_mock.assert_called_with(token2, base_url=None, timeout=50, verify=True)


def test_get_user(self):
self.provider.get_user("foo")
Expand Down Expand Up @@ -316,4 +327,4 @@ def test_ignore_ssl(self, github_mock):
provider._api("foo")

self.assertTrue(provider.ignore_ssl)
github_mock.assert_called_once_with("foo", timeout=50, verify=(not ignore_ssl))
github_mock.assert_called_once_with("foo", base_url=None, timeout=50, verify=(not ignore_ssl))

0 comments on commit 5972a1b

Please sign in to comment.