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

Too many positional arguments #139

Open
wants to merge 9 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dist
_version.py
.idea/
venv/
.venv/
tmp/
.vscode/
build/
Expand Down
3 changes: 2 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[MASTER]
disable=fixme,logging-fstring-interpolation,too-many-positional-arguments
disable=fixme,logging-fstring-interpolation
[DESIGN]
max-args=10
max-attributes=7
49 changes: 2 additions & 47 deletions dune_client/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import logging.config
import os
from json import JSONDecodeError
from typing import Any, Dict, List, Optional, Union, IO
from typing import Any, Dict, Optional, IO

from requests import Response, Session
from requests.adapters import HTTPAdapter, Retry
Expand All @@ -30,7 +30,7 @@ class BaseDuneClient:
and provides some convenient functions to use in other clients
"""

def __init__( # pylint: disable=too-many-arguments
def __init__( # pylint: disable=too-many-arguments, too-many-positional-arguments
self,
api_key: str,
base_url: str = "https://api.dune.com",
Expand Down Expand Up @@ -83,51 +83,6 @@ def default_headers(self) -> Dict[str, str]:
"User-Agent": f"dune-client/{client_version} (https://pypi.org/project/dune-client/)",
}

############
# Utilities:
############

def _build_parameters(
self,
params: Optional[Dict[str, Union[str, int]]] = None,
columns: Optional[List[str]] = None,
sample_count: Optional[int] = None,
filters: Optional[str] = None,
sort_by: Optional[List[str]] = None,
limit: Optional[int] = None,
offset: Optional[int] = None,
allow_partial_results: str = "true",
) -> Dict[str, Union[str, int]]:
"""
Utility function that builds a dictionary of parameters to be used
when retrieving advanced results (filters, pagination, sorting, etc.).
This is shared between the sync and async client.
"""
# Ensure we don't specify parameters that are incompatible:
assert (
# We are not sampling
sample_count is None
# We are sampling and don't use filters or pagination
or (limit is None and offset is None and filters is None)
), "sampling cannot be combined with filters or pagination"

params = params or {}
params["allow_partial_results"] = allow_partial_results
if columns is not None and len(columns) > 0:
params["columns"] = ",".join(columns)
if sample_count is not None:
params["sample_count"] = sample_count
if filters is not None:
params["filters"] = filters
if sort_by is not None and len(sort_by) > 0:
params["sort_by"] = ",".join(sort_by)
if limit is not None:
params["limit"] = limit
if offset is not None:
params["offset"] = offset

return params


class BaseRouter(BaseDuneClient):
"""Extending the Base Client with elementary api routing"""
Expand Down
35 changes: 19 additions & 16 deletions dune_client/api/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

from __future__ import annotations
from typing import List, Optional
from typing import List, NamedTuple, Optional

from dune_client.api.base import BaseRouter
from dune_client.models import (
Expand All @@ -13,6 +13,19 @@
)


class CustomAPIParams(NamedTuple):
"""
Params for Custom Endpoint API Function
"""

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


# pylint: disable=duplicate-code
class CustomEndpointAPI(BaseRouter):
"""
Expand All @@ -25,12 +38,7 @@ 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,
params: Optional[CustomAPIParams] = None,
) -> ResultsResponse:
"""
Custom endpoints allow you to fetch and filter data from any
Expand All @@ -48,17 +56,12 @@ def get_custom_endpoint_result(
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,
)
if params is None:
params = CustomAPIParams()

response_json = self._get(
route=f"/endpoints/{handle}/{endpoint}/results",
params=params,
params=params._asdict(),
)
try:
return ResultsResponse.from_dict(response_json)
Expand Down
61 changes: 26 additions & 35 deletions dune_client/api/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

from io import BytesIO
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, NamedTuple, Optional

from deprecated import deprecated

Expand All @@ -27,6 +27,21 @@
from dune_client.query import QueryBase


class GetExecutionResultsParams(NamedTuple):
"""
Parameters for get execution result functions
"""

limit: Optional[int] = None
columns: Optional[List[str]] = None
batch_size: Optional[int] = None
sample_count: Optional[int] = None
filters: Optional[str] = None
sort_by: Optional[List[str]] = None
offset: Optional[int] = None
allow_partial_results: str = "true"


class ExecutionAPI(BaseRouter):
"""
Query execution and result fetching functions.
Expand Down Expand Up @@ -75,38 +90,19 @@ def get_execution_status(self, job_id: str) -> ExecutionStatusResponse:
def get_execution_results(
self,
job_id: 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,
allow_partial_results: str = "true",
params: Optional[GetExecutionResultsParams] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hey, ya you did it here.

) -> ResultsResponse:
"""GET results from Dune API for `job_id` (aka `execution_id`)"""
params = self._build_parameters(
columns=columns,
sample_count=sample_count,
filters=filters,
sort_by=sort_by,
limit=limit,
offset=offset,
allow_partial_results=allow_partial_results,
)

if params is None:
params = GetExecutionResultsParams()

route = f"/execution/{job_id}/results"
url = self._route_url(route)
return self._get_execution_results_by_url(url=url, params=params)
return self._get_execution_results_by_url(url=url, params=params._asdict())

def get_execution_results_csv(
self,
job_id: str,
limit: Optional[int] = None,
offset: Optional[int] = None,
columns: Optional[List[str]] = None,
filters: Optional[str] = None,
sample_count: Optional[int] = None,
sort_by: Optional[List[str]] = None,
self, job_id: str, params: Optional[GetExecutionResultsParams] = None
) -> ExecutionResultCSV:
"""
GET results in CSV format from Dune API for `job_id` (aka `execution_id`)
Expand All @@ -115,18 +111,13 @@ def get_execution_results_csv(
use this method for large results where you want lower CPU and memory overhead
if you need metadata information use get_results() or get_status()
"""
params = self._build_parameters(
columns=columns,
sample_count=sample_count,
filters=filters,
sort_by=sort_by,
limit=limit,
offset=offset,
)

if params is None:
params = GetExecutionResultsParams()

route = f"/execution/{job_id}/results/csv"
url = self._route_url(route)
return self._get_execution_results_csv_by_url(url=url, params=params)
return self._get_execution_results_csv_by_url(url=url, params=params._asdict())

def _get_execution_results_by_url(
self, url: str, params: Optional[Dict[str, Any]] = None
Expand Down
Loading