Skip to content

Commit 1b9568d

Browse files
committed
feat: add env variable support when initiating ONC class
1 parent 475df4e commit 1b9568d

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ The example below uses the `getLocations` method to search for locations that in
6060
from onc import ONC
6161

6262
onc = ONC("YOUR_TOKEN")
63+
onc = ONC() # This works if the env variable "ONC_TOKEN" is set
6364

6465
onc.getLocations({"locationName": "Burrard"})
6566
```

doc/source/Tutorial/onc_Library_Tutorial.ipynb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
"\n",
7979
"to indicate that you want to see verbose messages after running each method, and you want the download directory to be \"./YOUR_DIRECTORY\" instead of the default \"./output\" when calling methods that involve downloading files like `orderDataProduct` and `downloadArchivefile`.\n",
8080
"\n",
81+
"You can also use `onc = ONC()` if you have set the env variable \"ONC_TOKEN\".\n",
82+
"\n",
8183
"For more information, check the API reference of the [ONC](https://oceannetworkscanada.github.io/api-python-client/autoapi/onc/index.html#onc.ONC) class.\n"
8284
]
8385
},

src/onc/onc.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import datetime
44
import json
5+
import os
56
import re
67
from pathlib import Path
78

@@ -20,8 +21,9 @@ class ONC:
2021
2122
Parameters
2223
----------
23-
token : str
24+
token : str | None, default None
2425
The ONC API token, which could be retrieved at https://data.oceannetworks.ca/Profile once logged in.
26+
If None, the token is read from the environment variable ``ONC_TOKEN``.
2527
production : boolean, default True
2628
Whether the ONC Production server URL is used for service requests.
2729
@@ -44,19 +46,26 @@ class ONC:
4446
Examples
4547
--------
4648
>>> from onc import ONC
49+
>>> onc = ONC() # Only if you set the env variable "ONC_TOKEN" # doctest: +SKIP
4750
>>> onc = ONC("YOUR_TOKEN_HERE") # doctest: +SKIP
4851
>>> onc = ONC("YOUR_TOKEN_HERE", showInfo=True, outPath="onc-files") # doctest: +SKIP
4952
""" # noqa: E501
5053

5154
def __init__(
5255
self,
53-
token,
56+
token: str | None = None,
5457
production: bool = True,
5558
showInfo: bool = False,
5659
showWarning: bool = False,
5760
outPath: str | Path = "output",
5861
timeout: int = 60,
5962
):
63+
if token is None or token == "":
64+
token = os.environ.get("ONC_TOKEN")
65+
if token is None or token == "":
66+
raise ValueError(
67+
"ONC API token is required. Please provide it as the first argument, "
68+
"or set it as the environment variable 'ONC_TOKEN'.")
6069
self.token = re.sub(r"[^a-zA-Z0-9\-]+", "", token)
6170
self.showInfo = showInfo
6271
self.showWarning = showWarning

tests/conftest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from onc import ONC
77

88
load_dotenv(override=True)
9-
token = os.getenv("TOKEN")
109
is_prod = os.getenv("ONC_ENV", "PROD") == "PROD"
1110

1211

@@ -17,7 +16,7 @@ def pytest_configure():
1716

1817
@pytest.fixture
1918
def requester(tmp_path) -> ONC:
20-
return ONC(token, is_prod, outPath=tmp_path)
19+
return ONC(production=is_prod, outPath=tmp_path)
2120

2221

2322
@pytest.fixture(scope="session")

0 commit comments

Comments
 (0)