|
| 1 | +async def get_item_info(self, item_type, item_id=None, item_name=None, |
| 2 | + collection_id=None, collection_name=None, |
| 3 | + params=None): |
| 4 | + """Async version of get_item_info""" |
| 5 | + assert item_type in ['database', 'table', 'card', 'collection', 'dashboard', 'pulse', 'segment'] |
| 6 | + |
| 7 | + if params: |
| 8 | + assert type(params) == dict |
| 9 | + |
| 10 | + if not item_id: |
| 11 | + if not item_name: |
| 12 | + raise ValueError(f'Either the name or id of the {item_type} must be provided.') |
| 13 | + item_id = await self.get_item_id(item_type, item_name, collection_id=collection_id, collection_name=collection_name) |
| 14 | + |
| 15 | + res = await self.get(f"/api/{item_type}/{item_id}", params=params) |
| 16 | + if res: |
| 17 | + return res |
| 18 | + else: |
| 19 | + raise ValueError(f'There is no {item_type} with the id "{item_id}"') |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +async def get_item_name(self, item_type, item_id): |
| 24 | + """Async version of get_item_name""" |
| 25 | + assert item_type in ['database', 'table', 'card', 'collection', 'dashboard', 'pulse', 'segment'] |
| 26 | + |
| 27 | + res = await self.get(f"/api/{item_type}/{item_id}") |
| 28 | + if res: |
| 29 | + return res['name'] |
| 30 | + else: |
| 31 | + raise ValueError(f'There is no {item_type} with the id "{item_id}"') |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +async def get_item_id(self, item_type, item_name, collection_id=None, collection_name=None, db_id=None, db_name=None, table_id=None): |
| 36 | + """Async version of get_item_id""" |
| 37 | + assert item_type in ['database', 'table', 'card', 'collection', 'dashboard', 'pulse', 'segment'] |
| 38 | + |
| 39 | + if item_type in ['card', 'dashboard', 'pulse']: |
| 40 | + if not collection_id: |
| 41 | + if not collection_name: |
| 42 | + # Collection name/id is not provided. Searching in all collections |
| 43 | + items = await self.get(f"/api/{item_type}/") |
| 44 | + item_IDs = [i['id'] for i in items if i['name'] == item_name and i['archived'] == False] |
| 45 | + else: |
| 46 | + collection_id = await self.get_item_id('collection', collection_name) if collection_name != 'root' else None |
| 47 | + items = await self.get(f"/api/{item_type}/") |
| 48 | + item_IDs = [i['id'] for i in items if i['name'] == item_name |
| 49 | + and i['collection_id'] == collection_id |
| 50 | + and i['archived'] == False] |
| 51 | + else: |
| 52 | + collection_name = await self.get_item_name('collection', collection_id) |
| 53 | + items = await self.get(f"/api/{item_type}/") |
| 54 | + item_IDs = [i['id'] for i in items if i['name'] == item_name |
| 55 | + and i['collection_id'] == collection_id |
| 56 | + and i['archived'] == False] |
| 57 | + |
| 58 | + if len(item_IDs) > 1: |
| 59 | + if not collection_name: |
| 60 | + raise ValueError(f'There is more than one {item_type} with the name "{item_name}".\n\ |
| 61 | + Provide collection id/name to limit the search space') |
| 62 | + raise ValueError(f'There is more than one {item_type} with the name "{item_name}" in the collection "{collection_name}"') |
| 63 | + if len(item_IDs) == 0: |
| 64 | + if not collection_name: |
| 65 | + raise ValueError(f'There is no {item_type} with the name "{item_name}"') |
| 66 | + raise ValueError(f'There is no item with the name "{item_name}" in the collection "{collection_name}"') |
| 67 | + |
| 68 | + return item_IDs[0] |
| 69 | + |
| 70 | + if item_type == 'collection': |
| 71 | + collections = await self.get("/api/collection/") |
| 72 | + collection_IDs = [i['id'] for i in collections if i['name'] == item_name] |
| 73 | + |
| 74 | + if len(collection_IDs) > 1: |
| 75 | + raise ValueError(f'There is more than one collection with the name "{item_name}"') |
| 76 | + if len(collection_IDs) == 0: |
| 77 | + raise ValueError(f'There is no collection with the name "{item_name}"') |
| 78 | + |
| 79 | + return collection_IDs[0] |
| 80 | + |
| 81 | + if item_type == 'database': |
| 82 | + res = await self.get("/api/database/") |
| 83 | + if type(res) == dict: # in Metabase version *.40.0 the format of the returned result for this endpoint changed |
| 84 | + res = res['data'] |
| 85 | + db_IDs = [i['id'] for i in res if i['name'] == item_name] |
| 86 | + |
| 87 | + if len(db_IDs) > 1: |
| 88 | + raise ValueError(f'There is more than one DB with the name "{item_name}"') |
| 89 | + if len(db_IDs) == 0: |
| 90 | + raise ValueError(f'There is no DB with the name "{item_name}"') |
| 91 | + |
| 92 | + return db_IDs[0] |
| 93 | + |
| 94 | + if item_type == 'table': |
| 95 | + tables = await self.get("/api/table/") |
| 96 | + |
| 97 | + if db_id: |
| 98 | + table_IDs = [i['id'] for i in tables if i['name'] == item_name and i['db']['id'] == db_id] |
| 99 | + elif db_name: |
| 100 | + table_IDs = [i['id'] for i in tables if i['name'] == item_name and i['db']['name'] == db_name] |
| 101 | + else: |
| 102 | + table_IDs = [i['id'] for i in tables if i['name'] == item_name] |
| 103 | + |
| 104 | + if len(table_IDs) > 1: |
| 105 | + raise ValueError(f'There is more than one table with the name {item_name}. Provide db id/name.') |
| 106 | + if len(table_IDs) == 0: |
| 107 | + raise ValueError(f'There is no table with the name "{item_name}" (in the provided db, if any)') |
| 108 | + |
| 109 | + return table_IDs[0] |
| 110 | + |
| 111 | + if item_type == 'segment': |
| 112 | + segments = await self.get("/api/segment/") |
| 113 | + segment_IDs = [i['id'] for i in segments if i['name'] == item_name and (not table_id or i['table_id'] == table_id)] |
| 114 | + |
| 115 | + if len(segment_IDs) > 1: |
| 116 | + raise ValueError(f'There is more than one segment with the name "{item_name}"') |
| 117 | + if len(segment_IDs) == 0: |
| 118 | + raise ValueError(f'There is no segment with the name "{item_name}"') |
| 119 | + |
| 120 | + return segment_IDs[0] |
0 commit comments