Skip to content

Commit

Permalink
DTR integration
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalGawor committed Jun 6, 2024
1 parent 861f591 commit 21c9a58
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
15 changes: 13 additions & 2 deletions doglib/doglib.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
import logging
import os
from typing import List, Union, Optional

from . import curl
from .dtr import expand_datatype
from .dtr import expand_datatype, DataTypeNotFoundException
from .pid import pid_factory, PID
from .repos import JSONParser, XMLParser
from .repos import RegRepo, warn_europeana
Expand Down Expand Up @@ -78,9 +79,16 @@ def fetch(self, pid_string: Union[str, PID], format: str = 'dict',
return ""
fetch_result: dict = self._fetch(pid)
if dtr:
unique_data_types: set = set()
for entry in fetch_result['ref_files']:
for resource in entry['ref_resources']:
resource['data_type_taxonomy'] = expand_datatype(resource['data_type'])
data_type = resource['data_type']
unique_data_types.add(data_type)
for unique_type in unique_data_types:
try:
fetch_result['dtr'][unique_type] = expand_datatype(unique_type)
except DataTypeNotFoundException:
continue
if format == 'dict':
return fetch_result
elif format == 'jsons' or format == 'str':
Expand Down Expand Up @@ -308,3 +316,6 @@ def is_pid(self, pid_string: Union[str, PID]) -> bool:
"""
pid = pid_factory(pid_string)
return True if pid is not None else False

def get_repository_report(self):

14 changes: 10 additions & 4 deletions doglib/dtr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import json
from typing import List, Set, Union

from requests import RequestException

from .curl import get


class DataTypeNotFound(Exception):
class DataTypeNotFoundException(Exception):
def __init__(self, message):
self.message = message

Expand All @@ -25,13 +27,17 @@ def get_dtr_taxonomy_by_type(data_type: str) -> dict:
:return: dict, a dictionary representation of the type's taxonomy
"""
dtr_type_search_endpoint = f"http://typeapi.lab.pidconsortium.net/v1/taxonomy/search?query={data_type}&name={data_type}"
url, dtr_taxonomy_search_response, header = get(dtr_type_search_endpoint)
try:
url, dtr_taxonomy_search_response, header = get(dtr_type_search_endpoint)
except RequestException as error:
raise DataTypeNotFoundException(f"DataType <{data_type}> doesn't exist in the DTR taxonomy") from error

dtr_taxonomy_json = json.loads(dtr_taxonomy_search_response)

try:
dtr_type_id = dtr_taxonomy_json[0]["id"]
except (IndexError, KeyError) as error:
raise DataTypeNotFound(f"DataType <{data_type}> doesn't exist in the DTR taxonomy") from error
raise DataTypeNotFoundException(f"DataType <{data_type}> doesn't exist in the DTR taxonomy") from error
parents = dtr_taxonomy_json[0]["parents"]
if parents:
for parent_id, parent_name in parents.items():
Expand All @@ -53,7 +59,7 @@ def get_taxonomy_root_node_by_id(data_type_id: str) -> str:
try:
dtr_type_id = dtr_taxonomy_json["id"]
except (IndexError, KeyError) as error:
raise DataTypeNotFound(f"DataType with id <{data_type_id}> doesn't exist in the DTR taxonomy") from error
raise DataTypeNotFoundException(f"DataType with id <{data_type_id}> doesn't exist in the DTR taxonomy") from error
parents = dtr_taxonomy_json["parents"]
# Assumption of single parent
taxonomy_root_id = dtr_type_id
Expand Down

0 comments on commit 21c9a58

Please sign in to comment.