Skip to content

Commit 07a974a

Browse files
committed
#533 - In progress of switch http lib to requests lib
* In progress of updating "uriopen()" to requests.get() Signed-off-by: Chin Yeung Li <[email protected]>
1 parent f6307be commit 07a974a

File tree

6 files changed

+132
-85
lines changed

6 files changed

+132
-85
lines changed

src/attributecode/api.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
# ============================================================================
1616

1717
import json
18+
from requests import get
1819

1920
from urllib.parse import quote
2021
from urllib.parse import urlencode
21-
from urllib.request import Request
22-
from urllib.request import urlopen
2322
from urllib.error import HTTPError
2423

2524
from attributecode import ERROR
@@ -55,9 +54,11 @@ def request_license_data(api_url, api_key, license_key):
5554
license_data = {}
5655
errors = []
5756
try:
58-
request = Request(quoted_url, headers=headers)
59-
response = urlopen(request)
60-
response_content = response.read().decode('utf-8')
57+
# request = Request(quoted_url, headers=headers)
58+
# response = urlopen(request)
59+
# response_content = response.read().decode('utf-8')
60+
response = get(quoted_url, headers=headers)
61+
response_content = response.text
6162
# FIXME: this should be an ordered dict
6263
license_data = json.loads(response_content)
6364
if not license_data.get('results', []):

src/attributecode/gen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def generate(location, base_dir, android=None, reference_dir=None, fetch_license
281281

282282
if gen_license:
283283
license_dict, err = model.pre_process_and_fetch_license_dict(
284-
abouts, api_url, api_key)
284+
abouts, api_url=api_url, api_key=api_key)
285285
if err:
286286
for e in err:
287287
# Avoid having same error multiple times

src/attributecode/model.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
import json
2828
import os
2929
import posixpath
30+
from requests import get
3031
import traceback
3132
from itertools import zip_longest
33+
3234
import urllib
3335
from urllib.parse import urljoin
3436
from urllib.parse import urlparse
@@ -1881,17 +1883,13 @@ def detect_special_char(expression):
18811883

18821884
def valid_api_url(api_url):
18831885
try:
1884-
request = Request(api_url)
1885-
# This will always goes to exception as no key are provided.
1886-
# The purpose of this code is to validate the provided api_url is correct
1887-
urlopen(request)
1888-
return True
1889-
except HTTPError as http_e:
1890-
# The 403 error code is refer to "Authentication credentials were not provided.".
1891-
# This is correct as no key are provided.
1892-
if http_e.code == 403:
1886+
response = get(api_url)
1887+
# The 403 error code is expected if the api_url is pointing to DJE as no
1888+
# API key is provided. The 200 status code represent connection success
1889+
# to scancode's LicenseDB. All other exception yield to invalid api_url
1890+
if response.status_code == 403 or response.status_code == 200:
18931891
return True
1892+
else:
1893+
return False
18941894
except:
1895-
# All other exceptions yield to invalid api_url
1896-
pass
1897-
return False
1895+
return False

src/attributecode/util.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,10 @@ def have_network_connection():
297297

298298
url = "https://scancode-licensedb.aboutcode.org/"
299299

300-
try:
301-
response = requests.get(url)
302-
if response.status_code == 200:
303-
return True
304-
else:
305-
return False
306-
except requests.exceptions.RequestException:
300+
response = requests.get(url)
301+
if response.status_code == 200:
302+
return True
303+
else:
307304
return False
308305

309306

tests/test_api.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515
# ============================================================================
1616

17+
import requests
1718
import unittest
1819
from unittest import mock
1920

@@ -27,6 +28,7 @@ class FakeResponse(object):
2728

2829
def __init__(self, response_content):
2930
self.response_content = response_content
31+
self.text = response_content
3032

3133
def read(self):
3234
return self.response_content
@@ -44,12 +46,13 @@ def test_api_get_license_details_from_api(self, request_license_data):
4446
errors = []
4547
request_license_data.return_value = license_data, errors
4648

47-
expected = ({'short_name': 'Apache 2.0', 'full_text': 'Apache License Version 2.0 ...', 'key': 'apache-2.0'}, [])
49+
expected = ({'short_name': 'Apache 2.0',
50+
'full_text': 'Apache License Version 2.0 ...', 'key': 'apache-2.0'}, [])
4851
result = api.get_license_details_from_api(
4952
api_url='api_url', api_key='api_key', license_key='license_key')
5053
assert expected == result
5154

52-
@mock.patch.object(api, 'urlopen')
55+
@mock.patch.object(api, 'get')
5356
def test_api_request_license_data_with_result(self, mock_data):
5457
response_content = (
5558
b'{"count":1,"results":[{"name":"Apache 2.0","key":"apache-2.0","text":"Text"}]}'
@@ -63,7 +66,7 @@ def test_api_request_license_data_with_result(self, mock_data):
6366
)
6467
assert expected == license_data
6568

66-
@mock.patch.object(api, 'urlopen')
69+
@mock.patch.object(api, 'get')
6770
def test_api_request_license_data_without_result(self, mock_data):
6871
response_content = b'{"count":0,"results":[]}'
6972
mock_data.return_value = FakeResponse(response_content)
@@ -79,5 +82,6 @@ def test_api_request_license_data_with_incorrect_url(self, mock_data):
7982
mock_data.return_value = FakeResponse(response_content)
8083
license_data = api.request_license_data(
8184
api_url='http://fake.url/', api_key='api_key', license_key='apache-2.0')
82-
expected = ({}, [Error(ERROR, "Invalid '--api_url'. License generation is skipped.")])
85+
expected = (
86+
{}, [Error(ERROR, "Invalid '--api_url'. License generation is skipped.")])
8387
assert expected == license_data

0 commit comments

Comments
 (0)