-
Notifications
You must be signed in to change notification settings - Fork 25
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
Feature: Download CSV #73
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
""" | ||
|
||
import time | ||
from io import BytesIO | ||
from typing import Union, Optional, Any | ||
|
||
from deprecated import deprecated | ||
|
@@ -15,7 +16,7 @@ | |
QueryFailed, | ||
ExecutionResultCSV, | ||
) | ||
from dune_client.query import QueryBase | ||
from dune_client.query import QueryBase, parse_query_object_or_id | ||
|
||
|
||
class ExtendedAPI(ExecutionAPI): | ||
|
@@ -78,19 +79,11 @@ def get_latest_result(self, query: Union[QueryBase, str, int]) -> ResultsRespons | |
GET the latest results for a query_id without re-executing the query | ||
(doesn't use execution credits) | ||
|
||
:param query: :class:`Query` object OR query id as string | int | ||
:param query: :class:`Query` object OR query id as string or int | ||
|
||
https://dune.com/docs/api/api-reference/latest_results/ | ||
https://dune.com/docs/api/api-reference/get-results/latest-results | ||
Comment on lines
-83
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. URL has changed since last time. |
||
""" | ||
if isinstance(query, QueryBase): | ||
params = { | ||
f"params.{p.key}": p.to_dict()["value"] for p in query.parameters() | ||
} | ||
query_id = query.query_id | ||
else: | ||
params = None | ||
query_id = int(query) | ||
|
||
params, query_id = parse_query_object_or_id(query) | ||
response_json = self._get( | ||
route=f"/query/{query_id}/results", | ||
params=params, | ||
|
@@ -100,6 +93,21 @@ def get_latest_result(self, query: Union[QueryBase, str, int]) -> ResultsRespons | |
except KeyError as err: | ||
raise DuneError(response_json, "ResultsResponse", err) from err | ||
|
||
def download_csv(self, query: Union[QueryBase, str, int]) -> ExecutionResultCSV: | ||
""" | ||
Almost like an alias for `get_latest_result` but for the csv endpoint. | ||
https://dune.com/docs/api/api-reference/get-results/latest-results | ||
""" | ||
params, query_id = parse_query_object_or_id(query) | ||
response = self._get( | ||
route=f"/query/{query_id}/results/csv", params=params, raw=True | ||
) | ||
response.raise_for_status() | ||
return ExecutionResultCSV(data=BytesIO(response.content)) | ||
|
||
############################ | ||
# Plus Subscription Features | ||
############################ | ||
def upload_csv(self, table_name: str, data: str, description: str = "") -> bool: | ||
""" | ||
https://dune.com/docs/api/api-reference/upload-data/?h=data+upload#endpoint | ||
|
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 |
---|---|---|
|
@@ -183,7 +183,7 @@ class ExecutionResultCSV: | |
Representation of a raw `result` in CSV format | ||
this payload can be passed directly to | ||
csv.reader(data) or | ||
pandas.from_csv(data) | ||
pandas.read_csv(data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a typo. |
||
""" | ||
|
||
data: BytesIO # includes all CSV rows, including the header row. | ||
|
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was unnecessary because the _get method does this same debug logging.