Fix #15678: Accomodate Metabase API changes (#15692)

This commit is contained in:
Mayur Singal 2024-03-27 15:19:20 +05:30 committed by GitHub
parent 6acb47a87a
commit bf0ec44f4b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 43 additions and 11 deletions

View File

@ -85,18 +85,49 @@ class MetabaseClient:
)
self.client = REST(client_config)
def get_dashboards_list(self) -> List[MetabaseDashboard]:
def get_dashboards_list(
self, collections: List[MetabaseCollection]
) -> List[MetabaseDashboard]:
"""
Get List of all dashboards
"""
try:
resp_dashboards = self.client.get("/dashboard")
dashboards = []
for collection in collections or []:
try:
resp_dashboards = self.client.get(
f"/collection/{collection.id}/items?models=dashboard"
)
if resp_dashboards:
dashboard_list = MetabaseDashboardList(**resp_dashboards)
dashboards.extend(dashboard_list.data)
except Exception:
logger.debug(traceback.format_exc())
logger.warning("Failed to fetch the dashboard list")
return dashboards
def get_dashboards_list_test_conn(
self, collections: List[MetabaseCollection]
) -> List[MetabaseDashboard]:
"""
Get List of all dashboards
"""
for collection in collections or []:
resp_dashboards = self.client.get(
f"/collection/{collection.id}/items?models=dashboard"
)
if resp_dashboards:
dashboard_list = MetabaseDashboardList(dashboards=resp_dashboards)
return dashboard_list.dashboards
except Exception:
logger.debug(traceback.format_exc())
logger.warning("Failed to fetch the dashboard list")
dashboard_list = MetabaseDashboardList(**resp_dashboards)
return dashboard_list.data
return []
def get_collections_list_test_conn(self) -> List[MetabaseCollection]:
"""
Get List of all collections
"""
resp_collections = self.client.get("/collection")
if resp_collections:
collection_list = MetabaseCollectionList(collections=resp_collections)
return collection_list.collections
return []
def get_collections_list(self) -> List[MetabaseCollection]:

View File

@ -44,7 +44,8 @@ def test_connection(
"""
def custom_executor():
return client.get_dashboards_list()
collections = client.get_collections_list_test_conn()
return client.get_dashboards_list_test_conn(collections)
test_fn = {"GetDashboards": custom_executor}

View File

@ -94,7 +94,7 @@ class MetabaseSource(DashboardServiceSource):
"""
Get List of all dashboards
"""
return self.client.get_dashboards_list()
return self.client.get_dashboards_list(self.collections)
def get_dashboard_name(self, dashboard: MetabaseDashboard) -> str:
"""

View File

@ -37,7 +37,7 @@ class MetabaseCollection(BaseModel):
class MetabaseDashboardList(BaseModel):
dashboards: Optional[List[MetabaseDashboard]]
data: Optional[List[MetabaseDashboard]]
class MetabaseCollectionList(BaseModel):