Exemplo n.º 1
0
    def request_license_data(self, url, username, api_key, license_key):
        """
        Send a request to a given API URL to gather license data for
        license_key, authenticating through an api_key and username. Return a
        python dictionary of results returned by the API.
        """
        payload = {
            'username': username,
            'api_key': api_key,
            'format': 'json'
        }

        url = url.rstrip('/')
        encoded_payload = urllib.urlencode(payload)
        full_url = '%(url)s/%(license_key)s/?%(encoded_payload)s' % locals()
        # handle special characters in URL such as space etc.
        full_url = urllib.quote(full_url, safe="%/:=&?~#+!$,;'@()*[]")
        license_data = {}
        try:
            request = urllib2.Request(full_url)
            response = urllib2.urlopen(request)
            response_content = response.read()
            license_data = json.loads(response_content)
        except urllib2.HTTPError as http_e:
            # some auth problem
            if http_e.code == 401:
                error_msg = ("Authorization denied. Invalid '--api_username' or '--api_key'."
                            " License data collection skipped.")
                print()
                print(error_msg)
                print()
                self.extract_dje_license_error = True
                self.errors.append(Error(VALUE, 'username/api_key',
                                         username + '/' + api_key, error_msg))
            else:
                # FIXME: would this be only with a 404?
                self.errors.append(Error(VALUE, 'dje_license_key', license_key,
                                         "Invalid 'dje_license_key'"))
        except urllib2.URLError:
            if about.check_network_connection():
                error_msg = ("URL not reachable. Invalid '--api_url'."
                             " LICENSE generation is skipped.")
                print("\n" + error_msg + "\n")
                self.extract_dje_license_error = True
                self.errors.append(Error(VALUE, '--api_url', url, error_msg))
            else:
                error_msg = "Network problem. Please check the Internet connection. LICENSE generation is skipped."
                print("\n" + error_msg + "\n")
                self.extract_dje_license_error = True
                self.errors.append(Error(NETWORK, 'Network', '', error_msg))
        except ValueError:
            # FIXME: when does this happen?
            pass
        finally:
            return license_data
Exemplo n.º 2
0
 def pre_process_and_dje_license_dict(self, input_list, api_url, api_key):
     """
     Modify a list of About data dictionaries by adding license information
     fetched from the DejaCode API.
     """
     dje_uri = urlparse(api_url)
     domain = '{uri.scheme}://{uri.netloc}/'.format(uri=dje_uri)
     dje_lic_urn = urljoin(domain, 'urn/?urn=urn:dje:license:')
     key_text_dict = {}
     license_dict = {}
     if check_network_connection():
         if not self.valid_api_url(api_url):
             error_msg = "URL not reachable. Invalid '--api_url'. License generation is skipped."
             print('\n%(error_msg)s\n' % locals())
             self.extract_license_api_error = True
             self.errors.append(Error(VALUE, '--api_url', api_url, error_msg))
     else:
         error_msg = "Network problem. Please check your Internet connection. License generation is skipped."
         print('\n%(error_msg)s\n' % locals())
         self.extract_license_api_error = True
         self.errors.append(Error(NETWORK, 'Network', '', error_msg))
     for line in input_list:
         try:
             if line['dje_license_key']:
                 if '\n' in line['dje_license_key']:
                     line['dje_license_name'] = ""
                     self.errors.append(Error(VALUE, 'dje_license_key', line['dje_license_key'], "No multiple licenses or newline character are accepted."))
                     continue
                 lic = line['dje_license_key']
                 if lic not in license_dict:
                     detail_list = []
                     license_name, license_key, license_text = self.get_license_details_from_api(api_url, api_key, lic)
                     line['dje_license_key'] = license_key
                     license_dict[license_key] = license_name
                     line['dje_license_name'] = license_name
                     line['dje_license_url'] = dje_lic_urn + license_key
                     detail_list.append(license_key)
                     detail_list.append(license_text)
                     key_text_dict[license_name] = detail_list
                 else:
                     line['dje_license_name'] = license_dict[lic]
                     line['dje_license_url'] = dje_lic_urn + lic
         except Exception as e:
             err = Warn(VALUE, 'dje_license_key', '', 'Missing "dje_license_key" for ' + line['about_file'])
             self.warnings.append(err)
     return key_text_dict