Skip to content

Commit 1dc12b9

Browse files
CYLCYL
authored andcommitted
Merge branch 'handle_invalid_api' into develop
2 parents 40b102d + 23c7a9a commit 1dc12b9

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

about_code_tool/genabout.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,23 @@ def request_license_data(url, username, api_key, license_key):
6464
url if url.endswith('/') else url + '/',
6565
license_key, urllib.urlencode(payload))
6666

67-
request = urllib2.Request(full_url)
67+
#request = urllib2.Request(full_url)
6868
try:
69+
request = urllib2.Request(full_url)
6970
response = urllib2.urlopen(request)
7071
response_content = response.read()
7172
data = json.loads(response_content)
72-
except (urllib2.HTTPError, ValueError):
73+
except urllib2.HTTPError as http_e:
74+
# the code 401 represents authorization problem
75+
if http_e.code == 401:
76+
return 'authorization denied'
77+
else:
78+
return {}
79+
except urllib2.URLError as url_e:
80+
if about.check_network_connection():
81+
return 'URL not reachable'
82+
return 'No network'
83+
except ValueError as value_e:
7384
return {}
7485
else:
7586
return data
@@ -220,6 +231,18 @@ def extract_dje_license(self, project_path, license_list, url, username, key):
220231
gen_license_path = join(project_path, gen_path, license_key) + '.LICENSE'
221232
if not _exists(gen_license_path):
222233
context = self.get_license_text_from_api(url, username, key, license_key)
234+
if context == 'authorization denied':
235+
print("Authorization denied. Invalid '--api_username' or '--api_key'.")
236+
print("LICENSE generation is skipped.")
237+
sys.exit(errno.EINVAL)
238+
if context == 'URL not reachable':
239+
print("URL not reachable. Invalid '--api_url'.")
240+
print("LICENSE generation is skipped.")
241+
sys.exit(errno.EINVAL)
242+
if context == 'No network':
243+
print("Network problem. Please check the Internet connection.")
244+
print("LICENSE generation is skipped.")
245+
sys.exit(errno.EINVAL)
223246
if not context:
224247
self.errors.append(Error('dje_license_key', license_key,
225248
"Invalid 'dje_license_key'"))
@@ -238,6 +261,8 @@ def get_license_text_from_api(url, username, api_key, license_key):
238261
Returns an empty string if the text is not available.
239262
"""
240263
data = request_license_data(url, username, api_key, license_key)
264+
if data == 'authorization denied' or data == 'URL not reachable' or data == 'No network':
265+
return data
241266
license_text = data.get('full_text', '')
242267
return license_text
243268

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

624-
main(args, opts)
649+
main(args, opts)

0 commit comments

Comments
 (0)