Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions dune_client/api/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
InsertTableResult,
CreateTableResult,
DeleteTableResult,
ClearTableResult,
)


Expand Down Expand Up @@ -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:
"""
Expand All @@ -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
15 changes: 13 additions & 2 deletions dune_client/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -388,3 +390,12 @@ class DeleteTableResult(DataClassJsonMixin):
"""

message: str


@dataclass
class ClearTableResult(DataClassJsonMixin):
"""
Data type returned by table/clear operation
"""

message: str
31 changes: 27 additions & 4 deletions tests/e2e/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
InsertTableResult,
CreateTableResult,
DeleteTableResult,
ClearTableResult,
)
from dune_client.types import QueryParameter
from dune_client.client import DuneClient
Expand Down Expand Up @@ -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.
Expand All @@ -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.")
Expand Down
Loading