File tree Expand file tree Collapse file tree 4 files changed +20
-8
lines changed
Expand file tree Collapse file tree 4 files changed +20
-8
lines changed Original file line number Diff line number Diff line change @@ -17,9 +17,6 @@ Export your `DUNE_API_KEY` (or place it in a `.env` file - as in
1717here [ .env.sample] ( ./.env.sample ) .
1818
1919``` python
20- import dotenv
21- import os
22-
2320from dune_client.types import QueryParameter
2421from dune_client.client import DuneClient
2522from dune_client.query import QueryBase
@@ -36,8 +33,7 @@ query = QueryBase(
3633)
3734print (" Results available at" , query.url())
3835
39- dotenv.load_dotenv()
40- dune = DuneClient(os.environ[" DUNE_API_KEY" ])
36+ dune = DuneClient.from_env()
4137results = dune.refresh(query)
4238```
4339
Original file line number Diff line number Diff line change 66from __future__ import annotations
77
88import logging .config
9+ import os
910from typing import Dict
1011
1112
@@ -27,6 +28,15 @@ def __init__(
2728 self .logger = logging .getLogger (__name__ )
2829 logging .basicConfig (format = "%(asctime)s %(levelname)s %(name)s %(message)s" )
2930
31+ @classmethod
32+ def from_env (cls ) -> BaseDuneClient :
33+ """
34+ Constructor allowing user to instantiate a client from environment variable
35+ without having to import dotenv or os manually
36+ We use `DUNE_API_KEY` as the environment variable that holds the API key.
37+ """
38+ return cls (os .environ ["DUNE_API_KEY" ])
39+
3040 @property
3141 def api_version (self ) -> str :
3242 """Returns client version string"""
Original file line number Diff line number Diff line change @@ -4,4 +4,4 @@ types-PyYAML>=6.0.12.11
44types-requests>=2.31.0.2
55python-dateutil>=2.8.2
66requests>=2.31.0
7- ndjson>=0.3.1
7+ ndjson>=0.3.1
Original file line number Diff line number Diff line change 1414)
1515from dune_client .query import QueryBase
1616
17+ dotenv .load_dotenv ()
18+
1719
1820class TestDuneClient (unittest .TestCase ):
1921 def setUp (self ) -> None :
@@ -28,9 +30,14 @@ def setUp(self) -> None:
2830 QueryParameter .enum_type (name = "ListField" , value = "Option 1" ),
2931 ],
3032 )
31- dotenv .load_dotenv ()
3233 self .valid_api_key = os .environ ["DUNE_API_KEY" ]
3334
35+ def test_from_env_constructor (self ):
36+ try :
37+ DuneClient .from_env ()
38+ except KeyError :
39+ self .fail ("DuneClient.from_env raised unexpectedly!" )
40+
3441 def test_get_status (self ):
3542 query = QueryBase (name = "No Name" , query_id = 1276442 , params = [])
3643 dune = DuneClient (self .valid_api_key )
@@ -189,7 +196,6 @@ def test_upload_csv_success(self):
189196@unittest .skip ("This is an enterprise only endpoint that can no longer be tested." )
190197class TestCRUDOps (unittest .TestCase ):
191198 def setUp (self ) -> None :
192- dotenv .load_dotenv ()
193199 self .valid_api_key = os .environ ["DUNE_API_KEY" ]
194200 self .client = DuneClient (self .valid_api_key , client_version = "alpha/v1" )
195201 self .existing_query_id = 2713571
You can’t perform that action at this time.
0 commit comments