From 6939f2865b8a385642fd169ac8e4d9983763cf7c Mon Sep 17 00:00:00 2001 From: Daniel White Date: Fri, 18 Jan 2019 11:32:04 +0100 Subject: [PATCH] Added method to fetch project versions using PyPi API, removed hardcoded versions in tests. --- l2tdevtools/download_helpers/github.py | 3 +-- l2tdevtools/download_helpers/interface.py | 27 +++++++++++++++++++++++ tests/download_helpers/pypi.py | 4 ++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/l2tdevtools/download_helpers/github.py b/l2tdevtools/download_helpers/github.py index cc86c70d..28e324c1 100644 --- a/l2tdevtools/download_helpers/github.py +++ b/l2tdevtools/download_helpers/github.py @@ -102,8 +102,7 @@ def GetLatestVersionWithAPI(self, version_definition): github_url = 'https://api.github.com/repos/{0:s}/{1:s}/releases'.format( self._organization, self._repository) page_content = self.DownloadPageContent(github_url) - if not page_content: - return None + api_response = json.loads(page_content) release_names = [release['name'] for release in api_response] diff --git a/l2tdevtools/download_helpers/interface.py b/l2tdevtools/download_helpers/interface.py index fd097a5e..77c9ba63 100644 --- a/l2tdevtools/download_helpers/interface.py +++ b/l2tdevtools/download_helpers/interface.py @@ -103,3 +103,30 @@ def DownloadPageContent(self, download_url, encoding='utf-8'): self._cached_url = download_url return self._cached_page_content + + def DownloadAPIPageContent(self, download_url, encoding='utf-8'): + """Download content from a github API url""" + if not download_url: + return None + + if self._cached_url != download_url: + try: + url_object = urllib_request.urlopen(download_url) + except urllib_error.URLError as exception: + logging.warning( + 'Unable to download URL: {0:s} with error: {1!s}'.format( + download_url, exception)) + return None + + if url_object.code != 403: + return None + + page_content = url_object.read() + + if encoding and isinstance(page_content, py2to3.BYTES_TYPE): + page_content = page_content.decode(encoding) + + self._cached_page_content = page_content + self._cached_url = download_url + + return self._cached_page_content diff --git a/tests/download_helpers/pypi.py b/tests/download_helpers/pypi.py index b302b53f..789b7059 100644 --- a/tests/download_helpers/pypi.py +++ b/tests/download_helpers/pypi.py @@ -30,10 +30,10 @@ def testGetLatestVersion(self): latest_version = download_helper.GetLatestVersion(self._PROJECT_NAME, None) - latest_version = download_helper.GetLatestVersionWithAPI( + latest_version_with_api = download_helper.GetLatestVersionWithAPI( self._PROJECT_NAME, None) - self.assertEqual(latest_version, self._PROJECT_VERSION) + self.assertEqual(latest_version, latest_version_with_api) def testGetDownloadURL(self): """Tests the GetDownloadURL functions."""