From 0381a24219886d55c64c093aa667f7b89558ed8a Mon Sep 17 00:00:00 2001 From: Alexander Watzinger Date: Wed, 7 Aug 2024 14:44:46 +0200 Subject: [PATCH] Moved external API URLs to config --- config/default.py | 4 ++++ openatlas/api/external/geonames.py | 8 ++++++-- openatlas/api/external/wikidata.py | 7 +++++-- tests/test_reference_system.py | 3 +-- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/config/default.py b/config/default.py index 91456980f..dd303d368 100644 --- a/config/default.py +++ b/config/default.py @@ -60,3 +60,7 @@ # Tests LOAD_WINDOWS_TEST_SQL = False + +# External APIs +API_WIKIDATA = 'https://www.wikidata.org/w/api.php' +API_GEONAMES = 'http://api.geonames.org/get' diff --git a/openatlas/api/external/geonames.py b/openatlas/api/external/geonames.py index 94a918cd6..b9fc9e576 100644 --- a/openatlas/api/external/geonames.py +++ b/openatlas/api/external/geonames.py @@ -4,14 +4,18 @@ import xmltodict from flask import g +from openatlas import app + def fetch_geonames(id_: str) -> dict[str, Any]: - url = 'http://api.geonames.org/get' params = { 'geonameId': {id_}, 'username': {g.settings['geonames_username']}} try: - data = requests.get(url, params, timeout=10).content + data = requests.get( + app.config['API_GEONAMES'], + params, + timeout=10).content data_dict = xmltodict.parse(data)['geoname'] except Exception: # pragma: no cover return {} diff --git a/openatlas/api/external/wikidata.py b/openatlas/api/external/wikidata.py index c4e6eeb2b..9580850a5 100644 --- a/openatlas/api/external/wikidata.py +++ b/openatlas/api/external/wikidata.py @@ -3,6 +3,7 @@ import requests from flask import g +from openatlas import app from openatlas.display.util import link @@ -11,7 +12,6 @@ def add_resolver_url(id_: str) -> str: def fetch_wikidata(id_: str) -> dict[str, Any]: - url = 'https://www.wikidata.org/w/api.php' params = { 'action': 'wbgetentities', 'ids': id_, @@ -19,7 +19,10 @@ def fetch_wikidata(id_: str) -> dict[str, Any]: 'languages': 'en'} info = {} try: - data = requests.get(url, params=params, timeout=10).json() + data = requests.get( + app.config['API_WIKIDATA'], + params=params, + timeout=10).json() except Exception: # pragma: no cover return {} try: diff --git a/tests/test_reference_system.py b/tests/test_reference_system.py index 5601b62f7..a9a712fd5 100644 --- a/tests/test_reference_system.py +++ b/tests/test_reference_system.py @@ -26,8 +26,7 @@ def test_reference_system(self) -> None: data={'id_': '1158433263'}) assert b'Abasgulijev' in rv.data - rv: Any = self.app.get( - url_for('insert', class_='reference_system')) + rv = self.app.get(url_for('insert', class_='reference_system')) assert b'resolver URL' in rv.data rv = self.app.post(