Google App Engine is one of the easiest ways to get an application using the Healthx API up and running. You build and maintain your application, but host and serve it through Google's infrastructure.
Once you've got App Engine chugging along, it's time to make your first Healthx Hello World application. In the first part of this tutorial, we walk you through setting up your application within the Google App Engine framework. If you are already familiar with the App Engine framework and want to skip right to the Healthx APIs, click here. By the time you're finished, you'll have a fully functional application, written in Python, that retrieves data from the Healthx API. Here is what it will look like.
Create a new directory named HxElig. The files for your Healthx Eligibility Hello World application will reside in this directory. You'll create two files in this directory, named app.py and app.yaml. Create these two skeleton files first to test your setup, you'll build up into a full Healthx application from there.
First create the app.py file. Give this file the following contents.
The first two lines import the necessary parts of the Google Webapp framework. You'll add to these lines later when accessing the Healthx data. The MainPage class is the code that gets executed when the "/" URL recieves an HTTP GET request. This mapping is defined in the webapp.WSGIApplication instance. Finally , the main() function starts the application.
Next, create the app.yaml file. Give this file the following contents.
The attributes of the app.yaml file are exactly as defined in the Google App Engine app.yaml reference.
Test your first application by launching it from the Google App Engine launcher that comes with Google App Engine. Passing in the port the appserver will listen on is optional. By default the appserver listens on port 8080.
Select your application and click Browse. This will open up a web browser where you can view your application.
You should see a message that reads "Healthx Eligibility Here I Come!". If you see this message you have set up Google App Engine correctly and are ready to make some simple queries to the Healthx API.
The Healthx API is RESTful, which means you build requests to the API using standard HTTP commands such as GET and POST to persistent URLs. Each API function is called by making an OAuth request to a unique URL. There are several libraries available for OAuth, including one for Python. Read more about the Healthx OAuth implementation here. For this sample, you'll use the eligibilityv1/getFamily function. The URL for this request looks like this.
This URL breaks down into a few relevant parts. First, the base path http://qa.healthx.com/API/. This is the standard path to the sandbox Healthx API. The production API lives at a similar subdomain, such as
Add the following lines to the top of your app.py file.
In order to import OAuth Python library, you'll need to download it from here. Place your oauth.py file in the root of the directory you created earlier, at the same level as your app.yaml file.
Add a few constants to the page. You'll get these constants when you sign up for a Healthx Developer account. You are welcome to use these for now, or input your own. If your application uses certificates, you'll have a slightly different set of credentials.
Modify the application definition to deal with the new classes we are about to create. It should look like this:
Edit the MainPage class to look like the below. This main page doesn't do anything interesting until the user has upgraded a Request Token to an Authorization Token (OAuth steps 1 and 2). We'll take care of the first two steps right after this.
Once those first two OAuth steps are complete, this page adds a form into which you can input your search parameters after the user has logged in to the Healthx site and the Healthx site has redirected the user's browser to this page.
Now, add an OAuthPage method. This OAuth page will do the work of fetching a Request Token, then passing the user over to the login screen where that user will provide his or her credentials.
Here, OAuth steps 1 and 2 take place. First, your application requests a request token from Healthx. Then your application exchanges that request token for an authorization token, redirecting the user to the Healthx authorization page. We save the token and secret so that we can use them later, so we need to create a model class. For more on the model class, refer to Google App Engine's documentation.
You can see in the OAuthPage class, as well as the MainPage class, we store these AppAuthTokens using the
self.response.headers['Content-Type'] = 'text/html'
The GetElig class exchanges the Authorization Token for an Access Token, then uses the Access Token to request access to protected resources, in this case, the user's eligibility record. This request must be accompanied by an instance ID on the header.
Finally, because simplejson correctly interprets NULL values as None, we must add a special formatting function for pretty printing.
To test your application, login with as a user subscribed to a service within the appropriate organization. Do not attempt to use your developer account, it is unlikely to be subscribed to the appropriate organization. For the APP_ID in this sample application, you can login with this user:PWD: euclid_01
When you click the button in your application, your code will fetch the data from the Healthx API and should return a series of name value pairs formatted into a nice table.
The sample uses inline markup, but you could also use the django framework, which works well with Google App Engine, to manipulate these results. If you did not get these results, here are some troubleshooting steps:
Upload your application according to Google's instructions. You may first need to create an application at http://appengine.google.com before you can upload it. Use appcfg.py from the command line to begin the upload or the Google App Engine Launcher. You will be prompted for your username and password.
Congratulations, you've already made your first Healthx API application! Next, take a look at our developer documentation for more information on how exactly to use the Healthx API.