def x_11paths_authorization(app_id, secret, context, utc=None): """ Calculate the authentication headers to be sent with a request to the API. :param app_id: :param secret: :param context :param utc: :return: array a map with the Authorization and Date headers needed to sign a Latch API request """ utc = utc or context.headers[X_11PATHS_DATE_HEADER_NAME] url_path = ensure_url_path_starts_with_slash(context.url_path) url_path_query = url_path if context.query_params: url_path_query += "?%s" % (url_encode(context.query_params, sort=True)) string_to_sign = (context.method.upper().strip() + "\n" + utc + "\n" + _get_11paths_serialized_headers(context.headers) + "\n" + url_path_query.strip()) if context.body_params and isinstance(context.renderer, FormRenderer): string_to_sign = string_to_sign + "\n" + url_encode( context.body_params, sort=True).replace("&", "") authorization_header_value = (AUTHORIZATION_METHOD + AUTHORIZATION_HEADER_FIELD_SEPARATOR + app_id + AUTHORIZATION_HEADER_FIELD_SEPARATOR + _sign_data(secret, string_to_sign)) return authorization_header_value
def x_11paths_authorization(app_id, secret, context, utc=None): """ Calculate the authentication headers to be sent with a request to the API. :param app_id: :param secret: :param context :param utc: :return: array a map with the Authorization and Date headers needed to sign a Latch API request """ utc = utc or context.headers[X_11PATHS_DATE_HEADER_NAME] url_path = ensure_url_path_starts_with_slash(context.url_path) url_path_query = url_path if context.query_params is not None: url_path_query += "?%s" % (url_encode(context.query_params)) string_to_sign = (context.method.upper().strip() + "\n" + utc + "\n" + _get_11paths_serialized_headers(context.headers) + "\n" + url_path_query.strip()) if context.body_params and isinstance(context.renderer, FormRenderer): string_to_sign = string_to_sign + "\n" + url_encode(context.body_params, sort=True).replace("&", "") authorization_header_value = (AUTHORIZATION_METHOD + AUTHORIZATION_HEADER_FIELD_SEPARATOR + app_id + AUTHORIZATION_HEADER_FIELD_SEPARATOR + _sign_data(secret, string_to_sign)) return authorization_header_value
def generate_url_path(url_path_format, prefix=None, format_suffix=None, allow_key_errors=True, **kwargs): prefix = prefix or "" suffix = ensure_url_path_format_suffix_starts_with_dot(format_suffix) while True: try: return ensure_url_path_starts_with_slash(prefix + url_path_format.format(**kwargs) + suffix) except KeyError as e: if not allow_key_errors: raise key = e.args[0] kwargs[key] = "{%s}" % key continue except: raise
def generate_url_path(url_path_format, prefix=None, format_suffix=None, allow_key_errors=True, **kwargs): prefix = prefix or '' suffix = ensure_url_path_format_suffix_starts_with_dot(format_suffix) while True: try: return ensure_url_path_starts_with_slash(prefix + url_path_format.format(**kwargs) + suffix) except KeyError as e: if not allow_key_errors: raise key = e.args[0] kwargs[key] = '{%s}' % key continue except: raise
def test_ensure_url_path_starts_with_slash_if_it_has_not_it(self): url = ensure_url_path_starts_with_slash("api/1.0/") self.assertEqual(url, "/api/1.0/")
def test_ensure_url_path_starts_with_slash_none(self): url = ensure_url_path_starts_with_slash(None) self.assertEqual(url, "/")
def test_ensure_url_path_starts_with_slash_empty_string(self): url = ensure_url_path_starts_with_slash("") self.assertEqual(url, "/")