diff --git a/dune_client/api/table.py b/dune_client/api/table.py index 239d0fc..c8d4762 100644 --- a/dune_client/api/table.py +++ b/dune_client/api/table.py @@ -12,6 +12,7 @@ InsertTableResult, CreateTableResult, DeleteTableResult, + ClearTableResult, ) @@ -106,7 +107,20 @@ def insert_table( try: return InsertTableResult.from_dict(result_json) except KeyError as err: - raise DuneError(result_json, "ResultsResponse", err) from err + raise DuneError(result_json, "InsertTable", err) from err + + def clear_data(self, namespace: str, table_name: str) -> ClearTableResult: + """ + https://docs.dune.com/api-reference/tables/endpoint/clear + The Clear endpoint removes all the data in the specified table, + but does not delete the table. + """ + + result_json = self._post(route=f"/table/{namespace}/{table_name}/clear") + try: + return ClearTableResult.from_dict(result_json) + except KeyError as err: + raise DuneError(result_json, "ClearData", err) from err def delete_table(self, namespace: str, table_name: str) -> DeleteTableResult: """ @@ -115,4 +129,7 @@ def delete_table(self, namespace: str, table_name: str) -> DeleteTableResult: """ response_json = self._delete(route=f"/table/{namespace}/{table_name}") - return DeleteTableResult.from_dict(response_json) + try: + return DeleteTableResult.from_dict(response_json) + except KeyError as err: + raise DuneError(response_json, "DeleteTable", err) from err diff --git a/dune_client/models.py b/dune_client/models.py index b975edb..d4230d9 100644 --- a/dune_client/models.py +++ b/dune_client/models.py @@ -365,10 +365,12 @@ class CreateTableResult(DataClassJsonMixin): Data type returned by table/create operation """ - example_query: str - full_name: str namespace: str table_name: str + full_name: str + example_query: str + already_existed: bool + message: str @dataclass @@ -388,3 +390,12 @@ class DeleteTableResult(DataClassJsonMixin): """ message: str + + +@dataclass +class ClearTableResult(DataClassJsonMixin): + """ + Data type returned by table/clear operation + """ + + message: str diff --git a/tests/e2e/test_client.py b/tests/e2e/test_client.py index 799c0c1..ed4b41a 100644 --- a/tests/e2e/test_client.py +++ b/tests/e2e/test_client.py @@ -14,6 +14,7 @@ InsertTableResult, CreateTableResult, DeleteTableResult, + ClearTableResult, ) from dune_client.types import QueryParameter from dune_client.client import DuneClient @@ -274,17 +275,39 @@ def test_insert_table_csv_success(self): # Make sure the table already exists and csv matches table schema. # You will need to change the namespace to your own. client = DuneClient(self.valid_api_key) + namespace = "bh2smith" + table_name = "dataset_e2e_test" + client.create_table( + namespace, + table_name, + schema=[ + {"name": "date", "type": "timestamp"}, + {"name": "dgs10", "type": "double"}, + ], + ) with open("./tests/fixtures/sample_table_insert.csv", "rb") as data: self.assertEqual( client.insert_table( - namespace="test", - table_name="dataset_e2e_test", + namespace, + table_name, data=data, content_type="text/csv", ), - InsertTableResult(rows_written=1), + InsertTableResult(rows_written=1, bytes_written=33), ) + @unittest.skip("Requires custom namespace and table_name input.") + def test_clear_data(self): + client = DuneClient(self.valid_api_key) + namespace = "bh2smith" + table_name = "dataset_e2e_test" + self.assertEqual( + client.clear_data(namespace, table_name), + ClearTableResult( + message="Table dune.bh2smith.dataset_e2e_test successfully cleared" + ), + ) + @unittest.skip("Requires custom namespace and table_name input.") def test_insert_table_json_success(self): # Make sure the table already exists and json matches table schema. @@ -298,7 +321,7 @@ def test_insert_table_json_success(self): data=data, content_type="application/x-ndjson", ), - InsertTableResult(rows_written=1), + InsertTableResult(rows_written=1, bytes_written=33), ) @unittest.skip("Requires custom namespace and table_name input.")