55start or end position.
66"""
77
8+ from typing import Dict , Tuple , List , Optional
89from ..utils .logging import LOG
910from .. import __apiVersion__ , __handover_beacon__ , __handover_drs__
1011from ..utils .data_query import filter_exists , find_datasets , fetch_datasets_access
1314from .exceptions import BeaconUnauthorised , BeaconForbidden , BeaconBadRequest
1415
1516
16- def access_resolution (request , token , host , public_data , registered_data , controlled_data ):
17+ def access_resolution (request : Dict , token : Dict ,
18+ host : str ,
19+ public_data : List [str ],
20+ registered_data : List [str ],
21+ controlled_data : List [str ]) -> Tuple [List [str ], List [str ]]:
1722 """Determine the access level for a user.
1823
1924 Depends on user bona_fide_status, and by default it should be PUBLIC.
@@ -23,12 +28,12 @@ def access_resolution(request, token, host, public_data, registered_data, contro
2328 # unless the request is for specific datasets
2429 if public_data :
2530 permissions .append ("PUBLIC" )
26- access = set (public_data ) # empty if no datasets are given
31+ accessible_datasets = set (public_data ) # empty if no datasets are given
2732
2833 # for now we are expecting that the permissions are a list of datasets
2934 if registered_data and token ["bona_fide_status" ] is True :
3035 permissions .append ("REGISTERED" )
31- access = access .union (set (registered_data ))
36+ accessible_datasets = accessible_datasets .union (set (registered_data ))
3237 # if user requests public datasets do not throw an error
3338 # if both registered and controlled datasets are request this will be shown first
3439 elif registered_data and not public_data :
@@ -43,7 +48,7 @@ def access_resolution(request, token, host, public_data, registered_data, contro
4348 # Default event, when user doesn't specify dataset ids
4449 # Contains only dataset ids from token that are present at beacon
4550 controlled_access = set (controlled_data ).intersection (set (token ['permissions' ]))
46- access = access .union (controlled_access )
51+ accessible_datasets = accessible_datasets .union (controlled_access )
4752 if controlled_access :
4853 permissions .append ("CONTROLLED" )
4954 # if user requests public datasets do not throw an error
@@ -54,11 +59,12 @@ def access_resolution(request, token, host, public_data, registered_data, contro
5459 raise BeaconUnauthorised (request , host , "missing_token" , 'Unauthorized access to dataset(s), missing token.' )
5560 # token is present, but is missing perms (user authed but no access)
5661 raise BeaconForbidden (request , host , 'Access to dataset(s) is forbidden.' )
57- LOG .info (f"Accesible datasets are: { list (access )} ." )
58- return permissions , list (access )
5962
63+ LOG .info (f"Accesible datasets are: { list (accessible_datasets )} ." )
64+ return permissions , list (accessible_datasets )
6065
61- async def query_request_handler (params ):
66+
67+ async def query_request_handler (params : Tuple ) -> Dict :
6268 """Handle the parameters of the query endpoint in order to find the required datasets.
6369
6470 params = db_pool, method, request, token, host
@@ -91,18 +97,20 @@ async def query_request_handler(params):
9197 raise BeaconBadRequest (request , params [4 ], "endMin value Must be smaller than endMax value" )
9298 if request .get ("startMin" ) and request .get ("startMin" ) > request .get ("startMax" ):
9399 raise BeaconBadRequest (request , params [4 ], "startMin value Must be smaller than startMax value" )
94- requested_position = (request .get ("start" , None ), request .get ("end" , None ),
95- request .get ("startMin" , None ), request .get ("startMax" , None ),
96- request .get ("endMin" , None ), request .get ("endMax" , None ))
100+ requested_position : Tuple [ Optional [ int ], ...] = (request .get ("start" , None ), request .get ("end" , None ),
101+ request .get ("startMin" , None ), request .get ("startMax" , None ),
102+ request .get ("endMin" , None ), request .get ("endMax" , None ))
97103
98104 # Get dataset ids that were requested, sort by access level
99105 # If request is empty (default case) the three dataset variables contain all datasets by access level
100106 # Datasets are further filtered using permissions from token
101107 public_datasets , registered_datasets , controlled_datasets = await fetch_datasets_access (params [0 ], request .get ("datasetIds" ))
102- access_type , accessible_datasets = access_resolution (request , params [3 ], params [4 ], public_datasets ,
103- registered_datasets , controlled_datasets )
108+ access_type , accessible_datasets = access_resolution (request ,
109+ params [3 ], params [4 ],
110+ public_datasets , registered_datasets , controlled_datasets )
104111 if 'mateName' in request or alleleRequest .get ('variantType' ) == 'BND' :
105- datasets = await find_fusion (params [0 ], request .get ("assemblyId" ), requested_position , request .get ("referenceName" ),
112+ datasets = await find_fusion (params [0 ],
113+ request .get ("assemblyId" ), requested_position , request .get ("referenceName" ),
106114 request .get ("referenceBases" ), request .get ('mateName' ),
107115 accessible_datasets , access_type , request .get ("includeDatasetResponses" , "NONE" ))
108116 else :
@@ -122,4 +130,5 @@ async def query_request_handler(params):
122130
123131 if __handover_drs__ :
124132 beacon_response ['beaconHandover' ] = make_handover (__handover_beacon__ , [x ['datasetId' ] for x in datasets ])
133+
125134 return beacon_response
0 commit comments