diff --git a/README.md b/README.md index 0783b95..7a83f15 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ You usually don't need to deal with these functions directly (e.g. [get_item_inf - [create_segment](https://github.com/vvaezian/metabase_api_python/blob/150c8143bf3ec964568d54bddd80bf9c1b2ca214/metabase_api/metabase_api.py#L486) - [copy_card](https://github.com/vvaezian/metabase_api_python/blob/150c8143bf3ec964568d54bddd80bf9c1b2ca214/metabase_api/metabase_api.py#L530) - [copy_pulse](https://github.com/vvaezian/metabase_api_python/blob/150c8143bf3ec964568d54bddd80bf9c1b2ca214/metabase_api/metabase_api.py#L591) +- [create_dashboard] - [copy_dashboard](https://github.com/vvaezian/metabase_api_python/blob/150c8143bf3ec964568d54bddd80bf9c1b2ca214/metabase_api/metabase_api.py#L643) - [copy_collection](https://github.com/vvaezian/metabase_api_python/blob/150c8143bf3ec964568d54bddd80bf9c1b2ca214/metabase_api/metabase_api.py#L736) - [clone_card](https://github.com/vvaezian/metabase_api_python/blob/77ef837972bc169f96a3ca520da769e0b933e8a8/metabase_api/metabase_api.py#L1003) diff --git a/metabase_api/metabase_api.py b/metabase_api/metabase_api.py index 6d3f07f..84229a6 100644 --- a/metabase_api/metabase_api.py +++ b/metabase_api/metabase_api.py @@ -642,6 +642,66 @@ def create_segment(self, segment_name, column_name, column_values, segment_descr + def create_dashboard( + self, name, description=None, parameters=[], cache_ttl=None, + collection_id=None, collection_position=None, collection_name=None, + return_dashboard=False): + """ + Create a dashboard using the given arguments utilizing the endpoint 'POST /api/dashboard/'. + If collection is not given, the root collection is used. + + Keyword arguments: + name -- name used to create the dashboard (mandatory) + description -- description of the dashboard (default Non) + collection_name -- name of the collection to place the dashboard (default None) + collection_id -- id of the collection to place the dashboard (default None) + collection_position -- position of the dashboard in the collection(default None) + cache_ttl -- Cache Time-to-Live, multiplier for caching management (see https://www.metabase.com/docs/latest/configuring-metabase/caching#cache-time-to-live-ttl) + parameters -- Array of maps for fine-tuning your dashboard. Each map must be of the form {'id': ..., 'type': ...} + return_dashboard -- whather to return the created dashboard info (default False) + """ + custom_json={} + + if not(name and isinstance(name, str)): + raise ValueError("Dashboard name incorrect. Please provide a valid string for dashboard name") + custom_json['name'] = name + + if description: + custom_json['description'] = description + + if parameters: + if not isinstance(parameters, list): + raise ValueError("Parameter 'parameters' incorrect. Please provide an array") + for element in parameters: + if set(element.keys()) != {'id', 'type'}: + raise ValueError("Parameter 'parameters' incorrect. Please provide an array of maps of keys 'id' and 'type'") + custom_json['parameters'] = parameters + + if cache_ttl: + if not(isinstance(cache_ttl, int) and cache_ttl > 0): + raise ValueError("Parameter `cache_ttl` must be a scrictly positive integer. Please provide a correct value") + custom_json['cache_ttl']: cache_ttl + + collection_id = self.get_item_id('collection', collection_name) if collection_name else collection_id + if collection_id: + if isinstance(collection_id, None): + pass + elif isinstance(collection_id, int): + custom_json['collection_id'] = collection_id + else: + raise ValueError("Parameter `collection_id` must be an integer. Please provide a correct value") + + if collection_position: + if not(isinstance(collection_position, int) and collection_position > 0): + raise ValueError("Parameter `collection_position` must be a stricly positive integer. Please provide a correct value") + custom_json['collection_position'] = collection_position + + res = self.post("/api/dashboard", json=custom_json) + if return_dashboard: + return res + + + def copy_card(self, source_card_name=None, source_card_id=None, source_collection_name=None, source_collection_id=None, destination_card_name=None, diff --git a/tests/test_metabase_api.py b/tests/test_metabase_api.py index 04ded25..185242d 100644 --- a/tests/test_metabase_api.py +++ b/tests/test_metabase_api.py @@ -240,6 +240,23 @@ def test_create_segment(self): + def test_create_dashboard(self): + t = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + dummy_name = 'dummy dashboard' + + with self.assertRaises(ValueError) as error: + res = mb.create_dashboard(name='') + self.assertEqual( + str(error.exception), + 'Dashboard name incorrect. Please provide a valid string for dashboard name' + ) + res = mb.create_dashboard(dummy_name) + + Metabase_API_Test.cleanup_objects['dashboard'].append(res['id']) + pass + + + def test_copy_card(self): t = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') newCard_id = mb.copy_card(source_card_id=166, destination_collection_id=29, destination_card_name='test_copy_card_{}'.format(t))