Skip to content

Commit 775549b

Browse files
Philipp Wassibauerbh2smith
andauthored
Reading custom endpoint results SDK support (#132)
* reading custom endpoint results works locally. * Update dune_client/api/custom.py Co-authored-by: Benjamin Smith <bh2smith@users.noreply.github.com> * add docstrings * fix to get pylint to play * add basic integration testing for custom endpoints * fix * test naming fix * fix pyling * linted --------- Co-authored-by: Benjamin Smith <bh2smith@users.noreply.github.com>
1 parent ed3836d commit 775549b

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

dune_client/api/custom.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
Custom endpoints API enables users to
3+
fetch and filter data from custom endpoints.
4+
"""
5+
6+
from __future__ import annotations
7+
from typing import List, Optional
8+
9+
from dune_client.api.base import BaseRouter
10+
from dune_client.models import (
11+
DuneError,
12+
ResultsResponse,
13+
)
14+
15+
16+
# pylint: disable=duplicate-code
17+
class CustomEndpointAPI(BaseRouter):
18+
"""
19+
Custom endpoints API implementation.
20+
Methods:
21+
get_custom_endpoint_result(): returns the results of a custom endpoint.
22+
"""
23+
24+
def get_custom_endpoint_result(
25+
self,
26+
handle: str,
27+
endpoint: str,
28+
limit: Optional[int] = None,
29+
offset: Optional[int] = None,
30+
columns: Optional[List[str]] = None,
31+
sample_count: Optional[int] = None,
32+
filters: Optional[str] = None,
33+
sort_by: Optional[List[str]] = None,
34+
) -> ResultsResponse:
35+
"""
36+
Custom endpoints allow you to fetch and filter data from any
37+
custom endpoint you created.
38+
More information on Custom Endpoints can be round here:
39+
https://docs.dune.com/api-reference/custom/overview
40+
41+
Args:
42+
handle (str): The handle of the team/user.
43+
endpoint (str): The slug of the custom endpoint.
44+
limit (int, optional): The maximum number of results to return.
45+
offset (int, optional): The number of results to skip.
46+
columns (List[str], optional): A list of columns to return.
47+
sample_count (int, optional): The number of results to return.
48+
filters (str, optional): The filters to apply.
49+
sort_by (List[str], optional): The columns to sort by.
50+
"""
51+
params = self._build_parameters(
52+
columns=columns,
53+
sample_count=sample_count,
54+
filters=filters,
55+
sort_by=sort_by,
56+
limit=limit,
57+
offset=offset,
58+
)
59+
response_json = self._get(
60+
route=f"/endpoints/{handle}/{endpoint}/results",
61+
params=params,
62+
)
63+
try:
64+
return ResultsResponse.from_dict(response_json)
65+
except KeyError as err:
66+
raise DuneError(response_json, "ResultsResponse", err) from err

dune_client/api/extensions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from dune_client.api.execution import ExecutionAPI
2121
from dune_client.api.query import QueryAPI
2222
from dune_client.api.table import TableAPI
23+
from dune_client.api.custom import CustomEndpointAPI
2324
from dune_client.models import (
2425
ResultsResponse,
2526
DuneError,
@@ -37,7 +38,7 @@
3738
POLL_FREQUENCY_SECONDS = 1
3839

3940

40-
class ExtendedAPI(ExecutionAPI, QueryAPI, TableAPI):
41+
class ExtendedAPI(ExecutionAPI, QueryAPI, TableAPI, CustomEndpointAPI):
4142
"""
4243
Provides higher level helper methods for faster
4344
and easier development on top of the base ExecutionAPI.

tests/e2e/test_custom_endpoints.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import copy
2+
import os
3+
import time
4+
import unittest
5+
6+
import dotenv
7+
8+
from dune_client.client import DuneClient
9+
10+
dotenv.load_dotenv()
11+
12+
13+
class TestCustomEndpoints(unittest.TestCase):
14+
def setUp(self) -> None:
15+
self.valid_api_key = os.environ["DUNE_API_KEY"]
16+
17+
def test_gettin_custom_endpoint_results(self):
18+
dune = DuneClient(self.valid_api_key)
19+
results = dune.get_custom_endpoint_result("dune", "new-test")
20+
self.assertEqual(len(results.get_rows()), 10)
21+
22+
23+
if __name__ == "__main__":
24+
unittest.main()

0 commit comments

Comments
 (0)