-
Notifications
You must be signed in to change notification settings - Fork 8
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
feat: add "POST ../access" & "PUT ../{access_id}" #32
Open
sarthakgupta072
wants to merge
6
commits into
dev
Choose a base branch
from
access_endpoints
base: dev
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7d3c515
feat(api): add POST and PUT access method endpoints
sarthakgupta072 872e213
feat: add implementation for register_access_methods
sarthakgupta072 e410bab
docs and refactoring
sarthakgupta072 bcd32b6
refactor: flake8 fix
sarthakgupta072 08c3204
refactor: new implementation
sarthakgupta072 1787452
minor doc change
sarthakgupta072 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
133 changes: 133 additions & 0 deletions
133
drs_filer/ga4gh/drs/endpoints/register_access_methods.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,133 @@ | ||
import logging | ||
from random import choice | ||
import string | ||
from typing import Dict, Optional | ||
|
||
from flask import current_app | ||
|
||
from drs_filer.errors.exceptions import ( | ||
ObjectNotFound, | ||
InternalServerError | ||
) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def register_access_method( | ||
data: Dict, | ||
object_id: str, | ||
access_id: Optional[str] = None, | ||
retries: int = 9 | ||
) -> str: | ||
"""Register access method. | ||
|
||
Args: | ||
data: Request object of type `AccessMethodRegister`. | ||
object_id: DRS object identifier. | ||
access_id: Access method identifier. Auto-generated if not provided. | ||
retries: If `access_id` is not supplied, how many times should the | ||
generation of a random identifier and insertion into the database | ||
be retried in case of duplicate access_ids. | ||
|
||
Returns: | ||
A unique identifier for the access method. | ||
""" | ||
# Set parameters | ||
db_collection = ( | ||
current_app.config['FOCA'].db.dbs['drsStore']. | ||
collections['objects'].client | ||
) | ||
obj = db_collection.find_one({"id": object_id}) | ||
if not obj: | ||
logger.error(f"DRS object with id: {object_id} not found.") | ||
raise ObjectNotFound | ||
|
||
# Set flags and parameters for POST/PUT routes | ||
replace = True | ||
if access_id is None: | ||
replace = False | ||
id_length = ( | ||
current_app.config['FOCA'].endpoints['objects']['id_length'] | ||
) | ||
id_charset: str = ( | ||
current_app.config['FOCA'].endpoints['objects']['id_charset'] | ||
) | ||
# evaluate character set expression or interpret literal string as set | ||
try: | ||
id_charset = eval(id_charset) | ||
except Exception: | ||
id_charset = ''.join(sorted(set(id_charset))) | ||
|
||
# Try to generate unique ID and insert object into database | ||
for i in range(retries + 1): | ||
logger.debug(f"Trying to insert/update access method: try {i}") | ||
# Set or generate object identifier | ||
if access_id is not None: | ||
data['access_id'] = access_id | ||
else: | ||
data['access_id'] = generate_id( | ||
charset=id_charset, # type: ignore | ||
length=id_length, # type: ignore | ||
) | ||
# Replace access method, then return (PUT) | ||
if replace: | ||
result_replace = db_collection.update_one( | ||
filter={'id': object_id}, | ||
update={ | ||
'$set': { | ||
'access_methods.$[element]': data | ||
} | ||
}, | ||
array_filters=[{'element.access_id': data['access_id']}], | ||
) | ||
|
||
if(result_replace.modified_count): | ||
logger.info( | ||
f"Replaced access method with access_id: " | ||
f"{data['access_id']} of DRS object with id: {object_id}" | ||
) | ||
break | ||
|
||
# Try inserting the access method incase of POST or incase | ||
# no element matches with the filter incase of PUT. | ||
result_insert = db_collection.update_one( | ||
filter={ | ||
'id': object_id, | ||
'access_methods.access_id': {'$ne': data['access_id']} | ||
}, | ||
update={ | ||
'$push': { | ||
'access_methods': data | ||
} | ||
} | ||
) | ||
if(result_insert.modified_count): | ||
logger.info( | ||
f"Added access method with access_id: {data['access_id']}" | ||
f" to DRS object with id: {object_id}" | ||
) | ||
break | ||
# Access method neither added nor updated. | ||
else: | ||
logger.error( | ||
f"Could not generate unique identifier. Tried {retries + 1} times." | ||
) | ||
raise InternalServerError | ||
return data['access_id'] | ||
|
||
|
||
def generate_id( | ||
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. I think you can import that function from FOCA now, it should be available in |
||
charset: str = ''.join([string.ascii_letters, string.digits]), | ||
length: int = 6 | ||
) -> str: | ||
"""Generate random string based on allowed set of characters. | ||
|
||
Args: | ||
charset: String of allowed characters. | ||
length: Length of returned string. | ||
|
||
Returns: | ||
Random string of specified length and composed of defined set of | ||
allowed characters. | ||
""" | ||
return ''.join(choice(charset) for __ in range(length)) |
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
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.
Create an access method