Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ jobs:

- name: Run test suite
env:
TOKEN: ${{ secrets.TOKEN }}
ONC_TOKEN: ${{ secrets.ONC_TOKEN }}
run: tox --skip-pkg-install
8 changes: 4 additions & 4 deletions .github/workflows/doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
```
Expand Down
2 changes: 2 additions & 0 deletions doc/source/Tutorial/onc_Library_Tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
Expand Down
16 changes: 13 additions & 3 deletions src/onc/onc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import datetime
import json
import os
import re
from pathlib import Path

Expand All @@ -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.

Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from onc import ONC

load_dotenv(override=True)
token = os.getenv("TOKEN")
is_prod = os.getenv("ONC_ENV", "PROD") == "PROD"


Expand All @@ -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")
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ deps =
python-dotenv
pytest
pytest-cov
passenv = TOKEN
passenv = ONC_TOKEN
commands =
pytest --cov=onc

Expand Down