Skip to content

Commit

Permalink
Added method to fetch project versions using PyPi API, removed hardco…
Browse files Browse the repository at this point in the history
…ded versions in tests.
  • Loading branch information
Onager committed Jan 18, 2019
1 parent 4f5cb4d commit 6939f28
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
3 changes: 1 addition & 2 deletions l2tdevtools/download_helpers/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
27 changes: 27 additions & 0 deletions l2tdevtools/download_helpers/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions tests/download_helpers/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit 6939f28

Please sign in to comment.