From d779e131e14aa97a6307594292838d13c30e6e21 Mon Sep 17 00:00:00 2001 From: david-i-berry Date: Thu, 19 Dec 2024 10:26:08 +0100 Subject: [PATCH 1/3] Changes to get_station_report - Fallback to REST API when either error accessing through OAI-PMH or when record deleted - Additional error handling / catching added --- pyoscar/__init__.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/pyoscar/__init__.py b/pyoscar/__init__.py index e9bd31a..8bfc3f7 100644 --- a/pyoscar/__init__.py +++ b/pyoscar/__init__.py @@ -220,16 +220,31 @@ def get_station_report(self, identifier: str, summary=False, LOGGER.debug(f'Request: {response.url}') LOGGER.debug(f'Response: {response.status_code}') + if "error" in str(response.content) or 'status="deleted"' in str(response.content): + # WSI not found via OAPI route, note this only works for primary WSI + # Try REST API, note this is much slower + LOGGER.warning(f"Falling back to REST API for {identifier}") + request = f"https://oscar.wmo.int/surface/rest/api/wmd/download/{identifier}" + response = requests.get(request, headers=self.headers) + if response.status_code == 404: + return {} + response.raise_for_status() if format_ == 'XML': - response = etree.fromstring(response.content) + try: + response = etree.fromstring(response.content) + except Exception as e: + raise e else: response = response.json() if summary: LOGGER.debug('Generating report summary') - return self.get_station_report_summary(response) + try: + return self.get_station_report_summary(response) + except Exception as e: + raise e else: return response From 5f849fb2ca1289dbe9dcb7497d5511420aa6a714 Mon Sep 17 00:00:00 2001 From: david-i-berry Date: Fri, 24 Jan 2025 16:57:47 +0100 Subject: [PATCH 2/3] Update __init__.py optimise checking of response text. --- pyoscar/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyoscar/__init__.py b/pyoscar/__init__.py index 8bfc3f7..0f5cc85 100644 --- a/pyoscar/__init__.py +++ b/pyoscar/__init__.py @@ -220,11 +220,11 @@ def get_station_report(self, identifier: str, summary=False, LOGGER.debug(f'Request: {response.url}') LOGGER.debug(f'Response: {response.status_code}') - if "error" in str(response.content) or 'status="deleted"' in str(response.content): - # WSI not found via OAPI route, note this only works for primary WSI + if any(s in response.text for s in ['error', 'deleted']): + # noqa WSI not found via OAPI route, note this only works for primary WSI # Try REST API, note this is much slower LOGGER.warning(f"Falling back to REST API for {identifier}") - request = f"https://oscar.wmo.int/surface/rest/api/wmd/download/{identifier}" + request = f"https://oscar.wmo.int/surface/rest/api/wmd/download/{identifier}" # noqa response = requests.get(request, headers=self.headers) if response.status_code == 404: return {} From 13c6a0c350da336832085de48b818d63a1a5578c Mon Sep 17 00:00:00 2001 From: david-i-berry Date: Fri, 24 Jan 2025 17:03:20 +0100 Subject: [PATCH 3/3] Update __init__.py Removal of hard coded API URL. --- pyoscar/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyoscar/__init__.py b/pyoscar/__init__.py index 0f5cc85..e46d241 100644 --- a/pyoscar/__init__.py +++ b/pyoscar/__init__.py @@ -224,7 +224,7 @@ def get_station_report(self, identifier: str, summary=False, # noqa WSI not found via OAPI route, note this only works for primary WSI # Try REST API, note this is much slower LOGGER.warning(f"Falling back to REST API for {identifier}") - request = f"https://oscar.wmo.int/surface/rest/api/wmd/download/{identifier}" # noqa + request = f"{self.api_url}/wmd/download/{identifier}" # noqa response = requests.get(request, headers=self.headers) if response.status_code == 404: return {}