Skip to content

Commit e2a9488

Browse files
committed
Include Request Timeout in Dune Source
1 parent ef40896 commit e2a9488

File tree

4 files changed

+38
-29
lines changed

4 files changed

+38
-29
lines changed

src/config.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
import requests
1414
import yaml
1515
from dotenv import load_dotenv
16-
from dune_client.query import QueryBase
1716

1817
from src.destinations.dune import DuneDestination
1918
from src.destinations.postgres import PostgresDestination
2019
from src.interfaces import Destination, Source
2120
from src.job import Database, Job
2221
from src.logger import log
23-
from src.sources.dune import DuneSource, parse_query_parameters
22+
from src.sources.dune import DuneSource, DuneSourceConfig
2423
from src.sources.postgres import PostgresSource
2524

2625

@@ -259,15 +258,7 @@ def _build_source(
259258
match source.type:
260259
case Database.DUNE:
261260
return DuneSource(
262-
api_key=source.key,
263-
query=QueryBase(
264-
query_id=int(source_config["query_id"]),
265-
params=parse_query_parameters(
266-
source_config.get("parameters", [])
267-
),
268-
),
269-
poll_frequency=source_config.get("poll_frequency", 1),
270-
query_engine=source_config.get("query_engine", "medium"),
261+
dune_config=DuneSourceConfig(source.key, source_config)
271262
)
272263

273264
case Database.POSTGRES:

src/destinations/dune.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,11 @@ class DuneDestination(Destination[TypedDataFrame]):
1414
1515
Attributes
1616
----------
17-
api_key : str
18-
The API key used for accessing the Dune Analytics API.
17+
client : DuneClient
18+
The API for accessing the Dune Analytics.
1919
table_name : str
2020
The name of the table where the query results will be stored.
2121
22-
[optional]
23-
request_timeout : int
24-
Default: 10
25-
The request timeout for the dune client.
26-
2722
"""
2823

2924
def __init__(self, api_key: str, table_name: str, request_timeout: int):

src/sources/dune.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
import re
77
from abc import ABC
8+
from dataclasses import dataclass
89
from typing import Any, Literal
910

1011
import pandas as pd
@@ -42,6 +43,28 @@
4243
}
4344

4445

46+
@dataclass
47+
class DuneSourceConfig:
48+
"""Represents Parameters used by Dune Client and Query execution constructors."""
49+
50+
api_key: str
51+
query: QueryBase
52+
request_timeout: int
53+
poll_frequency: int
54+
query_engine: Literal["medium", "large"]
55+
56+
def __init__(self, api_key: str, data: dict[str, Any]):
57+
self.api_key = api_key
58+
if data["query_id"] is not None:
59+
self.query = QueryBase(
60+
query_id=int(data["query_id"]),
61+
params=parse_query_parameters(data.get("parameters", [])),
62+
)
63+
self.request_timeout = int(data.get("request_timeout", 10))
64+
self.poll_frequency = int(data.get("poll_frequency", 1))
65+
self.query_engine = data.get("query_engine", "medium")
66+
67+
4568
def _parse_varchar_type(type_str: str) -> int | None:
4669
"""Extract the length from Dune's varchar type string like varchar(255).
4770
@@ -240,14 +263,15 @@ class DuneSource(Source[TypedDataFrame], ABC):
240263

241264
def __init__(
242265
self,
243-
api_key: str,
244-
query: QueryBase,
245-
poll_frequency: int = 1,
246-
query_engine: Literal["medium", "large"] = "medium",
266+
dune_config: DuneSourceConfig,
247267
) -> None:
248-
self.query = query
249-
self.poll_frequency = poll_frequency
250-
self.client = AsyncDuneClient(api_key, performance=query_engine)
268+
self.query = dune_config.query
269+
self.poll_frequency = dune_config.poll_frequency
270+
self.client = AsyncDuneClient(
271+
dune_config.api_key,
272+
performance=dune_config.query_engine,
273+
request_timeout=dune_config.request_timeout,
274+
)
251275
super().__init__()
252276

253277
def validate(self) -> bool:

tests/unit/job_test.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
from unittest.mock import AsyncMock, Mock, patch
33

44
import pytest
5-
from dune_client.query import QueryBase
65

76
from src.destinations.postgres import PostgresDestination
87
from src.job import Database, Job
9-
from src.sources.dune import DuneSource
8+
from src.sources.dune import DuneSource, DuneSourceConfig
109

1110

1211
class DatabaseTests(unittest.IsolatedAsyncioTestCase):
@@ -20,7 +19,7 @@ def test_database_resolution(self):
2019
self.assertEqual("Unknown Database type: redis", exc.exception.args[0])
2120

2221
def test_job_name_formatting(self):
23-
src = DuneSource(api_key="f00b4r", query=QueryBase(query_id=1234))
22+
src = DuneSource(DuneSourceConfig(api_key="f00b4r", data={"query_id": 1234}))
2423
dest = PostgresDestination(
2524
db_url="postgresql://postgres:postgres@localhost:5432/postgres",
2625
table_name="some_table",
@@ -31,7 +30,7 @@ def test_job_name_formatting(self):
3130
@pytest.mark.asyncio
3231
@patch("src.metrics.push_to_gateway")
3332
async def test_metrics_collection(self, mock_metrics_push, *_):
34-
src = DuneSource(api_key="f00b4r", query=QueryBase(query_id=1234))
33+
src = DuneSource(DuneSourceConfig(api_key="f00b4r", data={"query_id": 1234}))
3534
dest = PostgresDestination(
3635
db_url="postgresql://postgres:postgres@localhost:5432/postgres",
3736
table_name="some_table",

0 commit comments

Comments
 (0)