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

Add Client.from_env constructor #62

Merged
merged 3 commits into from
Sep 7, 2023
Merged
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
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
10 changes: 10 additions & 0 deletions dune_client/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from __future__ import annotations

import logging.config
import os
from typing import Dict


Expand All @@ -28,6 +29,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
bh2smith marked this conversation as resolved.
Show resolved Hide resolved
We use `DUNE_API_KEY` as the environment variable that holds the API key.
"""
return cls(os.environ["DUNE_API_KEY"])

@property
def api_version(self) -> str:
"""Returns client version string"""
Expand Down
2 changes: 1 addition & 1 deletion requirements/prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ types-PyYAML>=6.0.12.11
types-requests>=2.31.0.2
python-dateutil>=2.8.2
requests>=2.31.0
ndjson>=0.3.1
ndjson>=0.3.1
11 changes: 8 additions & 3 deletions tests/e2e/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
)
from dune_client.query import QueryBase

dotenv.load_dotenv()


class TestDuneClient(unittest.TestCase):
def setUp(self) -> None:
Expand All @@ -28,9 +30,14 @@ def setUp(self) -> None:
QueryParameter.enum_type(name="ListField", value="Option 1"),
],
)
dotenv.load_dotenv()
self.valid_api_key = os.environ["DUNE_API_KEY"]

def test_from_env_constructor(self):
try:
DuneClient.from_env()
except KeyError:
self.fail("DuneClient.from_env raised unexpectedly!")

def test_get_status(self):
query = QueryBase(name="No Name", query_id=1276442, params=[])
dune = DuneClient(self.valid_api_key)
Expand Down Expand Up @@ -156,7 +163,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 All @@ -179,7 +185,6 @@ def test_get_latest_result_with_query_id(self):
@unittest.skip("This is an enterprise only endpoint that can no longer be tested.")
class TestCRUDOps(unittest.TestCase):
def setUp(self) -> None:
dotenv.load_dotenv()
self.valid_api_key = os.environ["DUNE_API_KEY"]
self.client = DuneClient(self.valid_api_key, client_version="alpha/v1")
self.existing_query_id = 2713571
Expand Down
Loading