Skip to content

Commit

Permalink
Fix expired KNMI Data Platform API
Browse files Browse the repository at this point in the history
Test skips were added in #358 because they failed.

Co-Authored-By: Martin Vonk <[email protected]>
  • Loading branch information
ArtesiaWater and martinvonk committed Jul 4, 2024
1 parent d9e7049 commit 723d0fb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
35 changes: 18 additions & 17 deletions nlmod/read/knmi_data_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,26 @@

def get_anonymous_api_key() -> Union[str, None]:
try:
url = "https://developer.dataplatform.knmi.nl/get-started"
tables = read_html(url) # get all tables from url
for table in tables:
for coln in table.columns:
if "KEY" in coln.upper(): # look for columns with key
api_key_str = table.iloc[0].loc[
coln
] # get entry with key (first row)
api_key = max(
api_key_str.split(), key=len
) # get key base on str length
logger.info(f"Retrieved anonymous API Key from {url}")
return api_key
url = "https://developer.dataplatform.knmi.nl/open-data-api#token"
webpage = requests.get(url) # get webpage
api_key = (
webpage.text.split("</code></pre>")[0].split("<pre><code>")[-1].strip()
) # obtain apikey from codeblock on webpage
if len(api_key) != 120:
msg = f"Could not obtain API Key from {url}, trying API Key from memory. Found API Key = {api_key}"
logger.error(msg)
raise ValueError(msg)
logger.info(f"Retrieved anonymous API Key from {url}")
return api_key
except Exception as exc:
if Timestamp.today() < Timestamp("2024-07-01"):
logger.info("Retrieved anonymous API Key from memory")
api_key_memory_date = "2025-07-01"
if Timestamp.today() < Timestamp(api_key_memory_date):
logger.info(
f"Retrieved anonymous API Key (available till {api_key_memory_date}) from memory"
)
api_key = (
"eyJvcmciOiI1ZTU1NGUxOTI3NGE5NjAwMDEyYTNlYjEiLCJpZCI6ImE1OGI5"
"NGZmMDY5NDRhZDNhZjFkMDBmNDBmNTQyNjBkIiwiaCI6Im11cm11cjEyOCJ9"
"eyJvcmciOiI1ZTU1NGUxOTI3NGE5NjAwMDEyYTNlYjEiLCJpZCI6ImE1OGI5N
"GZmMDY5NDRhZDNhZjFkMDBmNDBmNTQyNjBkIiwiaCI6Im11cm11cjEyOCJ9"
)
return api_key
else:
Expand Down
6 changes: 2 additions & 4 deletions tests/test_018_knmi_data_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
import os
from pathlib import Path

import pytest

from nlmod.read import knmi_data_platform

data_path = Path(__file__).parent / "data"

@pytest.mark.skip(reason="FileNotFoundError: download/INTER_OPER_R___EV24____L3__20240626T000000_20240627T000000_0003.nc not found")

def test_download_multiple_nc_files() -> None:
dataset_name = "EV24"
dataset_version = "2"
Expand All @@ -33,7 +31,7 @@ def test_download_multiple_nc_files() -> None:
# plot the mean evaporation
ds["prediction"].mean("time").plot()

@pytest.mark.skip(reason="KeyError: 'files'")

def test_download_read_zip_file() -> None:
dataset_name = "rad_nl25_rac_mfbs_24h_netcdf4"
dataset_version = "2.0"
Expand Down

0 comments on commit 723d0fb

Please sign in to comment.