|
| 1 | +"""Databricks Workspace API Catalog. |
| 2 | +
|
| 3 | +Declarative registry of every Databricks REST API endpoint exposed by |
| 4 | +the explorer UI, organised by category. The two main data structures |
| 5 | +are: |
| 6 | +
|
| 7 | +* :data:`API_CATALOG` -- nested dict of categories → endpoint |
| 8 | + definitions. |
| 9 | +* :data:`LIST_TO_GET` -- mapping that drives inline navigation from |
| 10 | + list responses to their corresponding *get* endpoints. |
| 11 | +
|
| 12 | +Attributes: |
| 13 | + API_CATALOG: Master endpoint registry keyed by category name. |
| 14 | + LIST_TO_GET: Mapping of list-endpoint IDs to get-endpoint metadata. |
| 15 | + ENDPOINT_MAP: Flat map of *all* endpoints keyed by their string ID. |
| 16 | + TOTAL_ENDPOINTS: Total number of registered endpoints. |
| 17 | + TOTAL_CATEGORIES: Total number of API categories. |
1 | 18 | """ |
2 | | -Databricks Workspace API Catalog |
3 | | -Complete catalog of all major Databricks workspace REST APIs. |
4 | | -""" |
5 | | -from typing import Dict, Any, List |
6 | 19 |
|
7 | | -STR = "string" |
8 | | -INT = "integer" |
9 | | -BOOL = "boolean" |
| 20 | +from typing import Any, Dict, List, Optional |
| 21 | + |
| 22 | +STR: str = "string" |
| 23 | +INT: str = "integer" |
| 24 | +BOOL: str = "boolean" |
10 | 25 |
|
11 | 26 |
|
12 | | -def _p(name: str, desc: str, required: bool = False, type_: str = STR, default: str = "") -> Dict: |
13 | | - return {"name": name, "description": desc, "required": required, "type": type_, "default": default} |
| 27 | +def _p( |
| 28 | + name: str, |
| 29 | + desc: str, |
| 30 | + required: bool = False, |
| 31 | + type_: str = STR, |
| 32 | + default: str = "", |
| 33 | +) -> Dict[str, Any]: |
| 34 | + """Build a parameter definition dict for an endpoint. |
| 35 | +
|
| 36 | + Args: |
| 37 | + name: Parameter name as expected by the Databricks REST API. |
| 38 | + desc: Human-readable description shown in the UI. |
| 39 | + required: Whether the parameter is mandatory. |
| 40 | + type_: Data type hint (``"string"``, ``"integer"``, or |
| 41 | + ``"boolean"``). |
| 42 | + default: Default value pre-filled in the input field. |
| 43 | +
|
| 44 | + Returns: |
| 45 | + A parameter dict consumable by :func:`app.build_param_form`. |
| 46 | + """ |
| 47 | + return { |
| 48 | + "name": name, |
| 49 | + "description": desc, |
| 50 | + "required": required, |
| 51 | + "type": type_, |
| 52 | + "default": default, |
| 53 | + } |
14 | 54 |
|
15 | 55 |
|
16 | 56 | API_CATALOG: Dict[str, Any] = { |
@@ -620,17 +660,48 @@ def _p(name: str, desc: str, required: bool = False, type_: str = STR, default: |
620 | 660 | } |
621 | 661 |
|
622 | 662 |
|
623 | | -def _nested_get(obj: Dict, dotted_key: str): |
624 | | - """Get a value from a dict using a dotted key like 'settings.name'.""" |
| 663 | +def _nested_get(obj: Dict, dotted_key: str) -> Any: |
| 664 | + """Retrieve a value from a nested dict using a dotted key path. |
| 665 | +
|
| 666 | + Args: |
| 667 | + obj: The root dictionary. |
| 668 | + dotted_key: Dot-separated key path (e.g. ``"settings.name"``). |
| 669 | +
|
| 670 | + Returns: |
| 671 | + The resolved value, or ``None`` if any segment is missing. |
| 672 | + """ |
625 | 673 | for part in dotted_key.split("."): |
626 | 674 | if not isinstance(obj, dict): |
627 | 675 | return None |
628 | 676 | obj = obj.get(part) |
629 | 677 | return obj |
630 | 678 |
|
631 | 679 |
|
632 | | -def extract_chips(endpoint_id: str, data: Any) -> List[Dict]: |
633 | | - """Extract clickable ID chips from a list API response.""" |
| 680 | +def extract_chips(endpoint_id: str, data: Any) -> List[Dict[str, Any]]: |
| 681 | + """Extract clickable ID chip descriptors from a list API response. |
| 682 | +
|
| 683 | + Chips power the inline navigation links and side-panel items that |
| 684 | + let users jump from a list response to the corresponding *get* |
| 685 | + endpoint for each row. |
| 686 | +
|
| 687 | + Args: |
| 688 | + endpoint_id: The ``id`` of the list endpoint whose response is |
| 689 | + being processed (must be a key in :data:`LIST_TO_GET`). |
| 690 | + data: The parsed JSON response body (typically a ``dict`` with |
| 691 | + a top-level array key). |
| 692 | +
|
| 693 | + Returns: |
| 694 | + A list of chip dicts, each containing: |
| 695 | +
|
| 696 | + * ``get_id`` -- target *get* endpoint ID. |
| 697 | + * ``param`` -- query/path parameter name for the get call. |
| 698 | + * ``id_field`` -- JSON key that holds the identifier. |
| 699 | + * ``value`` -- the identifier value (stringified). |
| 700 | + * ``label`` -- short display label (max 60 chars). |
| 701 | + * ``title`` -- tooltip text (typically the raw ID). |
| 702 | + * ``extras`` -- additional params to pass along. |
| 703 | + * ``actions`` -- list of secondary action dicts. |
| 704 | + """ |
634 | 705 | mapping = LIST_TO_GET.get(endpoint_id) |
635 | 706 | if not mapping: |
636 | 707 | return [] |
@@ -679,17 +750,35 @@ def extract_chips(endpoint_id: str, data: Any) -> List[Dict]: |
679 | 750 | return chips |
680 | 751 |
|
681 | 752 |
|
682 | | -def get_endpoint_by_id(endpoint_id: str) -> Dict[str, Any]: |
683 | | - """Find an endpoint by its ID across all categories.""" |
| 753 | +def get_endpoint_by_id(endpoint_id: str) -> Optional[Dict[str, Any]]: |
| 754 | + """Find an endpoint definition by its unique string ID. |
| 755 | +
|
| 756 | + Searches every category in :data:`API_CATALOG`. |
| 757 | +
|
| 758 | + Args: |
| 759 | + endpoint_id: The endpoint ``id`` to look up (e.g. |
| 760 | + ``"clusters-list"``). |
| 761 | +
|
| 762 | + Returns: |
| 763 | + A copy of the endpoint dict enriched with ``category`` and |
| 764 | + ``category_color`` keys, or ``None`` if not found. |
| 765 | + """ |
684 | 766 | for category_name, category in API_CATALOG.items(): |
685 | 767 | for endpoint in category["endpoints"]: |
686 | 768 | if endpoint["id"] == endpoint_id: |
687 | 769 | return {**endpoint, "category": category_name, "category_color": category["color"]} |
688 | 770 | return None |
689 | 771 |
|
690 | 772 |
|
691 | | -def build_endpoint_map() -> Dict[str, Dict]: |
692 | | - """Build a flat map of all endpoints keyed by ID.""" |
| 773 | +def build_endpoint_map() -> Dict[str, Dict[str, Any]]: |
| 774 | + """Build a flat lookup of all endpoints keyed by their string ID. |
| 775 | +
|
| 776 | + Each value is an endpoint dict augmented with ``category`` and |
| 777 | + ``category_color``. |
| 778 | +
|
| 779 | + Returns: |
| 780 | + A ``{endpoint_id: endpoint_dict}`` mapping. |
| 781 | + """ |
693 | 782 | return { |
694 | 783 | endpoint["id"]: {**endpoint, "category": cat_name, "category_color": cat["color"]} |
695 | 784 | for cat_name, cat in API_CATALOG.items() |
|
0 commit comments