-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reading custom endpoint results SDK support (#132)
* reading custom endpoint results works locally. * Update dune_client/api/custom.py Co-authored-by: Benjamin Smith <[email protected]> * 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 <[email protected]>
- Loading branch information
Showing
3 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
""" | ||
Custom endpoints API enables users to | ||
fetch and filter data from custom endpoints. | ||
""" | ||
|
||
from __future__ import annotations | ||
from typing import List, Optional | ||
|
||
from dune_client.api.base import BaseRouter | ||
from dune_client.models import ( | ||
DuneError, | ||
ResultsResponse, | ||
) | ||
|
||
|
||
# pylint: disable=duplicate-code | ||
class CustomEndpointAPI(BaseRouter): | ||
""" | ||
Custom endpoints API implementation. | ||
Methods: | ||
get_custom_endpoint_result(): returns the results of a custom endpoint. | ||
""" | ||
|
||
def get_custom_endpoint_result( | ||
self, | ||
handle: str, | ||
endpoint: str, | ||
limit: Optional[int] = None, | ||
offset: Optional[int] = None, | ||
columns: Optional[List[str]] = None, | ||
sample_count: Optional[int] = None, | ||
filters: Optional[str] = None, | ||
sort_by: Optional[List[str]] = None, | ||
) -> ResultsResponse: | ||
""" | ||
Custom endpoints allow you to fetch and filter data from any | ||
custom endpoint you created. | ||
More information on Custom Endpoints can be round here: | ||
https://docs.dune.com/api-reference/custom/overview | ||
Args: | ||
handle (str): The handle of the team/user. | ||
endpoint (str): The slug of the custom endpoint. | ||
limit (int, optional): The maximum number of results to return. | ||
offset (int, optional): The number of results to skip. | ||
columns (List[str], optional): A list of columns to return. | ||
sample_count (int, optional): The number of results to return. | ||
filters (str, optional): The filters to apply. | ||
sort_by (List[str], optional): The columns to sort by. | ||
""" | ||
params = self._build_parameters( | ||
columns=columns, | ||
sample_count=sample_count, | ||
filters=filters, | ||
sort_by=sort_by, | ||
limit=limit, | ||
offset=offset, | ||
) | ||
response_json = self._get( | ||
route=f"/endpoints/{handle}/{endpoint}/results", | ||
params=params, | ||
) | ||
try: | ||
return ResultsResponse.from_dict(response_json) | ||
except KeyError as err: | ||
raise DuneError(response_json, "ResultsResponse", err) from err |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import copy | ||
import os | ||
import time | ||
import unittest | ||
|
||
import dotenv | ||
|
||
from dune_client.client import DuneClient | ||
|
||
dotenv.load_dotenv() | ||
|
||
|
||
class TestCustomEndpoints(unittest.TestCase): | ||
def setUp(self) -> None: | ||
self.valid_api_key = os.environ["DUNE_API_KEY"] | ||
|
||
def test_gettin_custom_endpoint_results(self): | ||
dune = DuneClient(self.valid_api_key) | ||
results = dune.get_custom_endpoint_result("dune", "new-test") | ||
self.assertEqual(len(results.get_rows()), 10) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |