Skip to content

Commit

Permalink
Add suggest service for reconciliation API
Browse files Browse the repository at this point in the history
  • Loading branch information
UnniKohonen committed Oct 24, 2023
1 parent 57c2893 commit eaec714
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
57 changes: 56 additions & 1 deletion annif/openapi/annif.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ paths:
"versions": [
"0.2"
],
"suggest": {
"entity": {
"service_path": "/suggest/entity",
"service_url": "/v1/projects/dummy-fi/reconcile"
}
},
"view": {
"url": "{{id}}"
}
Expand Down Expand Up @@ -276,7 +282,7 @@ paths:
post:
tags:
- Reconciliation
summary: reconcole against a project
summary: reconcile against a project
operationId: annif.rest.reconcile
parameters:
- $ref: '#components/parameters/project_id'
Expand Down Expand Up @@ -326,6 +332,36 @@ paths:
$ref: '#/components/schemas/ReconciliationResult'
"404":
$ref: '#/components/responses/NotFound'
/projects/{project_id}/reconcile/suggest/entity:
get:
tags:
- Reconciliation
summary: Entity auto-complete endpoint for the reconciliation service
operationId: annif.rest.reconcile_suggest
parameters:
- $ref: '#components/parameters/project_id'
- in: query
description: string to get suggestions for
name: prefix
required: true
schema:
type: string
example: example query
- in: query
description: number of suggestions to skip
name: cursor
required: false
schema:
type: integer
responses:
"200":
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/ReconcileSuggestResult'
"404":
$ref: '#/components/responses/NotFound'
components:
schemas:
ApiInfo:
Expand Down Expand Up @@ -559,6 +595,25 @@ components:
]
}
}
ReconcileSuggestResult:
type: object
required:
- result
properties:
result:
type: array
items:
type: object
required:
- name
- id
properties:
name:
type: string
example: example name
id:
type: string
example: example-id
Problem:
type: object
properties:
Expand Down
26 changes: 26 additions & 0 deletions annif/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ def reconcile_metadata(
"schemaSpace": "http://www.w3.org/2004/02/skos/core#Concept",
"view": {"url": "{{id}}"},
"defaultTypes": [{"id": "default-type", "name": "Default type"}],
"suggest": {
"entity": {
"service_path": "/suggest/entity",
"service_url": "http://localhost:5000/v1/projects/"
+ project_id
+ "/reconcile", # change to actual host url (how?)
}
},
}
else:
queries = json.loads(query_parameters["queries"])
Expand Down Expand Up @@ -284,3 +292,21 @@ def reconcile(
results[key] = {"result": data}

return results


def reconcile_suggest(
project_id: str, **query_parameters
) -> ConnexionResponse | dict[str, Any]:
"""suggest results for the given search term and return a dict with results
formatted according to OpenAPI spec"""

prefix = query_parameters.get("prefix")
cursor = query_parameters.get("cursor") if query_parameters.get("cursor") else 0
limit = cursor + 10

result = _suggest(project_id, [{"text": prefix}], {"limit": limit})
if _is_error(result):
return result

results = [{"id": res["uri"], "name": res["label"]} for res in result[0]["results"]]
return {"result": results[cursor:]}
15 changes: 15 additions & 0 deletions tests/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,18 @@ def test_openapi_reconcile_nonexistent(app_client):
"http://localhost:8000/v1/projects/nonexistent/reconcile", data=data
)
assert req.status_code == 404


def test_openapi_reconcile_suggest(app_client):
req = app_client.get(
"http://localhost:8000/v1/projects/dummy-fi/reconcile/suggest/entity?prefix=example"
)
assert req.status_code == 200
assert "result" in req.get_json()


def test_openapi_reconcile_suggest_nonexistent(app_client):
req = app_client.get(
"http://localhost:8000/v1/projects/nonexistent/reconcile/suggest/entity?prefix=example"
)
assert req.status_code == 404
12 changes: 12 additions & 0 deletions tests/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,15 @@ def test_rest_reconcile_nonexistent(app):
"nonexistent", {"queries": {"q0": {"query": "example text"}}}
)
assert result.status_code == 404


def test_rest_reconcile_suggest(app):
with app.app_context():
results = annif.rest.reconcile_suggest("dummy-fi", prefix="example text")
assert "result" in results


def test_rest_reconcile_nonexistent(app):
with app.app_context():
result = annif.rest.reconcile_suggest("nonexistent", prefix="example text")
assert result.status_code == 404

0 comments on commit eaec714

Please sign in to comment.