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

Reading custom endpoint results SDK support #132

Merged
merged 10 commits into from
Jun 20, 2024
65 changes: 65 additions & 0 deletions dune_client/api/custom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is likely because of all the filter arguments and suggests a deeper change to the project. Introduce a ResultFilter structure that contains all the filtering possibilities:

@dataclass
class ResultFilter:
    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

Then here would look more like:

    def get_custom_endpoint_result(
        self,
        handle: str,
        endpoint: str,
        filter: Optional[ResultFilter]
    ) -> ResultsResponse:

It would resolve both duplicated code lint warning and also reduce the "argument overload" lint error that is being globally suppressed here

max-args=10

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will log this and do at another time.

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

philippWassibauer marked this conversation as resolved.
Show resolved Hide resolved
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
3 changes: 2 additions & 1 deletion dune_client/api/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from dune_client.api.execution import ExecutionAPI
from dune_client.api.query import QueryAPI
from dune_client.api.table import TableAPI
from dune_client.api.custom import CustomEndpointAPI
from dune_client.models import (
ResultsResponse,
DuneError,
Expand All @@ -37,7 +38,7 @@
POLL_FREQUENCY_SECONDS = 1


class ExtendedAPI(ExecutionAPI, QueryAPI, TableAPI):
class ExtendedAPI(ExecutionAPI, QueryAPI, TableAPI, CustomEndpointAPI):
philippWassibauer marked this conversation as resolved.
Show resolved Hide resolved
"""
Provides higher level helper methods for faster
and easier development on top of the base ExecutionAPI.
Expand Down
23 changes: 23 additions & 0 deletions tests/e2e/test_custom_endpoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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()
Loading