Skip to content

Commit

Permalink
Merge pull request #1 from vikramezhil/WellnessAPI
Browse files Browse the repository at this point in the history
Wellness api
  • Loading branch information
vikramezhil committed Nov 23, 2018
2 parents 7d48690 + 295bc13 commit 2533637
Show file tree
Hide file tree
Showing 17 changed files with 648 additions and 313 deletions.
131 changes: 33 additions & 98 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ Human API Cordova Plugin for Android & IOS

cordova plugin add cordova-plugin-humanapi


<b><h2>Documentation</h2></b>

For a detailed documentation 📔, please have a look at the [Wiki](https://github.com/vikramezhil/cordova-plugin-humanapi/wiki).
Expand All @@ -28,24 +27,26 @@ For a detailed documentation 📔, please have a look at the [Wiki](https://gith

<b><h2>Usage</h2></b>

<b><h3>Authentication</h3></b>

<b>Cordova, Ionic 1</b>

window.plugin.humanapi.trigger("init", "CLIENT_ID", "CLIENT_SECRET", "USER_ID", "PUBLIC_TOKEN", "ACCESS_TOKEN", function(result) {
alert("Result = " + JSON.stringify(result))
window.plugin.humanapi.auth("CLIENT_ID", "CLIENT_SECRET", "USER_ID", "PUBLIC_TOKEN", "ACCESS_TOKEN", function(result) {
alert("Result = " + result)
}, function(error) {
alert("Error = " + JSON.stringify(error))
alert("Error = " + error)
})

<b>Ionic 2+</b>

var humanAPI: any = window
humanAPI.plugin.humanapi.trigger("init", "CLIENT_ID", "CLIENT_SECRET", "USER_ID", "PUBLIC_TOKEN", "ACCESS_TOKEN", (result) => {
alert("Result = " + JSON.stringify(result))
humanAPI.plugin.humanapi.auth("CLIENT_ID", "CLIENT_SECRET", "USER_ID", "PUBLIC_TOKEN", "ACCESS_TOKEN", (result) => {
alert("Result = " + result)
}, (error) => {
alert("Error = " + JSON.stringify(error))
alert("Error = " + error)
})

<b><h2>Plugin Data Format</h2></b>
<b>Authentication Data Format</b>

{
clientID: "CLIENT_ID",
Expand All @@ -54,97 +55,31 @@ For a detailed documentation 📔, please have a look at the [Wiki](https://gith
humanID: "HUMAN_ID",
sessionToken: "SESSION_TOKEN",
publicToken: "PUBLIC_TOKEN",
accessToken: "ACCESS_TOKEN"
humanAPIData: "REFER BELOW",
accessToken: "ACCESS_TOKEN",
pluginMsg: "The plugin message if any"
}

<b>humanAPIData</b> example data, more information can be found [here](https://reference.humanapi.co/v2.1/docs/data-overview)
<b><h3>Wellness Data</h3></b>

{
"userId": "52e20cb2fff56aac62000001",
"createdAt": "2014-01-24T06:48:18.361Z",
"bloodGlucose": {
"id": "52e20cb3fff56aac6200044a",
"timestamp": "2014-01-23T22:48:19.858Z",
"source": "glooko",
"value": 90,
"unit": "mg/dL"
},
"bloodOxygen": {
"id": "54d1f018810a5ba429951b07",
"timestamp": "2015-01-26T06:58:37.000Z",
"source": "withings",
"value": 99,
"unit": "SpO2"
},
"bloodPressure": {
"id": "550b8a8e834dd16f259683b1",
"userId": "52e20cb2fff56aac62000001",
"timestamp": "2015-03-19T22:48:26.000Z",
"source": "ihealth",
"systolic": 117,
"diastolic": 76,
"unit": "mmHg",
"heartRate": 66
},
"bmi": {
"id": "5702fed82add5c0900a6f95a",
"timestamp": "2016-04-05T06:59:59.000Z",
"source": "fitbit",
"value": 23.98,
"unit": "kg/m2"
},
"bodyFat": {
"id": "562b08ef74a7be0700a0f88d",
"timestamp": "2015-10-24T03:30:34.000Z",
"source": "fitbit",
"value": 21.4,
"unit": "%"
},
"height": {
"id": "57b531d4856bfe0a00d4cb25",
"timestamp": "2016-08-18T03:56:04.156Z",
"source": "fitbit",
"value": 1730,
"unit": "mm"
},
"heartRate": {
"id": "550b8a8e834dd16f259683b2",
"timestamp": "2015-03-19T22:48:26.000Z",
"source": "ihealth",
"value": 66,
"unit": "bpm"
},
"weight": {
"id": "5702fed8dfb2210c00af7405",
"timestamp": "2016-04-05T06:59:59.000Z",
"source": "fitbit",
"value": 71.7,
"unit": "kg"
},
"activitySummary": {
"id": "5812b6e19e554b0800d8a1f6",
"date": "2016-10-28",
"duration": 0,
"distance": 0,
"sedentary": 954,
"light": 0,
"moderate": 0,
"vigorous": 0,
"total": 954,
"steps": 0,
"calories": 1113,
"source": "fitbit"
},
"sleepSummary": {
"id": "540e96b74247c3ea71d671b9",
"userId": "52e20cb2fff56aac62000001",
"date": "2014-09-09",
"source": "fitbit",
"timeAsleep": 456,
"timeAwake": 0,
"updatedAt": "2014-09-11T02:28:27.377Z"
},
"humanId": "5dc2527186aaf9de560e5841f1a44bd6"
}
<b>Cordova, Ionic 1</b>

window.plugin.humanapi.execute("WELLNESS_NAME", "ACCESS_TOKEN", function(result) {
alert("Result = " + result)
}, function(error) {
alert("Error = " + error)
})

<b>Ionic 2+</b>

var humanAPI: any = window
humanAPI.plugin.humanapi.execute("WELLNESS_NAME", "ACCESS_TOKEN", (result) => {
alert("Result = " + result)
}, (error) => {
alert("Error = " + error)
})

<b>Wellness Supported API's & Data Format</b>

Currently the plugin supports 17 wellness API's (excluding Genetic Traits & Genotypes). Please refer for the list of wellness API's and data formats [here](https://reference.humanapi.co/v2.1/reference#wellness-api-introduction)

For plugin examples, please refer [here](https://github.com/vikramezhil/cordova-plugin-humanapi/wiki/6.-API-calls)
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-humanapi",
"version": "1.0.0",
"version": "1.0.1",
"description": "Human API Plugin for Android and IOS",
"cordova": {
"id": "cordova-plugin-humanapi",
Expand All @@ -18,13 +18,16 @@
"cordova-android",
"cordova-ios",
"cordova",
"cordova-plugin",
"ionic",
"ionic-plugin",
"android",
"ios",
"human",
"humanapi",
"human api",
"human-api"
"human-api",
"humanapi-plugin"
],
"author": "Vikram Ezhil <vikram.ezhil.1988@gmail.com>",
"license": "Apache 2.0",
Expand Down
5 changes: 5 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<source-file src="src/android/res/layout/activity_human.xml" target-dir="res/layout" />
<source-file src="src/android/human/HumanAPIActivity.java" target-dir="src/com/github/vikramezhil/cordova/human" />
<source-file src="src/android/human/HumanAPIModel.java" target-dir="src/com/github/vikramezhil/cordova/human" />
<source-file src="src/android/human/HumanAPIListener.java" target-dir="src/com/github/vikramezhil/cordova/human" />
<source-file src="src/android/human/HumanAPIProperties.java" target-dir="src/com/github/vikramezhil/cordova/human" />
<source-file src="src/android/human/HumanAPIService.java" target-dir="src/com/github/vikramezhil/cordova/human" />
</platform>

<platform name="ios">
Expand Down Expand Up @@ -74,5 +77,7 @@
<!-- Human API -->
<source-file src="src/ios/human/HumanAPIVC.swift" target-dir="src/ios/human" />
<source-file src="src/ios/human/HumanAPIModel.swift" target-dir="src/ios/human" />
<source-file src="src/ios/human/HumanAPIProperties.swift" target-dir="src/ios/human" />
<source-file src="src/ios/human/HumanAPIService.swift" target-dir="src/ios/human" />
</platform>
</plugin>
38 changes: 27 additions & 11 deletions src/android/HumanAPIPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import android.content.Intent;

import com.github.vikramezhil.cordova.human.HumanAPIActivity;
import com.github.vikramezhil.cordova.human.HumanAPIActivity.HumanAPIListener;
import com.github.vikramezhil.cordova.human.HumanAPIService;
import com.github.vikramezhil.cordova.human.HumanAPIListener;

/**
* HumanAPIPlugin Android Controller Class
Expand All @@ -27,9 +28,14 @@ public class HumanAPIPlugin extends CordovaPlugin implements HumanAPIListener {

public static HumanAPIPlugin context;

private HumanAPIService humanAPIService;

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
context = this;

// Initializing the human API service
humanAPIService = new HumanAPIService(cordova.getActivity().getApplicationContext(), this);
}

@Override
Expand All @@ -44,16 +50,22 @@ public void onDestroy() {

//*************************************************************************************

// Active8mePlugins
// Plugin Executions

//*************************************************************************************

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
Log.wtf(TAG, "Native Action = " + action);

if (action.equals("trigger")) {
this.trigger(callbackContext, args.getString(0), args.getString(1), args.getString(2), args.getString(3), args.getString(4), args.getString(5));
if (action == null || action.isEmpty()) {
return false;
} else if (action.equals("auth")) {
this.auth(callbackContext, args.getString(0), args.getString(1), args.getString(2), args.getString(3), args.getString(4));

return true;
} else if (action.equals("execute")) {
this.humanAPIService.execute(callbackContext, args.getString(0), args.getString(1));

return true;
}
Expand All @@ -70,17 +82,15 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
private CallbackContext humanAPICallbackContext;

/**
* Triggers Human API
* Triggers Human API Authentication
*
* @param callbackContext The callback context
* @param triggerType The human API trigger type
* @param clientID The human API client ID
* @param clientSecret The human API client secret
* @param userID The human API user ID
* @param publicToken The human API public token
* @param accessToken The human API access token
*/
private void trigger(CallbackContext callbackContext, String triggerType, String clientID, String clientSecret, String userID, String publicToken, String accessToken) {
private void auth(CallbackContext callbackContext, String clientID, String clientSecret, String userID, String publicToken, String accessToken) {
final CordovaInterface cordova = this.cordova;

// Saving a reference of the human API callback context
Expand All @@ -106,11 +116,17 @@ public void run() {
// MARK: HumanAPIListener Methods

@Override
public void onHumanAPIUpdate(Boolean success, String humanAPIData) {
public void onHumanAPIUpdate(CallbackContext callbackContext, Boolean success, String humanAPIData) {
PluginResult result = new PluginResult(success ? PluginResult.Status.OK : PluginResult.Status.ERROR, humanAPIData);
result.setKeepCallback(true);

if(humanAPICallbackContext != null) {
PluginResult result = new PluginResult(success ? PluginResult.Status.OK : PluginResult.Status.ERROR, humanAPIData);
result.setKeepCallback(true);
humanAPICallbackContext.sendPluginResult(result);
humanAPICallbackContext = null;
} else if(callbackContext != null) {
callbackContext.sendPluginResult(result);
} else {
Log.wtf(TAG, "Unable to send back data, callbackContext is null");
}
}
}
43 changes: 12 additions & 31 deletions src/android/human/HumanAPIActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import org.json.JSONObject;

import org.apache.cordova.PluginResult;
import org.apache.cordova.CallbackContext;

import com.github.vikramezhil.cordova.HumanAPIPlugin;
Expand All @@ -35,16 +34,6 @@ public class HumanAPIActivity extends Activity {

private HumanAPIListener humanAPIListener;

public interface HumanAPIListener {
/**
* Sends an update with the human API data
*
* @param success The success status
* @param humanAPIData The human API response data
*/
void onHumanAPIUpdate(Boolean success, String humanAPIData);
}

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -73,12 +62,12 @@ protected void onCreate(Bundle savedInstanceState) {

service = new Service(this, new ServiceListener() {
@Override
public void onServiceRunning(String passBackKey, Boolean serviceRunning) {
public void onServiceRunning(CallbackContext callbackContext, String passBackKey, Boolean serviceRunning) {
Log.wtf(TAG, "Service running status for " + passBackKey + " = " + serviceRunning);
}

@Override
public void onServiceResponse(String passBackKey, JSONObject serviceResponse) {
public void onServiceResponse(CallbackContext callbackContext, String passBackKey, String serviceResponse) {
if(passBackKey.equals(HumanAPIModel.humanAPITokenPassBackKey)) {
// Setting the tokens in the model data
humanAPIModel.setTokens(serviceResponse);
Expand All @@ -91,30 +80,22 @@ public void onServiceResponse(String passBackKey, JSONObject serviceResponse) {
Log.wtf(TAG, "publicToken = " + humanAPIModel.getPublicToken());
Log.wtf(TAG, "accessToken = " + humanAPIModel.getAccessToken());

// Sending back the human API data update
if(humanAPIModel.getAccessToken() == null) {
humanAPIListener.onHumanAPIUpdate(false, humanAPIModel.getHumanAPIHybridData("Access token is null, unable to proceed"));

finish();
} else {
// Getting the user human connected health data
service.get(HumanAPIModel.humanAPIDataPassBackKey, humanAPIModel.getDataURL(), humanAPIModel.getDataHeader());
humanAPIListener.onHumanAPIUpdate(callbackContext, false, humanAPIModel.getHumanAPIHybridTokensData("Access token is null"));
} else {
humanAPIListener.onHumanAPIUpdate(callbackContext, true, humanAPIModel.getHumanAPIHybridTokensData("Human API tokens got"));
}
} else if(passBackKey.equals(HumanAPIModel.humanAPIDataPassBackKey)) {
// Setting the human data in the model
humanAPIModel.setHumanData(serviceResponse);

// Sending back the human API data update
humanAPIListener.onHumanAPIUpdate(true, humanAPIModel.getHumanAPIHybridData("Human API data got"));

finish();
}

finish();
}

@Override
public void onServiceError(String passBackKey, String errorMessage) {
public void onServiceError(CallbackContext callbackContext, String passBackKey, String errorMessage) {
Log.wtf(TAG, "Service error for " + passBackKey + " = " + errorMessage);

humanAPIListener.onHumanAPIUpdate(false, humanAPIModel.getHumanAPIHybridData(errorMessage));
humanAPIListener.onHumanAPIUpdate(callbackContext, false, humanAPIModel.getHumanAPIHybridTokensData(errorMessage));

finish();
}
Expand All @@ -134,11 +115,11 @@ public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request
humanAPIModel.setHumanAPIResponse(request.getUrl().toString());

// Getting the human API public token for the user
service.post(HumanAPIModel.humanAPITokenPassBackKey, humanAPIModel.getTokenURL(), null, humanAPIModel.getTokenRequestBody());
service.post(null, HumanAPIModel.humanAPITokenPassBackKey, humanAPIModel.getTokenURL(), null, humanAPIModel.getTokenRequestBody());

return true;
} else if(request.getUrl().toString().startsWith(humanAPIModel.getCloseURL())) {
humanAPIListener.onHumanAPIUpdate(true, humanAPIModel.getHumanAPIHybridData("Human API page closed by user"));
humanAPIListener.onHumanAPIUpdate(null, true, humanAPIModel.getHumanAPIHybridTokensData("Human API page closed by user"));

finish();

Expand Down

0 comments on commit 2533637

Please sign in to comment.