From 02712d59bbb07d56f11fe8bd6b67fc1f1b75c683 Mon Sep 17 00:00:00 2001 From: Alex Courouble <56831527+couralex6@users.noreply.github.com> Date: Fri, 17 May 2024 16:06:19 -0700 Subject: [PATCH 1/5] Handle insert errors --- dune_client/api/table.py | 9 +++++++-- dune_client/models.py | 9 +++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/dune_client/api/table.py b/dune_client/api/table.py index 822735f..90e5e04 100644 --- a/dune_client/api/table.py +++ b/dune_client/api/table.py @@ -10,6 +10,7 @@ from dune_client.models import ( DuneError, InsertTableResult, + InsertTableErrorResult, CreateTableResult, DeleteTableResult, ) @@ -88,7 +89,7 @@ def insert_table( table_name: str, data: IO[bytes], content_type: str, - ) -> InsertTableResult: + ) -> InsertTableResult | InsertTableErrorResult: """ https://docs.dune.com/api-reference/tables/endpoint/insert The insert table endpoint allows you to insert data into an existing table in Dune. @@ -103,7 +104,11 @@ def insert_table( headers={"Content-Type": content_type}, data=data, ) - return InsertTableResult.from_dict(result_json) + if "error" in result_json: + return InsertTableErrorResult.from_dict(result_json) + else: + return InsertTableResult.from_dict(result_json) + def delete_table(self, namespace: str, table_name: str) -> DeleteTableResult: """ diff --git a/dune_client/models.py b/dune_client/models.py index 2389960..a44fed5 100644 --- a/dune_client/models.py +++ b/dune_client/models.py @@ -375,6 +375,15 @@ class InsertTableResult(DataClassJsonMixin): """ rows_written: int + bytes_written: int + +@dataclass +class InsertTableErrorResult(DataClassJsonMixin): + """ + Data type returned by table/insert operation error + """ + + error: str @dataclass From 886b7962e4953f2a6a33acd0fdb0245143131dab Mon Sep 17 00:00:00 2001 From: Alex Courouble <56831527+couralex6@users.noreply.github.com> Date: Fri, 24 May 2024 11:07:11 -0700 Subject: [PATCH 2/5] raise DuneError if insert failed --- dune_client/api/table.py | 9 +++++---- dune_client/models.py | 8 -------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/dune_client/api/table.py b/dune_client/api/table.py index 90e5e04..28374e8 100644 --- a/dune_client/api/table.py +++ b/dune_client/api/table.py @@ -104,11 +104,12 @@ def insert_table( headers={"Content-Type": content_type}, data=data, ) - if "error" in result_json: - return InsertTableErrorResult.from_dict(result_json) - else: - return InsertTableResult.from_dict(result_json) + # return InsertTableErrorResult.from_dict(result_json) + try: + return InsertTableResult.from_dict(result_json) + except KeyError as err: + raise DuneError(result_json, "ResultsResponse", err) from err def delete_table(self, namespace: str, table_name: str) -> DeleteTableResult: """ diff --git a/dune_client/models.py b/dune_client/models.py index a44fed5..270288f 100644 --- a/dune_client/models.py +++ b/dune_client/models.py @@ -377,14 +377,6 @@ class InsertTableResult(DataClassJsonMixin): rows_written: int bytes_written: int -@dataclass -class InsertTableErrorResult(DataClassJsonMixin): - """ - Data type returned by table/insert operation error - """ - - error: str - @dataclass class DeleteTableResult(DataClassJsonMixin): From f1d96182d14c7439e0306261a20791c6ee344195 Mon Sep 17 00:00:00 2001 From: Alex Courouble <56831527+couralex6@users.noreply.github.com> Date: Fri, 24 May 2024 11:10:10 -0700 Subject: [PATCH 3/5] removed unused import --- dune_client/api/table.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dune_client/api/table.py b/dune_client/api/table.py index 28374e8..4b93802 100644 --- a/dune_client/api/table.py +++ b/dune_client/api/table.py @@ -10,7 +10,6 @@ from dune_client.models import ( DuneError, InsertTableResult, - InsertTableErrorResult, CreateTableResult, DeleteTableResult, ) From a2c0fad5c6433fd6ad8f523ab43d509e70a9de1a Mon Sep 17 00:00:00 2001 From: Alex Courouble <56831527+couralex6@users.noreply.github.com> Date: Fri, 24 May 2024 11:13:29 -0700 Subject: [PATCH 4/5] fix format --- dune_client/api/table.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dune_client/api/table.py b/dune_client/api/table.py index 4b93802..0e5ac48 100644 --- a/dune_client/api/table.py +++ b/dune_client/api/table.py @@ -88,7 +88,7 @@ def insert_table( table_name: str, data: IO[bytes], content_type: str, - ) -> InsertTableResult | InsertTableErrorResult: + ) -> InsertTableResult: """ https://docs.dune.com/api-reference/tables/endpoint/insert The insert table endpoint allows you to insert data into an existing table in Dune. @@ -104,7 +104,6 @@ def insert_table( data=data, ) - # return InsertTableErrorResult.from_dict(result_json) try: return InsertTableResult.from_dict(result_json) except KeyError as err: From 1d186afaf70b66402575bddff34fd0588df4e118 Mon Sep 17 00:00:00 2001 From: Alex Courouble <56831527+couralex6@users.noreply.github.com> Date: Fri, 24 May 2024 11:17:06 -0700 Subject: [PATCH 5/5] format test --- dune_client/api/table.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dune_client/api/table.py b/dune_client/api/table.py index 0e5ac48..239d0fc 100644 --- a/dune_client/api/table.py +++ b/dune_client/api/table.py @@ -103,7 +103,6 @@ def insert_table( headers={"Content-Type": content_type}, data=data, ) - try: return InsertTableResult.from_dict(result_json) except KeyError as err: