Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return Types on Create and Insert Table #117

Merged
merged 5 commits into from
Mar 25, 2024
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
14 changes: 8 additions & 6 deletions dune_client/api/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"""

from __future__ import annotations
from typing import List, Dict, Any, IO
from typing import List, Dict, IO

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


class TableAPI(BaseRouter):
Expand Down Expand Up @@ -54,7 +54,7 @@ def create_table(
schema: List[Dict[str, str]],
description: str = "",
is_private: bool = False,
) -> Any:
) -> CreateTableResult:
"""
https://docs.dune.com/api-reference/tables/endpoint/create
The create table endpoint allows you to create an empty table
Expand All @@ -65,7 +65,7 @@ def create_table(
- Column names in the table can’t start with a special character or a digit.
"""

return self._post(
result_json = self._post(
route="/table/create",
params={
"namespace": namespace,
Expand All @@ -75,14 +75,15 @@ def create_table(
"is_private": is_private,
},
)
return CreateTableResult.from_dict(result_json)

def insert_table(
self,
namespace: str,
table_name: str,
data: IO[bytes],
content_type: str,
) -> Any:
) -> 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.
Expand All @@ -92,8 +93,9 @@ def insert_table(
- The file has to have the same schema as the table
"""

return self._post(
result_json = self._post(
route=f"/table/{namespace}/{table_name}/insert",
headers={"Content-Type": content_type},
data=data,
)
return InsertTableResult.from_dict(result_json)
23 changes: 22 additions & 1 deletion dune_client/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from io import BytesIO
from os import SEEK_END
from typing import Optional, Any, Union, List, Dict

from dataclasses_json import DataClassJsonMixin
from dateutil.parser import parse

from dune_client.types import DuneRecord
Expand Down Expand Up @@ -354,3 +354,24 @@ def __add__(self, other: ResultsResponse) -> ResultsResponse:
self.next_uri = other.next_uri
self.next_offset = other.next_offset
return self


@dataclass
class CreateTableResult(DataClassJsonMixin):
"""
Data type returned by table/create operation
"""

example_query: str
full_name: str
namespace: str
table_name: str


@dataclass
class InsertTableResult(DataClassJsonMixin):
bh2smith marked this conversation as resolved.
Show resolved Hide resolved
"""
Data type returned by table/insert operation
"""

rows_written: int
1 change: 1 addition & 0 deletions requirements/prod.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
aiohttp>=3.8.5
dataclasses-json==0.6.4
types-python-dateutil>=2.8.19.14
types-PyYAML>=6.0.12.11
types-requests>=2.28.0
Expand Down
26 changes: 15 additions & 11 deletions tests/e2e/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
ExecutionResponse,
ExecutionStatusResponse,
DuneError,
InsertTableResult,
CreateTableResult,
)
from dune_client.types import QueryParameter
from dune_client.client import DuneClient
Expand Down Expand Up @@ -125,7 +127,7 @@ def test_parameters_recognized(self):
{
"text_field": "different word",
"number_field": 22,
"date_field": "1991-01-01T00:00:00Z",
"date_field": "1991-01-01 00:00:00.000",
"list_field": "Option 2",
}
],
Expand Down Expand Up @@ -256,12 +258,14 @@ def test_create_table_success(self):
],
is_private=False,
),
{
"namespace": namespace,
"table_name": table_name,
"full_name": f"dune.{namespace}.{table_name}",
"example_query": f"select * from dune.{namespace}.{table_name} limit 10",
},
CreateTableResult.from_dict(
{
"namespace": namespace,
"table_name": table_name,
"full_name": f"dune.{namespace}.{table_name}",
"example_query": f"select * from dune.{namespace}.{table_name} limit 10",
}
),
)

@unittest.skip("This is a plus subscription endpoint.")
Expand All @@ -277,7 +281,7 @@ def test_insert_table_csv_success(self):
data=data,
content_type="text/csv",
),
{"rows_written": 1},
InsertTableResult(rows_written=1),
)

@unittest.skip("This is a plus subscription endpoint.")
Expand All @@ -293,7 +297,7 @@ def test_insert_table_json_success(self):
data=data,
content_type="application/x-ndjson",
),
{"rows_written": 1},
InsertTableResult(rows_written=1),
)

def test_download_csv_with_pagination(self):
Expand Down Expand Up @@ -350,7 +354,7 @@ def test_download_csv_success_by_id(self):
{
"text_field": "different word",
"number_field": 22,
"date_field": "1991-01-01T00:00:00Z",
"date_field": "1991-01-01 00:00:00.000",
"list_field": "Option 2",
}
],
Expand All @@ -372,7 +376,7 @@ def test_download_csv_success_with_params(self):
pandas.read_csv(result_csv.data).to_dict(orient="records"),
[
{
"date_field": "2022-05-04T00:00:00Z",
"date_field": "2022-05-04 00:00:00.000",
"list_field": "Option 1",
"number_field": 3.1415926535,
"text_field": "Plain Text",
Expand Down
Loading