Skip to content

stac

STAC-specific utilities.

ensure_type(data: dict[str, Any], key: str, expected_type: type[T], default_factory: Callable[[], T] | None = None) -> T

Ensure a dictionary value conforms to the expected type.

If the value doesn't exist or is not an instance of the expected type, it will be replaced with the default value from default_factory.

Parameters:

Name Type Description Default
data dict[str, Any]

The dictionary containing the value

required
key str

The key to check

required
expected_type type[T]

The expected type class

required
default_factory Callable[[], T] | None

Optional callable that returns the default value. If not provided, expected_type will be called with no arguments.

None

Returns:

Type Description
T

The value from the dictionary if it's the correct type, otherwise the default value

Example

data = {"stac_extensions": None} extensions = ensure_type(data, "stac_extensions", list)

extensions is now [] and data["stac_extensions"] is []

data = {"items": "invalid"} items = ensure_type(data, "items", list, lambda: ["default"])

items is now ["default"] with custom factory

Source code in src/stac_auth_proxy/utils/stac.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def ensure_type(
    data: dict[str, Any],
    key: str,
    expected_type: type[T],
    default_factory: Callable[[], T] | None = None,
) -> T:
    """
    Ensure a dictionary value conforms to the expected type.

    If the value doesn't exist or is not an instance of the expected type,
    it will be replaced with the default value from default_factory.

    Args:
        data: The dictionary containing the value
        key: The key to check
        expected_type: The expected type class
        default_factory: Optional callable that returns the default value.
            If not provided, expected_type will be called with no arguments.

    Returns:
        The value from the dictionary if it's the correct type, otherwise the default value

    Example:
        >>> data = {"stac_extensions": None}
        >>> extensions = ensure_type(data, "stac_extensions", list)
        >>> # extensions is now [] and data["stac_extensions"] is []
        >>>
        >>> data = {"items": "invalid"}
        >>> items = ensure_type(data, "items", list, lambda: ["default"])
        >>> # items is now ["default"] with custom factory

    """
    value = data.get(key)
    if not isinstance(value, expected_type):
        if value is not None:
            logger.warning(
                "Field '%s' expected %s but got %s: %r",
                key,
                expected_type.__name__,
                type(value).__name__,
                value,
            )
        factory = default_factory if default_factory is not None else expected_type
        value = factory()
        data[key] = value
    return value

Get all links from a STAC response.

Source code in src/stac_auth_proxy/utils/stac.py
61
62
63
64
65
66
67
68
69
70
71
72
73
def get_links(data: dict) -> chain[dict]:
    """Get all links from a STAC response."""
    return chain(
        # Item/Collection
        data.get("links", []),
        # Collections/Items/Search
        (
            link
            for prop in ["features", "collections"]
            for object_with_links in data.get(prop, [])
            for link in object_with_links.get("links", [])
        ),
    )