Skip to content

Commit bd4594e

Browse files
authored
Add Table Method clear_data (#146)
Satisfies: https://docs.dune.com/api-reference/tables/endpoint/clear
1 parent 5177a1d commit bd4594e

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

dune_client/api/table.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
InsertTableResult,
1313
CreateTableResult,
1414
DeleteTableResult,
15+
ClearTableResult,
1516
)
1617

1718

@@ -106,7 +107,20 @@ def insert_table(
106107
try:
107108
return InsertTableResult.from_dict(result_json)
108109
except KeyError as err:
109-
raise DuneError(result_json, "ResultsResponse", err) from err
110+
raise DuneError(result_json, "InsertTable", err) from err
111+
112+
def clear_data(self, namespace: str, table_name: str) -> ClearTableResult:
113+
"""
114+
https://docs.dune.com/api-reference/tables/endpoint/clear
115+
The Clear endpoint removes all the data in the specified table,
116+
but does not delete the table.
117+
"""
118+
119+
result_json = self._post(route=f"/table/{namespace}/{table_name}/clear")
120+
try:
121+
return ClearTableResult.from_dict(result_json)
122+
except KeyError as err:
123+
raise DuneError(result_json, "ClearData", err) from err
110124

111125
def delete_table(self, namespace: str, table_name: str) -> DeleteTableResult:
112126
"""
@@ -115,4 +129,7 @@ def delete_table(self, namespace: str, table_name: str) -> DeleteTableResult:
115129
"""
116130

117131
response_json = self._delete(route=f"/table/{namespace}/{table_name}")
118-
return DeleteTableResult.from_dict(response_json)
132+
try:
133+
return DeleteTableResult.from_dict(response_json)
134+
except KeyError as err:
135+
raise DuneError(response_json, "DeleteTable", err) from err

dune_client/models.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,12 @@ class CreateTableResult(DataClassJsonMixin):
365365
Data type returned by table/create operation
366366
"""
367367

368-
example_query: str
369-
full_name: str
370368
namespace: str
371369
table_name: str
370+
full_name: str
371+
example_query: str
372+
already_existed: bool
373+
message: str
372374

373375

374376
@dataclass
@@ -388,3 +390,12 @@ class DeleteTableResult(DataClassJsonMixin):
388390
"""
389391

390392
message: str
393+
394+
395+
@dataclass
396+
class ClearTableResult(DataClassJsonMixin):
397+
"""
398+
Data type returned by table/clear operation
399+
"""
400+
401+
message: str

tests/e2e/test_client.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
InsertTableResult,
1515
CreateTableResult,
1616
DeleteTableResult,
17+
ClearTableResult,
1718
)
1819
from dune_client.types import QueryParameter
1920
from dune_client.client import DuneClient
@@ -274,17 +275,39 @@ def test_insert_table_csv_success(self):
274275
# Make sure the table already exists and csv matches table schema.
275276
# You will need to change the namespace to your own.
276277
client = DuneClient(self.valid_api_key)
278+
namespace = "bh2smith"
279+
table_name = "dataset_e2e_test"
280+
client.create_table(
281+
namespace,
282+
table_name,
283+
schema=[
284+
{"name": "date", "type": "timestamp"},
285+
{"name": "dgs10", "type": "double"},
286+
],
287+
)
277288
with open("./tests/fixtures/sample_table_insert.csv", "rb") as data:
278289
self.assertEqual(
279290
client.insert_table(
280-
namespace="test",
281-
table_name="dataset_e2e_test",
291+
namespace,
292+
table_name,
282293
data=data,
283294
content_type="text/csv",
284295
),
285-
InsertTableResult(rows_written=1),
296+
InsertTableResult(rows_written=1, bytes_written=33),
286297
)
287298

299+
@unittest.skip("Requires custom namespace and table_name input.")
300+
def test_clear_data(self):
301+
client = DuneClient(self.valid_api_key)
302+
namespace = "bh2smith"
303+
table_name = "dataset_e2e_test"
304+
self.assertEqual(
305+
client.clear_data(namespace, table_name),
306+
ClearTableResult(
307+
message="Table dune.bh2smith.dataset_e2e_test successfully cleared"
308+
),
309+
)
310+
288311
@unittest.skip("Requires custom namespace and table_name input.")
289312
def test_insert_table_json_success(self):
290313
# Make sure the table already exists and json matches table schema.
@@ -298,7 +321,7 @@ def test_insert_table_json_success(self):
298321
data=data,
299322
content_type="application/x-ndjson",
300323
),
301-
InsertTableResult(rows_written=1),
324+
InsertTableResult(rows_written=1, bytes_written=33),
302325
)
303326

304327
@unittest.skip("Requires custom namespace and table_name input.")

0 commit comments

Comments
 (0)