-
Notifications
You must be signed in to change notification settings - Fork 43
Add reconciliation API endpoint to REST API #734
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
994a705
de054db
9a13014
4fb1bd3
57c2893
eaec714
d1f90df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
from __future__ import annotations | ||
|
||
import importlib | ||
import json | ||
from typing import TYPE_CHECKING, Any | ||
|
||
import connexion | ||
|
@@ -214,3 +215,72 @@ def learn( | |
return server_error(err) | ||
|
||
return None, 204 | ||
|
||
|
||
def _reconcile(project_id: str, query: dict[str, Any]) -> dict[str, Any]: | ||
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. The return type hint (found by SonarCloud) |
||
document = [{"text": query["query"]}] | ||
parameters = {"limit": query["limit"]} if "limit" in query else {} | ||
result = _suggest(project_id, document, parameters) | ||
|
||
if _is_error(result): | ||
return result | ||
|
||
results = [ | ||
{ | ||
"id": res["uri"], | ||
"name": res["label"], | ||
"score": res["score"], | ||
"match": res["label"] == query["query"], | ||
} | ||
for res in result[0]["results"] | ||
] | ||
return results | ||
|
||
|
||
def reconcile_metadata( | ||
project_id: str, **query_parameters | ||
) -> ConnexionResponse | dict[str, Any]: | ||
"""return service manifest or reconcile against a project and return a dict | ||
with results formatted according to OpenAPI spec""" | ||
|
||
try: | ||
project = annif.registry.get_project(project_id, min_access=Access.hidden) | ||
except ValueError: | ||
return project_not_found_error(project_id) | ||
|
||
if not query_parameters: | ||
return { | ||
"versions": ["0.2"], | ||
"name": "Annif Reconciliation Service for " + project.name, | ||
"identifierSpace": "", | ||
"schemaSpace": "http://www.w3.org/2004/02/skos/core#Concept", | ||
"view": {"url": "{{id}}"}, | ||
"defaultTypes": [{"id": "default-type", "name": "Default type"}], | ||
} | ||
else: | ||
queries = json.loads(query_parameters["queries"]) | ||
results = {} | ||
for key, query in queries.items(): | ||
data = _reconcile(project_id, query) | ||
if _is_error(data): | ||
return data | ||
results[key] = {"result": data} | ||
|
||
return results | ||
|
||
|
||
def reconcile( | ||
project_id: str, body: dict[str, Any] | ||
) -> ConnexionResponse | dict[str, Any]: | ||
"""reconcile against a project and return a dict with results | ||
formatted according to OpenAPI spec""" | ||
|
||
queries = body["queries"] | ||
results = {} | ||
for key, query in queries.items(): | ||
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. Using a for loop here means that the reconciliation isn't done in batches internally. It would be better (more efficient) to pass the whole batch to _suggest at once, similar to how the suggest_batch method works. |
||
data = _reconcile(project_id, query) | ||
if _is_error(data): | ||
return data | ||
results[key] = {"result": data} | ||
|
||
return results |
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.
It could be better to call this type ReconciliationServiceManifest (although it's pretty long...) so that it matches the reconciliation API spec.