Skip to content

Commit

Permalink
Merge pull request #119 from duneanalytics/support-delete-table-endpoint
Browse files Browse the repository at this point in the history
Add `delete` table endpoint to sdk
  • Loading branch information
waddahAldrobi committed Apr 2, 2024
2 parents 3eab3fb + 1978a84 commit 22a2659
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ _version.py
venv/
tmp/
.vscode/
build/
.DS_Store
11 changes: 11 additions & 0 deletions dune_client/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,14 @@ def _patch(self, route: str, params: Any) -> Any:
timeout=self.request_timeout,
)
return self._handle_response(response)

def _delete(self, route: str) -> Any:
"""Generic interface for the DELETE method of a Dune API request"""
url = self._route_url(route)
self.logger.debug(f"DELETE received input url={url}")
response = self.http.delete(
url=url,
headers=self.default_headers(),
timeout=self.request_timeout,
)
return self._handle_response(response)
16 changes: 15 additions & 1 deletion dune_client/api/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
from typing import List, Dict, IO

from dune_client.api.base import BaseRouter
from dune_client.models import DuneError, InsertTableResult, CreateTableResult
from dune_client.models import (
DuneError,
InsertTableResult,
CreateTableResult,
DeleteTableResult,
)


class TableAPI(BaseRouter):
Expand Down Expand Up @@ -99,3 +104,12 @@ def insert_table(
data=data,
)
return InsertTableResult.from_dict(result_json)

def delete_table(self, namespace: str, table_name: str) -> DeleteTableResult:
"""
https://docs.dune.com/api-reference/tables/endpoint/delete
The delete table endpoint allows you to delete an existing table in Dune.
"""

response_json = self._delete(route=f"/table/{namespace}/{table_name}")
return DeleteTableResult.from_dict(response_json)
9 changes: 9 additions & 0 deletions dune_client/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,12 @@ class InsertTableResult(DataClassJsonMixin):
"""

rows_written: int


@dataclass
class DeleteTableResult(DataClassJsonMixin):
"""
Data type returned by table/delete operation
"""

message: str
30 changes: 26 additions & 4 deletions tests/e2e/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
DuneError,
InsertTableResult,
CreateTableResult,
DeleteTableResult,
)
from dune_client.types import QueryParameter
from dune_client.client import DuneClient
Expand Down Expand Up @@ -226,7 +227,7 @@ def test_get_latest_result_with_query_id(self):
results = dune.get_latest_result(self.query.query_id).get_rows()
self.assertGreater(len(results), 0)

@unittest.skip("This is a plus subscription endpoint.")
@unittest.skip("Requires custom namespace and table_name input.")
def test_upload_csv_success(self):
client = DuneClient(self.valid_api_key)
self.assertEqual(
Expand All @@ -238,7 +239,7 @@ def test_upload_csv_success(self):
True,
)

@unittest.skip("This is a plus subscription endpoint.")
@unittest.skip("Requires custom namespace and table_name input.")
def test_create_table_success(self):
# Make sure the table doesn't already exist.
# You will need to change the namespace to your own.
Expand Down Expand Up @@ -268,7 +269,7 @@ def test_create_table_success(self):
),
)

@unittest.skip("This is a plus subscription endpoint.")
@unittest.skip("Requires custom namespace and table_name input.")
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.
Expand All @@ -284,7 +285,7 @@ def test_insert_table_csv_success(self):
InsertTableResult(rows_written=1),
)

@unittest.skip("This is a plus subscription endpoint.")
@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.
# You will need to change the namespace to your own.
Expand All @@ -300,6 +301,27 @@ def test_insert_table_json_success(self):
InsertTableResult(rows_written=1),
)

@unittest.skip("Requires custom namespace and table_name input.")
def test_delete_table_success(self):
# Make sure the table doesn't already exist.
# You will need to change the namespace to your own.
client = DuneClient(self.valid_api_key)

namespace = "test"
table_name = "dataset_e2e_test"

self.assertEqual(
client.delete_table(
namespace=namespace,
table_name=table_name,
),
DeleteTableResult.from_dict(
{
"message": "Table teamwaddah.waddah_test3 successfully deleted",
}
),
)

def test_download_csv_with_pagination(self):
# Arrange
client = DuneClient(self.valid_api_key)
Expand Down

0 comments on commit 22a2659

Please sign in to comment.