Skip to content

Commit 4c2c256

Browse files
authored
Add upload_csv route (#61)
* add upload/csv route * add better response handling
1 parent 1f4dd11 commit 4c2c256

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

dune_client/base_client.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from typing import Dict
1010

1111

12-
# pylint: disable=too-few-public-methods
1312
class BaseDuneClient:
1413
"""
1514
A Base Client for Dune which sets up default values

dune_client/client.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,26 @@ def make_public(self, query_id: int) -> None:
374374
"""
375375
response_json = self._post(route=f"/query/{query_id}/unprivate")
376376
assert not self.get_query(int(response_json["query_id"])).meta.is_private
377+
378+
def upload_csv(self, table_name: str, data: str, description: str = "") -> bool:
379+
"""
380+
https://dune.com/docs/api/api-reference/upload-data/?h=data+upload#endpoint
381+
The write API allows you to upload any .csv file into Dune. The only limitations are:
382+
383+
- File has to be < 200 MB
384+
- Column names in the table can't start with a special character or digits.
385+
386+
Below are the specifics of how to work with the API.
387+
"""
388+
response_json = self._post(
389+
route="/table/upload/csv",
390+
params={
391+
"table_name": table_name,
392+
"description": description,
393+
"data": data,
394+
},
395+
)
396+
try:
397+
return bool(response_json["success"])
398+
except KeyError as err:
399+
raise DuneError(response_json, "upload_csv response", err) from err

tests/e2e/test_client.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ def test_internal_error(self):
156156

157157
def test_invalid_job_id_error(self):
158158
dune = DuneClient(self.valid_api_key)
159-
160159
with self.assertRaises(DuneError) as err:
161160
dune.get_status("Wonky Job ID")
162161
self.assertEqual(
@@ -175,6 +174,17 @@ def test_get_latest_result_with_query_id(self):
175174
results = dune.get_latest_result(self.query.query_id).get_rows()
176175
self.assertGreater(len(results), 0)
177176

177+
def test_upload_csv_success(self):
178+
client = DuneClient(self.valid_api_key)
179+
self.assertEqual(
180+
client.upload_csv(
181+
table_name="e2e-test",
182+
description="best data",
183+
data="column1,column2\nvalue1,value2\nvalue3,value4",
184+
),
185+
True,
186+
)
187+
178188

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

0 commit comments

Comments
 (0)