diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 497ac1e..9426240 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -43,5 +43,5 @@ jobs: - name: Run test suite env: - TOKEN: ${{ secrets.TOKEN }} + ONC_TOKEN: ${{ secrets.ONC_TOKEN }} run: tox --skip-pkg-install diff --git a/.github/workflows/doc.yml b/.github/workflows/doc.yml index a1ab6e6..321d5ba 100644 --- a/.github/workflows/doc.yml +++ b/.github/workflows/doc.yml @@ -25,21 +25,21 @@ jobs: - name: Build HTML env: - TOKEN: ${{ secrets.TOKEN }} + ONC_TOKEN: ${{ secrets.ONC_TOKEN }} run: | # Convert the dummy token to the real token for executing the jupyter notebook - sed -i "s/YOUR_TOKEN/$TOKEN/" doc/source/Tutorial/onc_Library_Tutorial.ipynb + sed -i "s/YOUR_TOKEN/$ONC_TOKEN/" doc/source/Tutorial/onc_Library_Tutorial.ipynb sed -i '/nb_execution_mode = "off"/d' doc/source/conf.py # enable executing jupyter notebook sphinx-build -W doc/source doc/build/html - name: Remove ampersand encoding and token in the generated html env: - TOKEN: ${{ secrets.TOKEN }} + ONC_TOKEN: ${{ secrets.ONC_TOKEN }} run: | # See issues about ampersand encoding at https://github.com/executablebooks/MyST-Parser/issues/760 sed -i 's/amp;//g' doc/build/html/Tutorial/Oceans_3.0_API_Tutorial.html # Some cell outputs in the Jupyter notebook contain the token - sed -i "s/$TOKEN/YOUR_TOKEN/g" doc/build/html/Tutorial/onc_Library_Tutorial.html + sed -i "s/$ONC_TOKEN/YOUR_TOKEN/g" doc/build/html/Tutorial/onc_Library_Tutorial.html - name: Run ghp-import to generate GitHub Pages run: | diff --git a/README.md b/README.md index 92551ce..5422d6e 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ The example below uses the `getLocations` method to search for locations that in from onc import ONC onc = ONC("YOUR_TOKEN") +onc = ONC() # This works if the env variable "ONC_TOKEN" is set onc.getLocations({"locationName": "Burrard"}) ``` diff --git a/doc/source/Tutorial/onc_Library_Tutorial.ipynb b/doc/source/Tutorial/onc_Library_Tutorial.ipynb index 4159578..a6c7882 100644 --- a/doc/source/Tutorial/onc_Library_Tutorial.ipynb +++ b/doc/source/Tutorial/onc_Library_Tutorial.ipynb @@ -78,6 +78,8 @@ "\n", "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", "\n", + "You can also use `onc = ONC()` if you have set the env variable \"ONC_TOKEN\".\n", + "\n", "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" ] }, diff --git a/src/onc/onc.py b/src/onc/onc.py index 922f983..facdc41 100644 --- a/src/onc/onc.py +++ b/src/onc/onc.py @@ -2,6 +2,7 @@ import datetime import json +import os import re from pathlib import Path @@ -20,8 +21,9 @@ class ONC: Parameters ---------- - token : str + token : str | None, default None The ONC API token, which could be retrieved at https://data.oceannetworks.ca/Profile once logged in. + If None, the token is read from the environment variable ``ONC_TOKEN``. production : boolean, default True Whether the ONC Production server URL is used for service requests. @@ -44,19 +46,27 @@ class ONC: Examples -------- >>> from onc import ONC + >>> onc = ONC() # Only if you set the env variable "ONC_TOKEN" # doctest: +SKIP >>> onc = ONC("YOUR_TOKEN_HERE") # doctest: +SKIP >>> onc = ONC("YOUR_TOKEN_HERE", showInfo=True, outPath="onc-files") # doctest: +SKIP """ # noqa: E501 def __init__( self, - token, + token: str | None = None, production: bool = True, showInfo: bool = False, - showWarning: bool = False, + showWarning: bool = True, outPath: str | Path = "output", timeout: int = 60, ): + if token is None or token == "": + token = os.environ.get("ONC_TOKEN") + if token is None or token == "": + raise ValueError( + "ONC API token is required. Please provide it as the first argument, " + "or set it as the environment variable 'ONC_TOKEN'." + ) self.token = re.sub(r"[^a-zA-Z0-9\-]+", "", token) self.showInfo = showInfo self.showWarning = showWarning diff --git a/tests/conftest.py b/tests/conftest.py index 3808280..12d434d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,7 +6,6 @@ from onc import ONC load_dotenv(override=True) -token = os.getenv("TOKEN") is_prod = os.getenv("ONC_ENV", "PROD") == "PROD" @@ -17,7 +16,7 @@ def pytest_configure(): @pytest.fixture def requester(tmp_path) -> ONC: - return ONC(token, is_prod, outPath=tmp_path) + return ONC(production=is_prod, outPath=tmp_path) @pytest.fixture(scope="session") @@ -51,7 +50,7 @@ def update_file_with_token_and_qa(tmp_py: Path) -> None: index = contents.index('onc = ONC("YOUR_TOKEN")\n') contents.insert( index + 1, - f"onc.token, onc.production, onc.timeout = '{token}', False, 300\n", # noqa: E501 + f"onc.token, onc.production, onc.timeout = '{os.getenv('ONC_TOKEN')}', False, 300\n", # noqa: E501 ) with open(tmp_py, "w") as f: f.writelines(contents) diff --git a/tox.ini b/tox.ini index 63b765a..8784bd0 100644 --- a/tox.ini +++ b/tox.ini @@ -12,7 +12,7 @@ deps = python-dotenv pytest pytest-cov -passenv = TOKEN +passenv = ONC_TOKEN commands = pytest --cov=onc