def get_signature(sign, secret): """ Get signature according to stringToSign, secret @param sign the signed string @param secret accesskey secret @return the signature """ hash_val = hmac.new(TeaConverter.to_str(secret), TeaConverter.to_str(sign), hashlib.sha1).digest() signature = base64.b64encode(hash_val).decode('utf-8') return signature
def _flat_repeat_list(dic): query = {} if dic: Client._object_handler('', dic, query) l = [] q = sorted(query) for i in q: k = quote_plus(TeaConverter.to_str(i)) v = quote_plus(TeaConverter.to_str(query[i])) l.append(k + '=' + v) return '&&'.join(l)
def assert_as_map(value): """ Assert a value, if it is a map, return it, otherwise throws @return: the map value """ if not isinstance(value, dict): raise ValueError('%s is not a dict' % TeaConverter.to_str(value)) return value
def assert_as_number(value): """ Assert a value, if it is a number, return it, otherwise throws @return: the number value """ if not isinstance(value, TeaConverter.number): raise ValueError('%s is not a number' % TeaConverter.to_str(value)) return value
def assert_as_bytes(value): """ Assert a value, if it is a bytes, return it, otherwise throws @return: the bytes value """ if not isinstance(value, bytes): raise ValueError('%s is not a bytes' % TeaConverter.to_str(value)) return value
def assert_as_string(value): """ Assert a value, if it is a string, return it, otherwise throws @return: the string value """ if not isinstance(value, TeaConverter.unicode): raise ValueError('%s is not a string' % TeaConverter.to_str(value)) return value
def assert_as_boolean(value): """ Assert a value, if it is a boolean, return it, otherwise throws @return: the boolean value """ if not isinstance(value, bool): raise ValueError('%s is not a bool' % TeaConverter.to_str(value)) return value
def get_rpcsignature(signed_params, method, secret): """ Get signature according to signedParams, method and secret @type signed_params: dict @param signed_params: params which need to be signed @type method: str @param method: http method e.g. GET @type secret: str @param secret: AccessKeySecret @return: the signature """ queries = signed_params.copy() keys = list(queries.keys()) keys.sort() canonicalized_query_string = "" for k in keys: if queries[k] is not None: canonicalized_query_string += "&" canonicalized_query_string += quote(TeaConverter.to_str(k), safe='') canonicalized_query_string += "=" canonicalized_query_string += quote(TeaConverter.to_str(queries[k]), safe='') string_to_sign = "" string_to_sign += method string_to_sign += '&' string_to_sign += quote_plus("/") string_to_sign += '&' string_to_sign += quote_plus( canonicalized_query_string[1:]) digest_maker = hmac.new(TeaConverter.to_bytes(secret + '&'), TeaConverter.to_bytes(string_to_sign), digestmod=hashlib.sha1) hash_bytes = digest_maker.digest() signed_str = TeaConverter.to_string(base64.b64encode(hash_bytes)) return signed_str
def parse_json(val): """ Parse it by JSON format @return: the parsed result """ try: return json.loads(val) except ValueError: raise RuntimeError( 'Failed to parse the value as json format, Value: "%s".' % TeaConverter.to_str(val))
def to_form_string(val): """ Format a map to form string, like a=a%20b%20c @return: the form string """ if not val: return "" keys = sorted(list(val)) dic = [(k, TeaConverter.to_str(val[k])) for k in keys if not isinstance(val[k], READABLE)] return urlencode(dic)
def _get_rpc_signature(signed_params, method, secret): queries = signed_params.copy() keys = list(queries.keys()) keys.sort() canonicalized_query_string = "" for k in keys: if queries[k] is not None: canonicalized_query_string += '&{}={}'.format( quote(k, safe=''), quote(queries[k], safe='')) string_to_sign = '{}&%2F&{}'.format( method, quote_plus(canonicalized_query_string[1:])) digest_maker = hmac.new(TeaConverter.to_str(secret + Client.SEPARATOR), TeaConverter.to_str(string_to_sign), digestmod=hashlib.sha1) hash_bytes = digest_maker.digest() signed_str = str(base64.b64encode(hash_bytes)) return signed_str
def _object_handler(key, value, out): if value is None: return if isinstance(value, dict): dic = value for k, v in dic.items(): Client._object_handler('%s.%s' % (key, k), v, out) elif isinstance(value, (list, tuple)): lis = value for index, val in enumerate(lis): Client._object_handler('%s.%s' % (key, index + 1), val, out) else: if key.startswith('.'): key = key[1:] out[key] = TeaConverter.to_str(value)
def get_canonical_query_string(query): if query is None or len(query) <= 0: return '' canon_keys = [] for k, v in query.items(): if v is not None: canon_keys.append(k) canon_keys.sort() query_string = '' for key in canon_keys: value = quote(TeaConverter.to_str(query[key]), safe='~') if value is None: s = '%s&' % key else: s = '%s=%s&' % (key, value) query_string += s return query_string[:-1]
def default(self, o): if isinstance(o, TeaModel): return o.to_map() elif isinstance(o, bytes): return TeaConverter.to_str(o) super(Client._ModelEncoder, self).default(o)
def get_encode_param(param): return quote(TeaConverter.to_str(param), safe='~')
def get_encode_path(path): return quote(TeaConverter.to_str(path), safe='/~')