Skip to content
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

(PXP-6604) DRS endpoint to resolve regardless of prefix #291

Merged
merged 13 commits into from
Oct 20, 2020
2 changes: 1 addition & 1 deletion indexd/drs/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def get_drs_object(object_id):
"""
expand = True if flask.request.args.get("expand") == "true" else False

ret = blueprint.index_driver.get(object_id)
ret = blueprint.index_driver.get_with_nonstrict_prefix(object_id)

data = indexd_to_drs(ret, expand=expand, list_drs=False)

Expand Down
2 changes: 1 addition & 1 deletion indexd/index/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def get_index_record(record):
Returns a record.
"""

ret = blueprint.index_driver.get(record)
ret = blueprint.index_driver.get_with_nonstrict_prefix(record)
olivarity marked this conversation as resolved.
Show resolved Hide resolved

return flask.jsonify(ret), 200

Expand Down
23 changes: 23 additions & 0 deletions indexd/index/drivers/alchemy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import uuid
import json
import re
from contextlib import contextmanager
from cdislogging import get_logger
from sqlalchemy import (
Expand Down Expand Up @@ -1096,6 +1097,28 @@ def get(self, did, expand=True):

return record.to_document_dict()

def get_with_nonstrict_prefix(self, did, expand=True):
olivarity marked this conversation as resolved.
Show resolved Hide resolved
"""
Attempt to retrieve a record both with and without a prefix.
Proxies 'get' with provided id.
If not found but prefix matches default, attempt with prefix stripped.
If not found and id has no prefix, attempt with default prefix prepended.
"""
try:
# Try to resolve id as provided
record = self.get(did, expand=expand)
except NoRecordFound as e:
DEFAULT_PREFIX = self.config.get("DEFAULT_PREFIX")
match = re.match(r"(dg\.[0-9a-f]{4}\/)(.+)", did)
olivarity marked this conversation as resolved.
Show resolved Hide resolved
if match and match.group(1) == DEFAULT_PREFIX:
record = self.get(match.group(2), expand=expand)
elif match is None:
olivarity marked this conversation as resolved.
Show resolved Hide resolved
record = self.get(DEFAULT_PREFIX + did, expand=expand)
else:
raise e

return record

def update(self, did, rev, changing_fields):
"""
Updates an existing record with new values.
Expand Down