Skip to content

Commit

Permalink
add from_env constructor and update package config
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith committed Sep 5, 2023
1 parent f4828da commit 6a529d9
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 8 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ Export your `DUNE_API_KEY` (or place it in a `.env` file - as in
here [.env.sample](./.env.sample).

```python
import dotenv
import os

from dune_client.types import QueryParameter
from dune_client.client import DuneClient
from dune_client.query import QueryBase
Expand All @@ -36,8 +33,7 @@ query = QueryBase(
)
print("Results available at", query.url())

dotenv.load_dotenv()
dune = DuneClient(os.environ["DUNE_API_KEY"])
dune = DuneClient.from_env()
results = dune.refresh(query)
```

Expand Down
12 changes: 12 additions & 0 deletions dune_client/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
from __future__ import annotations

import logging.config
import os
from typing import Dict

from dotenv import load_dotenv


# pylint: disable=too-few-public-methods
class BaseDuneClient:
Expand All @@ -28,6 +31,15 @@ def __init__(
self.logger = logging.getLogger(__name__)
logging.basicConfig(format="%(asctime)s %(levelname)s %(name)s %(message)s")

@classmethod
def from_env(cls) -> BaseDuneClient:
"""
Constructor allowing user to instantiate a client from environment variable
without having to import dotenv or os manually
"""
load_dotenv()
return cls(os.environ["DUNE_API_KEY"])

@property
def api_version(self) -> str:
"""Returns client version string"""
Expand Down
1 change: 0 additions & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ black>=22.8.0
pandas>=1.0.0
pylint>=2.15.0
pytest>=7.1.3
python-dotenv>=0.21.0
mypy>=0.971
aiounittest>=1.4.2
3 changes: 2 additions & 1 deletion requirements/prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ types-PyYAML>=6.0.11
types-requests>=2.28.9
python-dateutil>=2.8.2
requests>=2.28.1
ndjson>=0.3.1
ndjson>=0.3.1
python-dotenv>=0.21.0
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ install_requires =
web3>=5.30.0
ndjson>=0.3.1
aiohttp>=3.8.3
python-dotenv>=0.21.0
python_requires = >=3.7
setup_requires =
setuptools_scm
Expand Down
1 change: 0 additions & 1 deletion tests/e2e/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ def test_internal_error(self):

def test_invalid_job_id_error(self):
dune = DuneClient(self.valid_api_key)

with self.assertRaises(DuneError) as err:
dune.get_status("Wonky Job ID")
self.assertEqual(
Expand Down

0 comments on commit 6a529d9

Please sign in to comment.