diff --git a/wis2-gdc-management/requirements-backend.txt b/wis2-gdc-management/requirements-backend.txt index b2066d6..c1cfe6b 100644 --- a/wis2-gdc-management/requirements-backend.txt +++ b/wis2-gdc-management/requirements-backend.txt @@ -1,2 +1,2 @@ elasticsearch -owslib +OWSLib diff --git a/wis2-gdc-management/wis2_gdc/backend/base.py b/wis2-gdc-management/wis2_gdc/backend/base.py index 22656e5..26631de 100644 --- a/wis2-gdc-management/wis2_gdc/backend/base.py +++ b/wis2-gdc-management/wis2_gdc/backend/base.py @@ -61,5 +61,17 @@ def save(self, record: dict) -> None: raise NotImplementedError() + @abstractmethod + def exists(self, identifier: str) -> bool: + """ + Querying whether a record exists in a backend + + :param identifier: `str` of record identifier + + :returns: `bool` of whether record exists in backend + """ + + raise NotImplementedError() + def __repr__(self): return '' diff --git a/wis2-gdc-management/wis2_gdc/backend/elastic.py b/wis2-gdc-management/wis2_gdc/backend/elastic.py index 0455270..6785a2e 100644 --- a/wis2-gdc-management/wis2_gdc/backend/elastic.py +++ b/wis2-gdc-management/wis2_gdc/backend/elastic.py @@ -22,7 +22,7 @@ import logging from urllib.parse import urlparse -from elasticsearch import Elasticsearch +from elasticsearch import Elasticsearch, NotFoundError from wis2_gdc.backend.base import BaseBackend @@ -158,5 +158,13 @@ def save(self, record: dict) -> None: LOGGER.debug(f"Indexing record {record['id']}") self.es.index(index=self.index_name, id=record['id'], body=record) + def exists(self, identifier: str) -> bool: + LOGGER.debug(f'Querying GDC for id {identifier}') + try: + _ = self.es.get(index=self.index_name, id=identifier) + return True + except NotFoundError: + return False + def __repr__(self): return '' diff --git a/wis2-gdc-management/wis2_gdc/backend/ogcapi_records.py b/wis2-gdc-management/wis2_gdc/backend/ogcapi_records.py index 6864671..61e5b4a 100644 --- a/wis2-gdc-management/wis2_gdc/backend/ogcapi_records.py +++ b/wis2-gdc-management/wis2_gdc/backend/ogcapi_records.py @@ -20,6 +20,9 @@ ############################################################################### import logging +import json + +from owslib.ogcapi import Records from wis2_gdc import env from wis2_gdc.backend.base import BaseBackend @@ -29,18 +32,18 @@ class OGCAPIRecordsBackend(BaseBackend): - def save(self): + def __init__(self, defs): + super().__init__(defs) - import json + self.conn = Records(env.API_URL) + self.collection = 'discovery-metadata' - from owslib.ogcapi import Records + def save(self): - oarec = Records(env.API_URL) - collection = 'discovery-metadata' ttype = 'create' try: - _ = oarec.get_collection_item(self.metadata['id']) + _ = self.conn.get_collection_item(self.metadata['id']) ttype = 'update' except Exception: pass @@ -49,10 +52,18 @@ def save(self): if ttype == 'create': LOGGER.debug('Adding new record to catalogue') - _ = oarec.get_collection_create(collection, payload) + _ = self.conn.get_collection_create(self.collection, payload) elif ttype == 'update': LOGGER.debug('Updating existing record in catalogue') - _ = oarec.get_collection_update(collection, payload) + _ = self.conn.get_collection_update(self.collection, payload) + + def exists(self, identifier: str) -> bool: + LOGGER.debug(f'Querying GDC for id {identifier}') + try: + _ = self.conn.collection_item(self.collection, identifier) + return True + except RuntimeError: + return False def __repr__(self): return ''