1- # views.py
1+ import json
22
33from flask import redirect , request
4- import requests
54from ..data .Database import *
65from . import app
76from ..entities .Settings import Settings
8- from ..utils .configUtil import getConfigSettings
9- from ..utils .bb2Util import generateAuthorizeUrl , getAccessToken , getBenefitData
10- from ..utils .userUtil import clearBB2Data , getLoggedInUser
7+ from ..utils .config_util import get_config_settings
8+ from ..utils .bb2_util import generate_authorize_url , get_access_token , get_benefit_data
9+ from ..utils .user_util import clear_bb2_data , get_loggedin_user
1110from ..shared .LoggerFactory import LoggerFactory
12- import json
1311
1412"""
1513This is the location of all the routes, via the port specified in the config, that allows the
2018
2119# initialize the logger object
2220myLogger = LoggerFactory .get_logger (log_file = __name__ ,log_level = 'DEBUG' )
23- loggedInUser = getLoggedInUser ()
21+ loggedInUser = get_loggedin_user ()
2422
2523#########################################################################################
2624# Test route
2725#########################################################################################
2826@app .route ('/' ,methods = ['GET' ])
29- def verifyPortListening ():
27+ def verify_port_listening ():
3028 return 'Listening on Port 3001 for the Server!'
3129
3230#########################################################################################
3331# Authorize routes
3432#########################################################################################
3533
3634@app .route ('/api/authorize/authurl' ,methods = ['GET' ])
37- def getAuthUrl ():
35+ def get_auth_url ():
3836 """ DEVELOPER NOTE:
3937 * to utilize the latest security features/best practices
4038 * it is recommended to utilize pkce
4139 """
4240 # get configuration and settings
43- myEnv = request .args .get ('env' ) or 'development'
44- myVersion = request .args .get ('version' ) or 'v2'
41+ my_env = request .args .get ('env' ) or 'development'
42+ my_version = request .args .get ('version' ) or 'v2'
4543 PKCE = request .args .get ('pkce' ) or True
46-
47- settings = Settings (myEnv ,myVersion ,PKCE )
48-
49- configSettings = getConfigSettings (myEnv )
50- authUrl = generateAuthorizeUrl (settings , configSettings )
51- return authUrl
44+ return generate_authorize_url (Settings (my_env , my_version , PKCE ), get_config_settings (my_env ))
5245
5346@app .route ('/api/authorize/currentAuthToken' ,methods = ['GET' ])
54- def getCurrentAuthToken ():
47+ def get_current_auth_token ():
5548 return loggedInUser .get ('authToken' )
5649
5750@app .route ('/api/bluebutton/callback/' ,methods = ['GET' ])
58- def authorizationCallback ():
51+ def authorization_callback ():
5952 try :
60- requestQuery = request .args
53+ request_query = request .args
6154
62- if (requestQuery .get ('error' ) == BENE_DENIED_ACCESS ):
55+ if (request_query .get ('error' ) == BENE_DENIED_ACCESS ):
6356 # clear all saved claims data since the bene has denied access for the application
64- clearBB2Data ()
57+ clear_bb2_data ()
6558 myLogger .error ('Beneficiary denied application access to their data' )
6659 return redirect ('http://localhost:3000' )
6760
68- if (requestQuery .get ('code' ) == '' ):
61+ if (request_query .get ('code' ) == '' ):
6962 myLogger .error ('Response was missing access code!' )
70- if (DBsettings .pkce and requestQuery .get ('state' )):
63+ if (DBsettings .pkce and request_query .get ('state' )):
7164 myLogger .error ('State is required when using PKCE' )
7265
7366 # get configuration and settings
74- myEnv = requestQuery .get ('env' ) or 'development'
75- myVersion = requestQuery .get ('version' ) or 'v2'
76- PKCE = requestQuery .get ('pkce' ) or True
67+ my_env = request_query .get ('env' ) or 'development'
68+ my_version = request_query .get ('version' ) or 'v2'
69+ PKCE = request_query .get ('pkce' ) or True
7770
78- settings = Settings (myEnv , myVersion , PKCE )
71+ settings = Settings (my_env , my_version , PKCE )
7972
80- configSettings = getConfigSettings ( myEnv )
73+ config_settings = get_config_settings ( my_env )
8174
8275 # this gets the token from Medicare.gov once the 'user' authenticates their Medicare.gov account
83- authToken = getAccessToken ( requestQuery .get ('code' ),requestQuery .get ('state' ),configSettings = configSettings , settings = settings )
76+ auth_token = get_access_token ( request_query .get ('code' ), request_query .get ('state' ), config_settings = config_settings , settings = settings )
8477
8578 """DEVELOPER NOTES:
8679 * This is where you would most likely place some type of
@@ -90,26 +83,34 @@ def authorizationCallback():
9083 * Here we are however, just updating the loggedInUser we pulled from our MockDb, but we aren't persisting that change
9184 * back into our mocked DB, normally you would want to do this
9285 """
93-
94- #Here we are grabbing the mocked 'user' for our application
86+
87+ # Here we are grabbing the mocked 'user' for our application
9588 # to be able to store the access token for that user
9689 # thereby linking the 'user' of our sample applicaiton with their Medicare.gov account
9790 # providing access to their Medicare data to our sample application
98- loggedInUser .update ({'authToken' :authToken })
99-
100- """ DEVELOPER NOTES:
101- * Here we will use the token to get the EoB data for the mocked 'user' of the sample application
102- * then to save trips to the BB2 API we will store it in the mocked db with the mocked 'user'
103- *
104- * You could also request data for the Patient endpoint and/or the Coverage endpoint here
105- * using similar functionality
106- """
107- eobData = getBenefitData (settings = settings ,configsSettings = configSettings ,query = requestQuery ,loggedInUser = loggedInUser )
108-
109- if (eobData != None and eobData != '' ):
110- loggedInUser .update ({'eobData' :json .dumps (eobData )})
91+ if auth_token and auth_token .get ('expires_at' ) is not None :
92+ loggedInUser .update ({'authToken' : auth_token })
93+
94+ """ DEVELOPER NOTES:
95+ * Here we will use the token to get the EoB data for the mocked 'user' of the sample application
96+ * then to save trips to the BB2 API we will store it in the mocked db with the mocked 'user'
97+ *
98+ * You could also request data for the Patient endpoint and/or the Coverage endpoint here
99+ * using similar functionality
100+ """
101+
102+ eob_data = get_benefit_data (settings = settings ,configs_settings = config_settings , query = request_query , logged_in_user = loggedInUser )
103+
104+ if eob_data :
105+ if eob_data .get ('entry' , None ) is not None :
106+ loggedInUser ['eobData' ] = eob_data
107+ else :
108+ # error or malformed bundle, send generic error message to client
109+ loggedInUser .update ({'eobData' : {'message' : 'Unable to load EOB Data - fetch FHIR resource error.' }})
111110 else :
112- loggedInUser .update ({'eobData' :json .dumps ('Unable to load EOB Data!' )})
111+ clear_bb2_data ()
112+ # send generic error message to FE
113+ loggedInUser .update ({'eobData' : {'message' : 'Unable to load EOB Data - authorization failed.' }})
113114
114115 except BaseException as err :
115116 """DEVELOPER NOTES:
@@ -135,10 +136,8 @@ def authorizationCallback():
135136* DB you would choose to use
136137"""
137138@app .route ('/api/data/benefit' ,methods = ['GET' ])
138- def getPatientEOB ():
139- if (loggedInUser != None
140- and loggedInUser .get ('eobData' ) != None
141- and loggedInUser .get ('eobData' ) != '' ):
142- return json .loads (loggedInUser .get ('eobData' ))
139+ def get_patient_eob ():
140+ if loggedInUser and loggedInUser .get ('eobData' ):
141+ return loggedInUser .get ('eobData' )
143142 else :
144- return ''
143+ return {}
0 commit comments