Skip to content

Commit 6939f28

Browse files
committed
Added method to fetch project versions using PyPi API, removed hardcoded versions in tests.
1 parent 4f5cb4d commit 6939f28

3 files changed

Lines changed: 30 additions & 4 deletions

File tree

l2tdevtools/download_helpers/github.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ def GetLatestVersionWithAPI(self, version_definition):
102102
github_url = 'https://api.github.com/repos/{0:s}/{1:s}/releases'.format(
103103
self._organization, self._repository)
104104
page_content = self.DownloadPageContent(github_url)
105-
if not page_content:
106-
return None
105+
107106

108107
api_response = json.loads(page_content)
109108
release_names = [release['name'] for release in api_response]

l2tdevtools/download_helpers/interface.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,30 @@ def DownloadPageContent(self, download_url, encoding='utf-8'):
103103
self._cached_url = download_url
104104

105105
return self._cached_page_content
106+
107+
def DownloadAPIPageContent(self, download_url, encoding='utf-8'):
108+
"""Download content from a github API url"""
109+
if not download_url:
110+
return None
111+
112+
if self._cached_url != download_url:
113+
try:
114+
url_object = urllib_request.urlopen(download_url)
115+
except urllib_error.URLError as exception:
116+
logging.warning(
117+
'Unable to download URL: {0:s} with error: {1!s}'.format(
118+
download_url, exception))
119+
return None
120+
121+
if url_object.code != 403:
122+
return None
123+
124+
page_content = url_object.read()
125+
126+
if encoding and isinstance(page_content, py2to3.BYTES_TYPE):
127+
page_content = page_content.decode(encoding)
128+
129+
self._cached_page_content = page_content
130+
self._cached_url = download_url
131+
132+
return self._cached_page_content

tests/download_helpers/pypi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ def testGetLatestVersion(self):
3030

3131
latest_version = download_helper.GetLatestVersion(self._PROJECT_NAME, None)
3232

33-
latest_version = download_helper.GetLatestVersionWithAPI(
33+
latest_version_with_api = download_helper.GetLatestVersionWithAPI(
3434
self._PROJECT_NAME, None)
3535

36-
self.assertEqual(latest_version, self._PROJECT_VERSION)
36+
self.assertEqual(latest_version, latest_version_with_api)
3737

3838
def testGetDownloadURL(self):
3939
"""Tests the GetDownloadURL functions."""

0 commit comments

Comments
 (0)