Skip to content

Commit

Permalink
add exists function for backend
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis committed Mar 20, 2024
1 parent 400c606 commit ec0c505
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion wis2-gdc-management/requirements-backend.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
elasticsearch
owslib
OWSLib
12 changes: 12 additions & 0 deletions wis2-gdc-management/wis2_gdc/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<BaseBackend>'
10 changes: 9 additions & 1 deletion wis2-gdc-management/wis2_gdc/backend/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 '<ElasticsearchBackend>'
27 changes: 19 additions & 8 deletions wis2-gdc-management/wis2_gdc/backend/ogcapi_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 '<OGCAPIRecordsBackend>'

0 comments on commit ec0c505

Please sign in to comment.