@@ -59,89 +59,95 @@ For application registration and client id and client secret, please refer to:
5959
6060## Sample Usages: Obtain Access Grant, Probe Scope, and Access Data <a name =" usages " ></a >
6161
62- Below are psuedo code snippets showing SDK used with node express server.
62+ Below are psuedo code snippets showing SDK used with python server and flask .
6363
6464```
6565
66- import express, { Request, Response } from 'express';
67-
68- const app = express();
69-
70- const bb = new BlueButton();
71- const authData = bb.generateAuthData();
72-
73- // AuthorizationToken holds access grant info:
74- // access token, expire in, expire at, token type, scope, refreh token, etc.
75- // it is associated with current logged in user in real app,
76- // check SDK js docs for more details.
77-
78- let authToken: AuthorizationToken;
79-
80- // start authorize flow: response with URL to redirect to Medicare.gov beneficiary login
81- app.get('/', (req, res) => {
82- const redirectUrl = bb.generateAuthorizeUrl(authData);
83- res.redirect(redirectUrl);
84- })
85-
86- // oauth2 call back: obtain access token, optionally check scope, and fetch data
87- app.get('api/bluebutton/callback', async (req: Request, res: Response) => {
88-
89- let results = {};
90- try {
91- authToken = await bb.getAuthorizationToken(authData, req.query.code, req.query.state, req.query.error);
92- // now access token obtained, note, during authorization, the beneficiary can grant
93- // access to his/her demographic data and claims data or only claims data, check the scope
94- // of the current access token as shown below:
95- const scopes: string[] = authToken.scope;
96- // iterate scope entries here or check if a permission is in the scope
97- if (authToken.scope.index("patient/Patient.read") > -1) {
98- // patient info access granted
99- }
100-
101- /**
102- * 1. access token scope where demagraphic info included:
103- *
104- * scope: [
105- * "patient/Coverage.read",
106- * "patient/ExplanationOfBenefit.read",
107- * "patient/Patient.read",
108- * "profile",
109- * ]
110- *
111- * 2. access token scope where demagraphic info not included:
112- *
113- * scope: [
114- * "patient/Coverage.read",
115- * "patient/ExplanationOfBenefit.read",
116- * ]
117- */
118-
119- // data flow: after access granted
120- // the app logic can fetch the beneficiary's data in app specific ways:
121- // e.g. download EOB periodically etc.
122- // access token can expire, SDK automatically refresh access token when that happens.
123- eobResults = await bb.getExplanationOfBenefitData(authToken);
124- patientResults = await bb.getPatientData(authToken);
125- coverageResults = await bb.getCoverageData(authToken);
126- profileResults = await bb.getProfileData(authToken);
127-
128- results = {
129- eob: eobResults.response.data,
130- patient: patientResults.response.data,
131- coverage: coverageResults.response.data,
132- profile: profileResults.response.data
133- }
134-
135- authToken = profileResults.token;
136-
137- } catch (e) {
138- console.log(e);
66+ from flask import Flask
67+ from flask import redirect, request
68+ from cms_bluebutton import BlueButton, AuthorizationToken
69+
70+ # initialize the app
71+ app = Flask(__name__)
72+
73+ bb = BlueButton()
74+ # auth_data is saved for the current user
75+ auth_data = bb.generate_auth_data()
76+
77+ # AuthorizationToken holds access grant info:
78+ # access token, expire in, expire at, token type, scope, refreh token, etc.
79+ # it is associated with current logged in user in real app,
80+ # check SDK python docs for more details.
81+
82+ auth_token = None
83+
84+ # start authorize flow: response with URL to redirect to Medicare.gov beneficiary login
85+ @app.route("/", methods=["GET"])
86+ def get_auth_url():
87+ return bb.generate_authorize_url(auth_data)
88+
89+
90+ @app.route('/api/bluebutton/callback/', methods=['GET'])
91+ def authorization_callback():
92+ request_query = request.args
93+ code = request_query.get('code')
94+ state = request_query.get('state')
95+
96+ auth_token = bb.get_authorization_token(auth_data, code, state)
97+
98+ # now access token obtained, note, during authorization, the beneficiary can grant
99+ # access to his/her demographic data and claims data or only claims data, check the scope
100+ # of the current access token as shown below:
101+
102+ scopes = auth_token.scope;
103+
104+ # iterate scope entries here or check if a permission is in the scope
105+ if (scopes.index("patient/Patient.read") > -1) {
106+ // patient info access granted
139107 }
140108
141- res.json(results)
109+ # 1. access token scope where demagraphic info included:
110+ #
111+ # scope: [
112+ # "patient/Coverage.read",
113+ # "patient/ExplanationOfBenefit.read",
114+ # "patient/Patient.read",
115+ # "profile",
116+ # ]
117+ #
118+ # 2. access token scope where demagraphic info not included:
119+ #
120+ # scope: [
121+ # "patient/Coverage.read",
122+ # "patient/ExplanationOfBenefit.read",
123+ # ]
124+
125+ config = {
126+ "auth_token": auth_token,
127+ "params": {},
128+ "url": "to be overriden"
129+ }
130+
131+ result = {}
132+
133+ # fetch eob, patient, coverage, profile
134+ try:
135+ eob_data = bb.get_explaination_of_benefit_data(config)
136+ result['eob_data'] = eob_data['response'].json()
137+ pt_data = bb.get_patient_data(config)
138+ result['patient_data'] = pt_data['response'].json()
139+ coverage_data = bb.get_coverage_data(config)
140+ result['coverage_data'] = coverage_data['response'].json()
141+ profile_data = bb.get_profile_data(config)
142+ result['profile_data'] = profile_data['response'].json()
143+ except Exception as ex:
144+ print(ex)
145+
146+ return result
142147
143- });
144148
149+ if __name__ == '__main__':
150+ app.run(debug=True, host='0.0.0.0', port=3001)
145151```
146152
147153## A Complete Sample App <a name =" samples " ></a >
0 commit comments