Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
103 lines (91 sloc) 5.87 KB

ihealth connector

About iHealth

iHealth is a company that connecected health devices. The device's data and services are exposed to developers as REST APIs.

Purpose of the scriptr.io connector for iHealth

The purpose of this connector is to simplify and streamline the way you access iHealth' APIs from scriptr.io, by providing you with a few native objects that you can directly integrate into your own scripts. This will hopefully allow you to create sophisticated connected health applications.

Components

  • ihealth/user: this is the main object to interact with. It provides access to the data that is generated by the devices of a given user (the one for who you are passing an access token)
  • ihealth/mappings: configuration file used for internal purposes (constants and descriptions).
  • ihealth/httpclient: generic http client that handles the communication between scriptr.io and iHealth' APIs
  • ihealth/api/handleEvent: default callback invoked by iHealth when sending notifications.
  • ihealth/authorization/getRequestTokenUrl: This script implements steps 1 and 2 of the iHealth OAuth authorization process.
  • ihealth/authorization/getAccessToken: This script implements step 3 of the iHealth OAuth authorization process.
  • ihealth/test/tests.js: a list of examples on how to use the connectors' objects and corresponding methods.

How to use

Obtain access tokens from iHealth

Step 1

From a front-end application, send a request to the modules/ihealth/oauth2/getRequestTokenUrl script. The result returned by the aforementioned script should resemble the following:

>> curl -X POST  -F username=john.doe -H 'Authorization: bearer YOUR_SCRIPTRIO_TOKEN' 'https://api.scriptrapps.io/ihealth/oauth2/getRequestCodeUrl'
{
	"metadata": {
		"requestId": "13a75c1a-b9cd-41bb-811b-ae20dd59b92b",
		"status": "success",
		"statusCode": "200"
	},
	"result": "https://api.ihealthlabs.com:8443/OpenApiV2/OAuthv2/userauthorization/?client_id=0712a59c9d0b4aae88e9750c76b3a91a&response_type=code&APIName=OpenApiActivity%20OpenApiBG%20OpenApiBP%20OpenApiFood%09OpenApiSleep%20OpenApiSpO2%20OpenApiSport%20OpenApiUserInfo%20OpenApiWeight&state=bbc132&redirect_uri=https%3A%2F%2Fapi.scriptr.io%2Fihealth%2Foauth2%2FgetAccessToken%3Fauth_token%3DYOUR_SCRIPTRIO_TOKEN3D%26state%3Dbbc132"
}

Step 2

From the front-end, issue a request to the obtained URL. This redirects your end user to the iHealth login and authorization page, where he has to enter his credentials then authorize the application on the requested scope. Once this is done, iHealth automatically calls back the modules/ihealth/oauth2/getAccessToken script, providing it with access and secret tokens. These ared stored in your scriptr.io's global storage.

Important note

Because iHealth's authentication workflow involves many steps, you might obtain an error at the end of the authentication process such as the below:

{"response": {
  "metadata": {
    "requestId": "54c24cf0-380c-477b-a876-8dcbfe5734bb",
    "status": "success",
    "scriptLog": [
      {
        "timestamp": "2017-02-02 08:26:01.982",
        "level": "log",
        "component": "ihealth/oauth2/getAccessToken",
        "message": "state ://oau"
      }
    ],
    "statusCode": "200"
  },"result": {
 "status": "failure",
 "errorCode": "Inconsistency_Error",
 "errorDetail": "Could not find username undefined in store to store the tokens"
}
}}

If this was the case, kindly execute Step1 and Step2 once again.

Use the connector

In order to use the connector, you need to import the main module: modules/ihealth/user, as described below:

var userModule = require("/modules/iheatlth/user");

Then create a new instance of the User class, defined in this module (we assume that we already otbained an access token for the given user):

var user = new userModule.User({username:"john.doe"});

The User class provides many methods to obtain data related to devices pertaining to the end user, such as:

data.userGetInfo = user.getInfo();
data.listSPO2Measures = user.listSPO2Measures();
data.listBloodPressureMeasures = user.listBloodPressureMeasures({from:new Date("2016-04-01").getTime()});
data.listActivities = user.listActivities();
data.getActivityAtDate = user.getActivityAtDate({date:new Date("2016-04-05").getTime()});
data.listWeightMeasures = user.listWeightMeasures({from:new Date("2016-04-05").getTime(), to:new Date().getTime()});
data.listFatFreeMassMeasures = user.listFatFreeMassMeasures();
data.listFatRatioMeasures = user.listFatRatioMeasures();
data.listFatMassMeasures = user.listFatMassMeasures();
data.listDiastolicBloodPressureMeasures = user.listDiastolicBloodPressureMeasures();
data.listWorkoutMeasures = user.listWorkoutMeasures();
data.listSleepMeasures = user.listSleepMeasures();

About notifications

As mentioned, you can subscribe to notifications that are emitted by a given device for the concerned user. By default, iHealth is instructed by the connector to send notifications to the https://api.scriptrapps.io/modules/ihealth/api/handleNotifications.js API that resides in your account. This API's job is to trigger the handler that is configured to process the corresponding notification type.