1111from ..conf import CONFIG_INFO
1212
1313
14- def process_exception_data (request : Dict ,
15- host : str ,
16- error_code : int ,
17- error : str ) -> Dict :
14+ def process_exception_data (request : Dict , host : str , error_code : int , error : str ) -> Dict :
1815 """Return request data as dictionary.
1916
2017 Generates custom exception messages based on request parameters.
2118 """
22- data = {'beaconId' : '.' .join (reversed (host .split ('.' ))),
23- "apiVersion" : __apiVersion__ ,
24- 'exists' : None ,
25- 'error' : {'errorCode' : error_code ,
26- 'errorMessage' : error },
27- 'alleleRequest' : {'referenceName' : request .get ("referenceName" , None ),
28- 'referenceBases' : request .get ("referenceBases" , None ),
29- 'includeDatasetResponses' : request .get ("includeDatasetResponses" , "NONE" ),
30- 'assemblyId' : request .get ("assemblyId" , None )},
31- # showing empty datasetsAlleRsponse as no datasets found
32- # A null/None would represent no data while empty array represents
33- # none found or error and corresponds with exists null/None
34- 'datasetAlleleResponses' : []}
19+ data = {
20+ "beaconId" : "." .join (reversed (host .split ("." ))),
21+ "apiVersion" : __apiVersion__ ,
22+ "exists" : None ,
23+ "error" : {"errorCode" : error_code , "errorMessage" : error },
24+ "alleleRequest" : {
25+ "referenceName" : request .get ("referenceName" , None ),
26+ "referenceBases" : request .get ("referenceBases" , None ),
27+ "includeDatasetResponses" : request .get ("includeDatasetResponses" , "NONE" ),
28+ "assemblyId" : request .get ("assemblyId" , None ),
29+ },
30+ # showing empty datasetsAlleRsponse as no datasets found
31+ # A null/None would represent no data while empty array represents
32+ # none found or error and corresponds with exists null/None
33+ "datasetAlleleResponses" : [],
34+ }
3535 # include datasetIds only if they are specified
3636 # as per specification if they don't exist all datatsets will be queried
3737 # Only one of `alternateBases` or `variantType` is required, validated by schema
38- oneof_fields = ["alternateBases" , "variantType" , "start" , "end" , "startMin" , "startMax" ,
39- "endMin" , "endMax" , "datasetIds" ]
40- data ['alleleRequest' ].update ({k : request .get (k ) for k in oneof_fields if k in request })
38+ oneof_fields = ["alternateBases" , "variantType" , "start" , "end" , "startMin" , "startMax" , "endMin" , "endMax" , "datasetIds" ]
39+ data ["alleleRequest" ].update ({k : request .get (k ) for k in oneof_fields if k in request })
4140
4241 return data
4342
@@ -49,12 +48,11 @@ class BeaconBadRequest(web.HTTPBadRequest):
4948 Used in conjunction with JSON Schema validator.
5049 """
5150
52- def __init__ (self , request : Dict ,
53- host : str , error : str ) -> None :
51+ def __init__ (self , request : Dict , host : str , error : str ) -> None :
5452 """Return custom bad request exception."""
5553 data = process_exception_data (request , host , 400 , error )
5654 super ().__init__ (text = json .dumps (data ), content_type = "application/json" )
57- LOG .error (f' 401 ERROR MESSAGE: { error } ' )
55+ LOG .error (f" 401 ERROR MESSAGE: { error } " )
5856
5957
6058class BeaconUnauthorised (web .HTTPUnauthorized ):
@@ -64,17 +62,21 @@ class BeaconUnauthorised(web.HTTPUnauthorized):
6462 Used in conjunction with Token authentication aiohttp middleware.
6563 """
6664
67- def __init__ (self , request : Dict ,
68- host : str , error : str , error_message : str ) -> None :
65+ def __init__ (self , request : Dict , host : str , error : str , error_message : str ) -> None :
6966 """Return custom unauthorized exception."""
7067 data = process_exception_data (request , host , 401 , error )
71- headers_401 = {"WWW-Authenticate" : f"Bearer realm=\" { CONFIG_INFO .url } \" \n \
72- error=\" { error } \" \n \
73- error_description=\" { error_message } \" " }
74- super ().__init__ (content_type = "application/json" , text = json .dumps (data ),
75- # we use auth scheme Bearer by default
76- headers = headers_401 )
77- LOG .error (f'401 ERROR MESSAGE: { error } ' )
68+ headers_401 = {
69+ "WWW-Authenticate" : f'Bearer realm="{ CONFIG_INFO .url } "\n \
70+ error="{ error } "\n \
71+ error_description="{ error_message } "'
72+ }
73+ super ().__init__ (
74+ content_type = "application/json" ,
75+ text = json .dumps (data ),
76+ # we use auth scheme Bearer by default
77+ headers = headers_401 ,
78+ )
79+ LOG .error (f"401 ERROR MESSAGE: { error } " )
7880
7981
8082class BeaconForbidden (web .HTTPForbidden ):
@@ -85,12 +87,11 @@ class BeaconForbidden(web.HTTPForbidden):
8587 but not granted the resource. Used in conjunction with Token authentication aiohttp middleware.
8688 """
8789
88- def __init__ (self , request : Dict ,
89- host : str , error : str ) -> None :
90+ def __init__ (self , request : Dict , host : str , error : str ) -> None :
9091 """Return custom forbidden exception."""
9192 data = process_exception_data (request , host , 403 , error )
9293 super ().__init__ (content_type = "application/json" , text = json .dumps (data ))
93- LOG .error (f' 403 ERROR MESSAGE: { error } ' )
94+ LOG .error (f" 403 ERROR MESSAGE: { error } " )
9495
9596
9697class BeaconServerError (web .HTTPInternalServerError ):
@@ -101,7 +102,6 @@ class BeaconServerError(web.HTTPInternalServerError):
101102
102103 def __init__ (self , error : str ) -> None :
103104 """Return custom forbidden exception."""
104- data = {'errorCode' : 500 ,
105- 'errorMessage' : error }
105+ data = {"errorCode" : 500 , "errorMessage" : error }
106106 super ().__init__ (content_type = "application/json" , text = json .dumps (data ))
107- LOG .error (f' 500 ERROR MESSAGE: { error } ' )
107+ LOG .error (f" 500 ERROR MESSAGE: { error } " )
0 commit comments