|
| 1 | +# (C) 2022 GoodData Corporation |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from typing import Any, List, Optional, Type |
| 5 | + |
| 6 | +import attr |
| 7 | +import cattr |
| 8 | +from cattrs.gen import make_dict_structure_fn, make_dict_unstructure_fn, override |
| 9 | + |
| 10 | +from gooddata_metadata_client.model.dependent_entities_edge import DependentEntitiesEdge |
| 11 | +from gooddata_metadata_client.model.dependent_entities_graph import DependentEntitiesGraph |
| 12 | +from gooddata_metadata_client.model.dependent_entities_node import DependentEntitiesNode |
| 13 | +from gooddata_metadata_client.model.dependent_entities_request import DependentEntitiesRequest |
| 14 | +from gooddata_metadata_client.model.dependent_entities_response import DependentEntitiesResponse |
| 15 | +from gooddata_metadata_client.model.entity_identifier import EntityIdentifier |
| 16 | +from gooddata_sdk.catalog.base import Base |
| 17 | + |
| 18 | + |
| 19 | +@attr.s(auto_attribs=True, kw_only=True) |
| 20 | +class GraphBase(Base): |
| 21 | + @classmethod |
| 22 | + def _custom_converter(cls) -> Optional[cattr.GenConverter]: |
| 23 | + if cls._converter is None: |
| 24 | + cls._converter = cattr.GenConverter() |
| 25 | + unstructured_hook = make_dict_unstructure_fn( |
| 26 | + CatalogDependentEntitiesEdge, cls._converter, from_=override(rename="_from") |
| 27 | + ) |
| 28 | + structured_hook = make_dict_structure_fn( |
| 29 | + CatalogDependentEntitiesEdge, cls._converter, from_=override(rename="_from") |
| 30 | + ) |
| 31 | + cls._converter.register_unstructure_hook(CatalogDependentEntitiesEdge, unstructured_hook) |
| 32 | + cls._converter.register_structure_hook(CatalogDependentEntitiesEdge, structured_hook) |
| 33 | + return cls._converter |
| 34 | + |
| 35 | + def to_api(self) -> Any: |
| 36 | + """ |
| 37 | + to_api method is not supported for dependent entities graph for these reasons: |
| 38 | + * there is no endpoint which would require dependent entities as the input |
| 39 | + * it would take some effort to change to_api to support |
| 40 | + conversion regarding the from attribute (from\\_ -> _from) |
| 41 | + """ |
| 42 | + return NotImplemented |
| 43 | + |
| 44 | + |
| 45 | +@attr.s(auto_attribs=True, kw_only=True) |
| 46 | +class CatalogDependentEntitiesRequest(Base): |
| 47 | + identifiers: List[CatalogEntityIdentifier] = attr.field(factory=list) |
| 48 | + |
| 49 | + @staticmethod |
| 50 | + def client_class() -> Type[DependentEntitiesRequest]: |
| 51 | + return DependentEntitiesRequest |
| 52 | + |
| 53 | + |
| 54 | +@attr.s(auto_attribs=True, kw_only=True) |
| 55 | +class CatalogDependentEntitiesResponse(GraphBase): |
| 56 | + graph: CatalogDependentEntitiesGraph |
| 57 | + |
| 58 | + @staticmethod |
| 59 | + def client_class() -> Type[DependentEntitiesResponse]: |
| 60 | + return DependentEntitiesResponse |
| 61 | + |
| 62 | + |
| 63 | +@attr.s(auto_attribs=True, kw_only=True) |
| 64 | +class CatalogDependentEntitiesGraph(GraphBase): |
| 65 | + nodes: List[CatalogDependentEntitiesNode] = attr.field(factory=list) |
| 66 | + edges: List[CatalogDependentEntitiesEdge] = attr.field(factory=list) |
| 67 | + |
| 68 | + @staticmethod |
| 69 | + def client_class() -> Type[DependentEntitiesGraph]: |
| 70 | + return DependentEntitiesGraph |
| 71 | + |
| 72 | + |
| 73 | +@attr.s(auto_attribs=True, kw_only=True) |
| 74 | +class CatalogDependentEntitiesNode(Base): |
| 75 | + id: str |
| 76 | + type: str |
| 77 | + title: Optional[str] = None |
| 78 | + |
| 79 | + @staticmethod |
| 80 | + def client_class() -> Type[DependentEntitiesNode]: |
| 81 | + return DependentEntitiesNode |
| 82 | + |
| 83 | + |
| 84 | +@attr.s(auto_attribs=True, kw_only=True) |
| 85 | +class CatalogDependentEntitiesEdge(GraphBase): |
| 86 | + from_: CatalogEntityIdentifier |
| 87 | + """ |
| 88 | + Beware of the attribute from\\_ which may result in several issues. |
| 89 | + Important points: |
| 90 | + * from is a Python keyword and cannot be used as a variable name therefore we use from\\_ in dataclass |
| 91 | + * client class uses _from naming and as a result to_dict contains _from key |
| 92 | + * variable _from results in error |
| 93 | + * using from_dict we expect from\\_ |
| 94 | + """ |
| 95 | + to: CatalogEntityIdentifier |
| 96 | + |
| 97 | + @staticmethod |
| 98 | + def client_class() -> Type[DependentEntitiesEdge]: |
| 99 | + return DependentEntitiesEdge |
| 100 | + |
| 101 | + |
| 102 | +@attr.s(auto_attribs=True, kw_only=True) |
| 103 | +class CatalogEntityIdentifier(Base): |
| 104 | + id: str |
| 105 | + type: str |
| 106 | + |
| 107 | + @staticmethod |
| 108 | + def client_class() -> Type[EntityIdentifier]: |
| 109 | + return EntityIdentifier |
0 commit comments