-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pragerdom/be-366: OpenAIRE authority provider #199
Open
pragerdom
wants to merge
20
commits into
main
Choose a base branch
from
pragerdom/be-366-implement-openaire-authority-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+508
−56
Open
Changes from 9 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e86dbce
feat: schema file for awards
pragerdom d711eb0
feat: OpenAIRE Provider constructor
pragerdom b849af8
Merge branch 'main' into pragerdom/be-366-implement-openaire-authorit…
pragerdom 6f7f149
feat: introduce OpenAIRE to tests and model
pragerdom d23d456
feat: OpenAIRE Authority Provider implementation
pragerdom b430aee
fix: incorrect (test) docker-compose.yml file
pragerdom f911415
refactor: remove unused import, return vocab item faster
pragerdom 000fa87
refactor: logger, token caching, config from current_app
pragerdom f037630
refactor: access app for keys in tests from context
pragerdom 8f6041b
refactor: remove some try-except blocks
pragerdom 70e268f
refactor: make relations (organizations) get more readable
pragerdom 4b9c2e8
fix: prevent None value access
pragerdom e18e381
Merge branch 'main' into pragerdom/be-366-implement-openaire-authorit…
pragerdom 4113437
refactor: unused import, change program finding method to recursive call
pragerdom 5deb86c
temporary dependancy fix
b96ddda
refactor: more consistent relations fetch, unite tests with ORCID
pragerdom 95acb2e
format: reformat OpenAIRE provider
pragerdom 3545ddc
refactor: more readable NoneType checking
pragerdom e1f65e8
Merge branch 'main' into pragerdom/be-366-implement-openaire-authorit…
pragerdom ae6e22d
fix: ROR provider code update from main
pragerdom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from .providers import AuthorityProvider, RORProviderV2, ORCIDProvider | ||
from .providers import AuthorityProvider, RORProviderV2, ORCIDProvider, OpenAIREProvider | ||
|
||
__all__ = ("AuthorityProvider", "RORProviderV2", "ORCIDProvider") | ||
__all__ = ("AuthorityProvider", "RORProviderV2", "ORCIDProvider", "OpenAIREProvider") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
254 changes: 254 additions & 0 deletions
254
oarepo_vocabularies/authorities/providers/openaire_provider.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
import base64 | ||
import logging | ||
import os | ||
from flask import current_app | ||
import requests | ||
|
||
from oarepo_vocabularies.authorities.providers.base import AuthorityProvider | ||
|
||
|
||
logger = logging.getLogger("oarepo-vocabularies.providers.openaire") | ||
|
||
class OpenAIREClient(object): | ||
|
||
def __init__(self, client_id, client_secret, url=None, testing=False, timeout=None, **kwargs): | ||
self.client_id = client_id | ||
self.client_secret = client_secret | ||
self.testing = testing | ||
self.timeout = timeout or 10000 | ||
|
||
def _get_token(self): | ||
url = "https://aai.openaire.eu/oidc/token" | ||
credentials = f"{self.client_id}:{self.client_secret}" | ||
encoded_credentials = base64.b64encode(credentials.encode('utf-8')).decode('utf-8') | ||
|
||
headers = { | ||
"Authorization": f"Basic {encoded_credentials}" | ||
} | ||
|
||
data = { | ||
"grant_type": "client_credentials" | ||
} | ||
|
||
try: | ||
response = requests.post(url, headers=headers, data=data) | ||
response.raise_for_status() | ||
return response.json().get("access_token") | ||
except requests.exceptions.HTTPError as http_err: | ||
logger.error(f"HTTP error occurred: {http_err}") | ||
except Exception as err: | ||
logger.error(f"Other error occurred: {err}") | ||
|
||
def quick_search(self, access_token, search_query="", page=1, page_size=20 ): | ||
url = "https://api.openaire.eu/search/projects?format=json" | ||
if not access_token: | ||
return {} | ||
headers = { | ||
"Authorization": f"Bearer {access_token.strip()}" | ||
} | ||
|
||
if not search_query or search_query == "": | ||
return {} | ||
|
||
params = { | ||
"name": search_query, | ||
"page": page, | ||
"size": page_size | ||
} | ||
|
||
response = requests.get(url, headers=headers, params=params) | ||
if response.status_code != 200: | ||
logger.error(f"Error response: {response.status_code}") | ||
logger.error(f"Response content: {response.text}") | ||
response.raise_for_status() | ||
return response.json() | ||
|
||
def get_record(self, item_id, access_token): | ||
url = f"https://api.openaire.eu/search/projects?openaireProjectID={item_id}&format=json" | ||
|
||
headers = { | ||
"Authorization": f"Bearer {access_token.strip()}" | ||
} | ||
|
||
response = requests.get(url, headers=headers) | ||
response.raise_for_status() | ||
return response.json() | ||
|
||
|
||
class OpenAIREProvider(AuthorityProvider): | ||
|
||
_cached_token = None | ||
|
||
def __init__(self, url=None, testing=False, **kwargs): | ||
self.openaire_client = OpenAIREClient(current_app.config["OPENAIRE_CLIENT_ID"], current_app.config["OPENAIRE_CLIENT_SECRET"], url, testing, **kwargs) | ||
|
||
def get_access_token(self): | ||
if self._cached_token is None: | ||
self._cached_token = self.openaire_client._get_token() | ||
return self._cached_token | ||
|
||
def search(self, identity, params, **kwargs): | ||
params = params or {} | ||
access_token = self.get_access_token() | ||
|
||
response = self.openaire_client.quick_search( | ||
access_token=access_token, | ||
search_query=params.get("q", ""), | ||
page=params.get("page", 1), | ||
page_size=params.get("page_size", 20) | ||
) | ||
|
||
results = response.get("response", {}) | ||
|
||
if not results: | ||
return [], 0 | ||
|
||
items = [self.to_vocabulary_item(openaire_item) for openaire_item in results.get("results", []).get("result", [])] | ||
total = OpenAIREProvider.dict_get(results, "header", "total", "$") | ||
|
||
|
||
return items, total | ||
|
||
|
||
|
||
def get(self, identity, item_id, **kwargs): | ||
|
||
access_token = self.get_access_token() | ||
|
||
record = self.openaire_client.get_record(item_id, access_token) | ||
|
||
if record is None: | ||
raise KeyError(f"OpenAIRE record {item_id} not found.") | ||
|
||
return self.to_vocabulary_item(record.get("response", {})) | ||
|
||
@staticmethod | ||
def dict_get(d, *args, default={}): | ||
""" Iteratively reach for a key in a nested dictionary """ | ||
for path in args: | ||
if not isinstance(d, dict) or path not in d: | ||
return default | ||
d = d[path] | ||
return d | ||
|
||
@staticmethod | ||
def get_program_from_funding(funding_tree): | ||
""" Explicitly search for the first program in the funding tree """ | ||
if funding_tree == []: | ||
return "N/A" | ||
if isinstance(funding_tree, list): | ||
funder_info = funding_tree[0].items() | ||
else: | ||
funder_info = funding_tree.items() | ||
|
||
for _, value in funder_info: | ||
if isinstance(value, dict): | ||
if "parent" in value and value["parent"] is not None: | ||
for _, value in value["parent"].items(): | ||
if "class" in value: | ||
return value["class"]["$"] | ||
if "class" in value: | ||
return value["class"]["$"] | ||
|
||
return "N/A" | ||
|
||
@staticmethod | ||
def to_vocabulary_item(record): | ||
|
||
# Parse the record | ||
header = record.get("header", {}) | ||
metadata = record.get("metadata", {}) | ||
entity = metadata.get("oaf:entity", {}) | ||
project = entity.get("oaf:project", {}) | ||
|
||
try: | ||
relations = project.get("rels", {}).get("rel", []) | ||
except KeyError: | ||
relations = {} | ||
except AttributeError: | ||
relations = {} | ||
|
||
# If there is only one relation, convert it to a list | ||
if not isinstance(relations, list): | ||
relations = [relations] | ||
|
||
# Tags (keywords) | ||
keywords = project.get("keywords", "") | ||
|
||
if isinstance(keywords, dict): | ||
keywords = keywords.get("$", "") | ||
tags = keywords.split(",") | ||
|
||
# Identifiers | ||
identifiers = [] | ||
|
||
identifiers.append({ | ||
"identifier": header.get("dri:objIdentifier", {}).get("$", ""), | ||
"scheme": "dri:objIdentifier" | ||
}) | ||
|
||
identifiers.append({ | ||
"identifier": project.get("originalId", {}).get("$", ""), | ||
"scheme": "openaire:originalId" | ||
}) | ||
|
||
# Number (code), title (with locale) and acronym | ||
number = project.get("code", {}).get("$", "") | ||
title = { | ||
header.get("locale", {}).get("$", "en")[:2]: project.get("title", {}).get("$", "") | ||
} | ||
acronym = project.get("acronym", {}).get("$", "") | ||
|
||
|
||
# Funder and according program | ||
funding = project.get("fundingtree", []) | ||
try: | ||
funder = { | ||
"id": OpenAIREProvider.dict_get(funding, "funder", "id", "$"), | ||
"name": OpenAIREProvider.dict_get(funding, "funder", "name", "$"), | ||
} | ||
except IndexError: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these errors thrown? dict_get seems to check for path existence before x[...] |
||
funder = {} | ||
except KeyError: | ||
funder = {} | ||
|
||
program = OpenAIREProvider.get_program_from_funding(funding) | ||
|
||
# Subjects and organizations | ||
subjects = [] | ||
|
||
subject_list = project.get("subject", []) | ||
|
||
if not isinstance(subject_list, list) and subject_list is not None: | ||
subject_list = [subject_list] | ||
|
||
for subject in subject_list: | ||
subjects.append({ | ||
"id": subject.get("@classid", ""), | ||
"subject": subject.get("$", "") | ||
}) | ||
|
||
organizations = [] | ||
for relation in relations: | ||
try: | ||
relation_to = relation.get("to", "") | ||
organizations.append({ | ||
"scheme": relation_to.get("@scheme", ""), | ||
"id": relation_to.get("$", ""), | ||
"organization": relation.get("legalname", {}).get("$", "") | ||
}) | ||
except AttributeError: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where does the attribute error come from? |
||
organizations.append({}) | ||
|
||
return { | ||
"$schema": "local://awards/award-v1.0.0.json", | ||
"tags": tags, | ||
"identifiers": identifiers, | ||
"number": number, | ||
"title": title, | ||
"funder": funder, | ||
"acronym": acronym, | ||
"program": program, | ||
"subjects": subjects, | ||
"organizations": organizations | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Key & Attribute error should not be thrown here, as you use .get(...)