Skip to content

Commit

Permalink
Add upload_csv route (#61)
Browse files Browse the repository at this point in the history
* add upload/csv route
* add better response handling
  • Loading branch information
bh2smith authored Sep 6, 2023
1 parent 1f4dd11 commit 4c2c256
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
1 change: 0 additions & 1 deletion dune_client/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from typing import Dict


# pylint: disable=too-few-public-methods
class BaseDuneClient:
"""
A Base Client for Dune which sets up default values
Expand Down
23 changes: 23 additions & 0 deletions dune_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,26 @@ def make_public(self, query_id: int) -> None:
"""
response_json = self._post(route=f"/query/{query_id}/unprivate")
assert not self.get_query(int(response_json["query_id"])).meta.is_private

def upload_csv(self, table_name: str, data: str, description: str = "") -> bool:
"""
https://dune.com/docs/api/api-reference/upload-data/?h=data+upload#endpoint
The write API allows you to upload any .csv file into Dune. The only limitations are:
- File has to be < 200 MB
- Column names in the table can't start with a special character or digits.
Below are the specifics of how to work with the API.
"""
response_json = self._post(
route="/table/upload/csv",
params={
"table_name": table_name,
"description": description,
"data": data,
},
)
try:
return bool(response_json["success"])
except KeyError as err:
raise DuneError(response_json, "upload_csv response", err) from err
12 changes: 11 additions & 1 deletion tests/e2e/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ def test_internal_error(self):

def test_invalid_job_id_error(self):
dune = DuneClient(self.valid_api_key)

with self.assertRaises(DuneError) as err:
dune.get_status("Wonky Job ID")
self.assertEqual(
Expand All @@ -175,6 +174,17 @@ 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)

def test_upload_csv_success(self):
client = DuneClient(self.valid_api_key)
self.assertEqual(
client.upload_csv(
table_name="e2e-test",
description="best data",
data="column1,column2\nvalue1,value2\nvalue3,value4",
),
True,
)


@unittest.skip("This is an enterprise only endpoint that can no longer be tested.")
class TestCRUDOps(unittest.TestCase):
Expand Down

0 comments on commit 4c2c256

Please sign in to comment.