def _remove_qs(self, url): ''' Removes a query string from a URL before signing. :param url: The URL to strip. :type url: str ''' scheme, netloc, path, query, fragment = urlsplit(url) return urlunsplit((scheme, netloc, path, '', fragment))
def sign(url, app_id, app_secret, hash_meth="sha1", **params): """ A signature method which generates the necessary Ofly parameters. :param app_id: The oFlyAppId, i.e. "application ID". :type app_id: str :param app_secret: The oFlyAppSecret, i.e. "shared secret". :type app_secret: str :param hash_meth: The hash method to use for signing, defaults to "sha1". :type hash_meth: str :param \*\*params: Additional parameters. :type \*\*\params: dict """ hash_meth_str = hash_meth if hash_meth == "sha1": hash_meth = sha1 elif hash_meth == "md5": hash_meth = md5 else: raise TypeError('hash_meth must be one of "sha1", "md5"') now = datetime.utcnow() milliseconds = now.microsecond // 1000 time_format = "%Y-%m-%dT%H:%M:%S.{0}Z".format(milliseconds) ofly_params = { "oflyAppId": app_id, "oflyHashMeth": hash_meth_str.upper(), "oflyTimestamp": now.strftime(time_format), } url_path = urlsplit(url).path signature_base_string = app_secret + url_path + "?" if len(params): signature_base_string += get_sorted_params(params) + "&" signature_base_string += get_sorted_params(ofly_params) if not isinstance(signature_base_string, bytes): signature_base_string = signature_base_string.encode("utf-8") ofly_params["oflyApiSig"] = hash_meth(signature_base_string).hexdigest() all_params = dict(tuple(ofly_params.items()) + tuple(params.items())) return get_sorted_params(all_params)