def prepare_headers(headers): canon_keys = [] tmp_headers = {} for k, v in headers.items(): if v is not None: if k.lower() not in canon_keys: canon_keys.append(k.lower()) tmp_headers[k.lower()] = [TeaConverter.to_string(v).strip()] else: tmp_headers[k.lower()].append(TeaConverter.to_string(v).strip()) canon_keys.sort() return {key: ','.join(sorted(tmp_headers[key])) for key in canon_keys}
def get_canonicalized_headers(headers): canon_keys = [] tmp_headers = {} for k, v in headers.items(): if v is not None: if k.lower() not in canon_keys: canon_keys.append(k.lower()) tmp_headers[k.lower()] = [TeaConverter.to_string(v).strip()] else: tmp_headers[k.lower()].append(TeaConverter.to_string(v).strip()) canon_keys.sort() canonical_headers = '' for key in canon_keys: header_entry = ','.join(sorted(tmp_headers[key])) s = '%s:%s\n' % (key, header_entry) canonical_headers += s return canonical_headers, ';'.join(canon_keys)
def _get_canonicalized_resource(pathname, query): if len(query) <= 0: return pathname resource = '%s?' % pathname query_list = sorted(list(query)) for key in query_list: if query[key] is not None: if query[key] == '': s = '%s&' % key else: s = '%s=%s&' % (key, TeaConverter.to_string(query[key])) resource += s return resource[:-1]
def stringify_map_value(m): """ Stringify the value of map @return: the new stringified map """ if m is None: return {} dic_result = {} for k, v in m.items(): if v is not None: v = TeaConverter.to_string(v) dic_result[k] = v return dic_result
def _object_handler(key, value, out): if value is None: return if isinstance(value, dict): for k, v in value.items(): Client._object_handler('%s.%s' % (key, k), v, out) elif isinstance(value, TeaModel): for k, v in value.to_map().items(): Client._object_handler('%s.%s' % (key, k), v, out) elif isinstance(value, (list, tuple)): for index, val in enumerate(value): Client._object_handler('%s.%s' % (key, index + 1), val, out) else: if key.startswith('.'): key = key[1:] if not isinstance(value, STREAM_CLASS): out[key] = TeaConverter.to_string(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 to_string(val): """ Convert a bytes to string(utf8) @return: the return string """ return TeaConverter.to_string(val)