2020
2121import json
2222
23- try : # Python 2
24- from urllib import urlencode , quote
25- from urllib2 import urlopen , Request , HTTPError
26- except ImportError : # Python 3
27- from urllib .parse import urlencode , quote
28- from urllib .request import urlopen , Request
29- from urllib .error import HTTPError
30-
3123from attributecode import ERROR
3224from attributecode import Error
25+ from attributecode .util import python2
26+
27+
28+ if python2 : # pragma: nocover
29+ from urllib import quote # NOQA
30+ from urllib import urlencode # NOQA
31+ from urllib2 import HTTPError # NOQA
32+ from urllib2 import Request # NOQA
33+ from urllib2 import urlopen # NOQA
34+ else : # pragma: nocover
35+ from urllib .parse import quote # NOQA
36+ from urllib .parse import urlencode # NOQA
37+ from urllib .request import Request # NOQA
38+ from urllib .request import urlopen # NOQA
39+ from urllib .error import HTTPError # NOQA
3340
3441
3542"""
3643API call helpers
3744"""
3845
3946
40- def request_license_data (url , api_key , license_key ):
47+ # FIXME: args should start with license_key
48+ def request_license_data (api_url , api_key , license_key ):
4149 """
42- Return a dictionary of license data.
43- Send a request to a given API URL to gather license data for
44- license_key, authenticating through an api_key.
50+ Return a tuple of (dictionary of license data, list of errors) given a
51+ `license_key`. Send a request to `api_url` authenticating with `api_key`.
4552 """
4653 headers = {
4754 'Authorization' : 'Token %s' % api_key ,
@@ -52,9 +59,10 @@ def request_license_data(url, api_key, license_key):
5259 'format' : 'json'
5360 }
5461
55- url = url .rstrip ('/' )
56- encoded_payload = urlencode (payload )
57- full_url = '%(url)s/?%(encoded_payload)s' % locals ()
62+ api_url = api_url .rstrip ('/' )
63+ payload = urlencode (payload )
64+
65+ full_url = '%(api_url)s/?%(payload)s' % locals ()
5866 # handle special characters in URL such as space etc.
5967 quoted_url = quote (full_url , safe = "%/:=&?~#+!$,;'@()*[]" )
6068
@@ -64,10 +72,12 @@ def request_license_data(url, api_key, license_key):
6472 request = Request (quoted_url , headers = headers )
6573 response = urlopen (request )
6674 response_content = response .read ().decode ('utf-8' )
75+ # FIXME: this should be an ordered dict
6776 license_data = json .loads (response_content )
6877 if not license_data ['results' ]:
6978 msg = u"Invalid 'license': %s" % license_key
7079 errors .append (Error (ERROR , msg ))
80+
7181 except HTTPError as http_e :
7282 # some auth problem
7383 if http_e .code == 403 :
@@ -80,20 +90,29 @@ def request_license_data(url, api_key, license_key):
8090 # this exception.
8191 msg = u"Invalid 'license': %s" % license_key
8292 errors .append (Error (ERROR , msg ))
93+
8394 except Exception as e :
8495 errors .append (Error (ERROR , str (e )))
96+
8597 finally :
86- license_data = license_data .get ('results' )[0 ] if license_data .get ('count' ) == 1 else {}
98+ if license_data .get ('count' ) == 1 :
99+ license_data = license_data .get ('results' )[0 ]
100+ else :
101+ license_data = {}
87102
88103 return license_data , errors
89104
90105
91- def get_license_details_from_api (url , api_key , license_key ):
106+ # FIXME: args should start with license_key
107+ def get_license_details_from_api (api_url , api_key , license_key ):
92108 """
93- Return the license_text of a given license_key using an API request.
94- Return an empty string if the text is not available.
109+ Return a tuple of license data given a `license_key` using the `api_url`
110+ authenticating with `api_key`.
111+ The details are a tuple of (license_name, license_key, license_text, errors)
112+ where errors is a list of strings.
113+ Missing values are provided as empty strings.
95114 """
96- license_data , errors = request_license_data (url , api_key , license_key )
115+ license_data , errors = request_license_data (api_url , api_key , license_key )
97116 license_name = license_data .get ('name' , '' )
98117 license_text = license_data .get ('full_text' , '' )
99118 license_key = license_data .get ('key' , '' )
0 commit comments