|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import List |
| 4 | + |
| 5 | +from metabase.missing import MISSING |
| 6 | +from metabase.resource import CreateResource, GetResource, ListResource, UpdateResource |
| 7 | + |
| 8 | + |
| 9 | +class Card(ListResource, CreateResource, GetResource, UpdateResource): |
| 10 | + ENDPOINT = "/api/card" |
| 11 | + |
| 12 | + id: int |
| 13 | + table_id: int |
| 14 | + database_id: int |
| 15 | + collection_id: int |
| 16 | + creator_id: int |
| 17 | + made_public_by_id: int |
| 18 | + public_uuid: str |
| 19 | + |
| 20 | + name: str |
| 21 | + description: str |
| 22 | + |
| 23 | + collection: dict # TODO: Collection |
| 24 | + collection_position: int |
| 25 | + |
| 26 | + query_type: str |
| 27 | + dataset_query: dict # TODO: DatasetQuery |
| 28 | + display: str |
| 29 | + visualization_settings: dict # TODO: VisualizationSettings |
| 30 | + result_metadata: List[dict] |
| 31 | + |
| 32 | + embedding_params: dict |
| 33 | + cache_ttl: str |
| 34 | + creator: "User" |
| 35 | + |
| 36 | + favorite: bool |
| 37 | + archived: bool |
| 38 | + enable_embedding: bool |
| 39 | + |
| 40 | + updated_at: str |
| 41 | + created_at: str |
| 42 | + |
| 43 | + @classmethod |
| 44 | + def list(cls) -> List[Card]: |
| 45 | + """ |
| 46 | + Get all the Cards. Option filter param f can be used to change the set |
| 47 | + of Cards that are returned; default is all, but other options include |
| 48 | + mine, fav, database, table, recent, popular, and archived. |
| 49 | +
|
| 50 | + See corresponding implementation functions above for the specific behavior |
| 51 | + of each filter option. :card_index:. |
| 52 | + """ |
| 53 | + # TODO: add support for endpoint parameters: f, model_id. |
| 54 | + return super(Card, cls).list() |
| 55 | + |
| 56 | + @classmethod |
| 57 | + def get(cls, id: int) -> Card: |
| 58 | + """ |
| 59 | + Get Card with ID. |
| 60 | + """ |
| 61 | + return super(Card, cls).get(id) |
| 62 | + |
| 63 | + @classmethod |
| 64 | + def create( |
| 65 | + cls, |
| 66 | + name: str, |
| 67 | + dataset_query: dict, # TODO: DatasetQuery |
| 68 | + visualization_settings: dict, # TODO: VisualizationSettings |
| 69 | + display: str, |
| 70 | + description: str = None, |
| 71 | + collection_id: str = None, |
| 72 | + collection_position: int = None, |
| 73 | + result_metadata: List[dict] = None, |
| 74 | + metadata_checksum: str = None, |
| 75 | + cache_ttl: int = None, |
| 76 | + **kwargs, |
| 77 | + ) -> Card: |
| 78 | + """ |
| 79 | + Create a new Card. |
| 80 | + """ |
| 81 | + return super(Card, cls).create( |
| 82 | + name=name, |
| 83 | + dataset_query=dataset_query, |
| 84 | + visualization_settings=visualization_settings, |
| 85 | + display=display, |
| 86 | + description=description, |
| 87 | + collection_id=collection_id, |
| 88 | + collection_position=collection_position, |
| 89 | + result_metadata=result_metadata, |
| 90 | + metadata_checksum=metadata_checksum, |
| 91 | + cache_ttl=cache_ttl, |
| 92 | + **kwargs, |
| 93 | + ) |
| 94 | + |
| 95 | + def update( |
| 96 | + self, |
| 97 | + name: str = MISSING, |
| 98 | + dataset_query: dict = MISSING, # TODO: DatasetQuery |
| 99 | + visualization_settings: dict = MISSING, # TODO: VisualizationSettings |
| 100 | + display: str = MISSING, |
| 101 | + description: str = MISSING, |
| 102 | + collection_id: str = MISSING, |
| 103 | + collection_position: int = MISSING, |
| 104 | + result_metadata: List[dict] = MISSING, |
| 105 | + metadata_checksum: str = MISSING, |
| 106 | + archived: bool = MISSING, |
| 107 | + enable_embedding: bool = MISSING, |
| 108 | + embedding_params: dict = MISSING, |
| 109 | + cache_ttl: int = None, |
| 110 | + **kwargs, |
| 111 | + ) -> None: |
| 112 | + """ |
| 113 | + Update a Card. |
| 114 | + """ |
| 115 | + return super(Card, self).update( |
| 116 | + name=name, |
| 117 | + dataset_query=dataset_query, |
| 118 | + visualization_settings=visualization_settings, |
| 119 | + display=display, |
| 120 | + description=description, |
| 121 | + collection_id=collection_id, |
| 122 | + collection_position=collection_position, |
| 123 | + result_metadata=result_metadata, |
| 124 | + metadata_checksum=metadata_checksum, |
| 125 | + archived=archived, |
| 126 | + enable_embedding=enable_embedding, |
| 127 | + embedding_params=embedding_params, |
| 128 | + cache_ttl=cache_ttl, |
| 129 | + ) |
| 130 | + |
| 131 | + def archive(self): |
| 132 | + """Archive a Metric.""" |
| 133 | + return self.update( |
| 134 | + archived=True, revision_message="Archived by metabase-python." |
| 135 | + ) |
0 commit comments