Skip to content

Commit

Permalink
Merge branch 'handle_invalid_api' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
CYL authored and CYL committed Jan 15, 2014
2 parents 40b102d + 23c7a9a commit 1dc12b9
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions about_code_tool/genabout.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,23 @@ def request_license_data(url, username, api_key, license_key):
url if url.endswith('/') else url + '/',
license_key, urllib.urlencode(payload))

request = urllib2.Request(full_url)
#request = urllib2.Request(full_url)
try:
request = urllib2.Request(full_url)
response = urllib2.urlopen(request)
response_content = response.read()
data = json.loads(response_content)
except (urllib2.HTTPError, ValueError):
except urllib2.HTTPError as http_e:
# the code 401 represents authorization problem
if http_e.code == 401:
return 'authorization denied'
else:
return {}
except urllib2.URLError as url_e:
if about.check_network_connection():
return 'URL not reachable'
return 'No network'
except ValueError as value_e:
return {}
else:
return data
Expand Down Expand Up @@ -220,6 +231,18 @@ def extract_dje_license(self, project_path, license_list, url, username, key):
gen_license_path = join(project_path, gen_path, license_key) + '.LICENSE'
if not _exists(gen_license_path):
context = self.get_license_text_from_api(url, username, key, license_key)
if context == 'authorization denied':
print("Authorization denied. Invalid '--api_username' or '--api_key'.")
print("LICENSE generation is skipped.")
sys.exit(errno.EINVAL)
if context == 'URL not reachable':
print("URL not reachable. Invalid '--api_url'.")
print("LICENSE generation is skipped.")
sys.exit(errno.EINVAL)
if context == 'No network':
print("Network problem. Please check the Internet connection.")
print("LICENSE generation is skipped.")
sys.exit(errno.EINVAL)
if not context:
self.errors.append(Error('dje_license_key', license_key,
"Invalid 'dje_license_key'"))
Expand All @@ -238,6 +261,8 @@ def get_license_text_from_api(url, username, api_key, license_key):
Returns an empty string if the text is not available.
"""
data = request_license_data(url, username, api_key, license_key)
if data == 'authorization denied' or data == 'URL not reachable' or data == 'No network':
return data
license_text = data.get('full_text', '')
return license_text

Expand Down Expand Up @@ -455,7 +480,7 @@ def option_usage():
<--api_url='URL'> - URL to the DJE License Library
<--api_username='user_api'> - The regular DJE username
<--api_key='user_api_key'> - Hash attached to your username which is used
to Authenticate yourself in the API. Contact
to authenticate yourself in the API. Contact
us to get the hash key.
Example syntax:
genabout.py --extract_license --api_url='https://enterprise.dejacode.com/api/v1/license_text/' --api_username='<user_api>' --api_key='<user_api_key>'
Expand Down Expand Up @@ -621,4 +646,4 @@ def main(args, opts):
option_usage()
sys.exit(errno.EINVAL)

main(args, opts)
main(args, opts)

1 comment on commit 1dc12b9

@chinyeungli
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed #65

Please sign in to comment.