Skip to content

endpoints.py

Provides utils for endpoints.

convert_path_params(endpoint, path_params, converters)

Convert path parameters to necessary types.

Parameters:

Name Type Description Default
endpoint str

Route endpoint.

required
path_params dict

Path parameters.

required
converters dict

Converters for path parameter.

required

Returns:

Type Description
dict

Converted path parameters.

Source code in jetweb/utils/endpoints.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def convert_path_params(endpoint: str, path_params: dict, converters: dict) -> dict:
    """
    Convert path parameters to necessary types.

    :param endpoint: Route endpoint.
    :param path_params: Path parameters.
    :param converters: Converters for path parameter.
    :returns: Converted path parameters.
    """
    for param_name in path_params:
        for converter in converters.values():
            if f"{param_name}:{converter.identifier}" in endpoint:
                path_params[param_name] = converter.convert(path_params[param_name])

    return path_params

convert_to_regex(match, converters)

Convert a "{name:type}" placeholder into a named regex group using converters.

Parameters:

Name Type Description Default
match Match

Match object with path parameter.

required
converters dict

Converters for path parameter.

required

Returns:

Type Description
str

Regex substring for path parameter.

Raises:

Type Description
ValueError

If converter identifier is not known.

Source code in jetweb/utils/endpoints.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def convert_to_regex(match: Match, converters: dict) -> str:
    """
    Convert a "{name:type}" placeholder into a named regex group using converters.

    :param match: Match object with path parameter.
    :param converters: Converters for path parameter.
    :returns: Regex substring for path parameter.
    :raises ValueError: If converter identifier is not known.
    """
    param_name, param_type = match.groups()
    param_type = (param_type or "path").lstrip(":")

    if param_type not in converters:
        raise ValueError("Converter must be registered")

    return f"(?P<{param_name}>{converters[param_type].pattern})"

create_pattern(endpoint, converters)

Build a compiled regex pattern from a route endpoint.

Parameters:

Name Type Description Default
endpoint str

Route endpoint.

required
converters dict

Converters for path parameter.

required

Returns:

Type Description
Pattern

Pattern object.

Source code in jetweb/utils/endpoints.py
26
27
28
29
30
31
32
33
34
35
def create_pattern(endpoint: str, converters: dict) -> Pattern:
    """
    Build a compiled regex pattern from a route endpoint.

    :param endpoint: Route endpoint.
    :param converters: Converters for path parameter.
    :returns: Pattern object.
    """
    pattern = sub(r"{(\w+)(:\w+)?}", lambda match: convert_to_regex(match, converters), endpoint)
    return compile("^" + pattern + "$")

normalize_endpoint(endpoint)

Add leading slash and remove several sequential slashes.

Parameters:

Name Type Description Default
endpoint str

Route endpoint.

required

Returns:

Type Description
str

Normalized route endpoint.

Source code in jetweb/utils/endpoints.py
55
56
57
58
59
60
61
62
63
def normalize_endpoint(endpoint: str) -> str:
    """
    Add leading slash and remove several sequential slashes.

    :param endpoint: Route endpoint.
    :returns: Normalized route endpoint.
    """
    endpoint = "/" + endpoint
    return sub(r"/+", r"/", endpoint)