# Path: qingcloud/misc/utils.py
# def get_utf8_value(value):
#     if sys.version < "3":
#         if isinstance(value, unicode):
#             return value.encode('utf-8')
#         if not isinstance(value, str):
#             value = str(value)
#         return value
#     else:
#         return str(value)
# 
# def get_ts(ts=None):
#     """ Get formatted time
#     """
#     if not ts:
#         ts = time.gmtime()
#     return time.strftime(ISO8601, ts)
# 
# def base64_url_decode(inp):
#     if sys.version > "3":
#         if isinstance(inp, bytes):
#             inp = inp.decode()
#         return base64.urlsafe_b64decode(inp + '=' * (4 - len(inp) % 4)).decode()
#     else:
#         return base64.urlsafe_b64decode(str(inp + '=' * (4 - len(inp) % 4)))
# 
# def base64_url_encode(inp):
#     if sys.version > "3":
#         if isinstance(inp, str):
#             inp = inp.encode()
#         return bytes.decode(base64.urlsafe_b64encode(inp).rstrip(b'='))
#     else:
#         return base64.urlsafe_b64encode(str(inp)).rstrip(b'=')


import sys
import hmac
import base64
import datetime
    import urllib.parse as urllib
    import urllib
from hashlib import sha1, sha256
from past.builtins import basestring
from qingcloud.misc.json_tool import json_dump, json_load
from qingcloud.misc.utils import get_utf8_value, get_ts, base64_url_decode,\
    base64_url_encode
    APIVersion = 1

    def _calc_signature(self, params, verb, path):
        """ calc signature for request
        """
        string_to_sign = '%s\n%s\n' % (verb, path)
        params['signature_method'] = self.algorithm()
        keys = sorted(params.keys())
        pairs = []
        for key in keys:
            val = get_utf8_value(params[key])
            if is_python3:
                key = key.encode()
            pairs.append(urllib.quote(key, safe='') + '=' +
                         urllib.quote(val, safe='-_~'))
        qs = '&'.join(pairs)
        string_to_sign += qs
        # print "string to sign:[%s]" % string_to_sign
        b64 = self.sign_string(string_to_sign)
        return (qs, b64)

    def add_auth(self, req, **kwargs):
        """ add authorize information for request
        """
        req.params['access_key_id'] = self.qy_access_key_id if 'access_key' not in kwargs else kwargs.get('access_key')
        if 'token' in kwargs:
            req.params['token'] = kwargs.get('token')
        req.params['signature_version'] = self.SignatureVersion if 'signature_version' not in kwargs \
            else kwargs.get('signature_version')
        req.params['version'] = self.APIVersion

# Path: qingcloud/misc/utils.py
# def get_utf8_value(value):
#     if sys.version < "3":
#         if isinstance(value, unicode):
#             return value.encode('utf-8')
#         if not isinstance(value, str):
#             value = str(value)
#         return value
#     else:
#         return str(value)
# 
# def get_ts(ts=None):
#     """ Get formatted time
#     """
#     if not ts:
#         ts = time.gmtime()
#     return time.strftime(ISO8601, ts)
# 
# def base64_url_decode(inp):
#     if sys.version > "3":
#         if isinstance(inp, bytes):
#             inp = inp.decode()
#         return base64.urlsafe_b64decode(inp + '=' * (4 - len(inp) % 4)).decode()
#     else:
#         return base64.urlsafe_b64decode(str(inp + '=' * (4 - len(inp) % 4)))
# 
# def base64_url_encode(inp):
#     if sys.version > "3":
#         if isinstance(inp, str):
#             inp = inp.encode()
#         return bytes.decode(base64.urlsafe_b64encode(inp).rstrip(b'='))
#     else:
#         return base64.urlsafe_b64encode(str(inp)).rstrip(b'=')


# Path: qingcloud/conn/auth.py
import sys
import hmac
import base64
import datetime
    import urllib.parse as urllib
    import urllib
from hashlib import sha1, sha256
from past.builtins import basestring
from qingcloud.misc.json_tool import json_dump, json_load
from qingcloud.misc.utils import get_utf8_value, get_ts, base64_url_decode,\
    base64_url_encode

    APIVersion = 1

    def _calc_signature(self, params, verb, path):
        """ calc signature for request
        """
        string_to_sign = '%s\n%s\n' % (verb, path)
        params['signature_method'] = self.algorithm()
        keys = sorted(params.keys())
        pairs = []
        for key in keys:
            val = get_utf8_value(params[key])
            if is_python3:
                key = key.encode()
            pairs.append(urllib.quote(key, safe='') + '=' +
                         urllib.quote(val, safe='-_~'))
        qs = '&'.join(pairs)
        string_to_sign += qs
        # print "string to sign:[%s]" % string_to_sign
        b64 = self.sign_string(string_to_sign)
        return (qs, b64)

    def add_auth(self, req, **kwargs):
        """ add authorize information for request
        """
        req.params['access_key_id'] = self.qy_access_key_id if 'access_key' not in kwargs else kwargs.get('access_key')
        if 'token' in kwargs:
            req.params['token'] = kwargs.get('token')
        req.params['signature_version'] = self.SignatureVersion if 'signature_version' not in kwargs \
            else kwargs.get('signature_version')
        req.params['version'] = self.APIVersion

        time_stamp = get_ts()