Ejemplo n.º 1
0
 def _api_request(self, method, endpoint, headers=None, values=None):
     if python_version == 2:
         if sys.version_info < (2, 7, 9):
             raise SystemError("Unsupported version of Python. Upgrade to version 2.7.9")
     else:
         if sys.version_info < (3, 4, 3):
             raise SystemError("Unsupported version of Python. Upgrade to version 3.4.3")
     url = self.url+endpoint
     ctx = ssl.create_default_context()
     data = None
     if values:
         data = json.dumps(values).encode()
     if headers:
         req = urllib2.Request(url, data=data, headers=headers)
     else:
         req = urllib2.Request(url, data=data)
     req.get_method = lambda: method
     try:
         response = urllib2.urlopen(req, context=ctx)
         return response.read()
     except urllib2.HTTPError as e:
         try:
             error_res = e.read()
             # error_code = json.loads(error_res)
             error_code = Helper.json_loads(error_res)
             e.msg = errors_list[error_code['code']]
             raise
         except ValueError:
             raise e
 def generate(identity_value, identity_type, private_key, password):
     random_id = Helper.generate_id()
     signature = base64.b64encode(bytearray(
         CryptoWrapper.sign(random_id + identity_type + identity_value, private_key, password)))
     token = random_id + '.' + signature.decode()
     return base64.b64encode(bytearray(token, 'utf-8')).decode()