docs: add withings API

This commit is contained in:
Jan De Dobbeleer 2022-07-11 14:41:50 +02:00 committed by Jan De Dobbeleer
parent aee16fd62c
commit 1a03a6d9e8
8 changed files with 140 additions and 7 deletions

View file

@ -1,4 +1,5 @@
const strava = require('../shared/strava.js');
const withings = require('../shared/withings.js');
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
@ -20,7 +21,10 @@ module.exports = async function (context, req) {
switch (segment) {
case "strava":
tokens = await strava.getStravaToken(code);
tokens = await strava.getToken(code);
break;
case "withings":
tokens = await withings.getToken(code);
break;
default:
context.log(`Unknown segment: ${segment}`);

View file

@ -1,4 +1,5 @@
const strava = require('../shared/strava.js');
const withings = require('../shared/withings.js');
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
@ -18,7 +19,10 @@ module.exports = async function (context, req) {
let body = null;
switch (segment) {
case "strava":
body = await strava.refreshStravaToken(refresh_token);
body = await strava.refreshToken(refresh_token);
break;
case "withings":
body = await withings.refreshToken(refresh_token);
break;
default:
context.res = {

View file

@ -1,6 +1,6 @@
const axios = require('axios');
async function getStravaToken(code) {
async function getToken(code) {
const params = {
client_id: process.env['STRAVA_CLIENT_ID'],
client_secret: process.env['STRAVA_CLIENT_SECRET'],
@ -16,7 +16,7 @@ async function getStravaToken(code) {
};
}
async function refreshStravaToken(refresh_token) {
async function refreshToken(refresh_token) {
const params = {
client_id: process.env['STRAVA_CLIENT_ID'],
client_secret: process.env['STRAVA_CLIENT_SECRET'],
@ -33,6 +33,6 @@ async function refreshStravaToken(refresh_token) {
}
module.exports = {
getStravaToken: getStravaToken,
refreshStravaToken: refreshStravaToken,
getToken: getToken,
refreshToken: refreshToken,
}

View file

@ -0,0 +1,42 @@
const axios = require('axios');
async function getToken(code) {
const params = {
client_id: process.env['WITHINGS_CLIENT_ID'],
client_secret: process.env['WITHINGS_CLIENT_SECRET'],
code: code,
grant_type: 'authorization_code',
action: 'requesttoken',
redirect_uri: 'https://ohmyposh.dev',
};
const resp = await axios.post('https://wbsapi.withings.net/v2/oauth2', null, { params: params });
return {
access_token: resp.data.access_token,
refresh_token: resp.data.refresh_token,
expires_in: resp.data.expires_in
};
}
async function refreshToken(refresh_token) {
const params = {
client_id: process.env['WITHINGS_CLIENT_ID'],
client_secret: process.env['WITHINGS_CLIENT_SECRET'],
refresh_token: refresh_token,
grant_type: 'refresh_token',
action: 'requesttoken',
redirect_uri: 'https://ohmyposh.dev',
};
const resp = await axios.post('https://wbsapi.withings.net/v2/oauth2', null, { params: params });
return {
access_token: resp.data.access_token,
refresh_token: resp.data.refresh_token,
expires_in: resp.data.expires_in
};
}
module.exports = {
getToken: getToken,
refreshToken: refreshToken,
}

View file

@ -8,7 +8,7 @@ import StravaConnect from '/img/strava_connect.svg';
## What
[Strava][strava] ia a popular activity tracker for bike, run or any other training.
[Strava][strava] is a popular activity tracker for bike, run or any other training.
To keep up with your training goals it is important to be reminded about it.
An Oh My Posh Strava segment shows your last activity,
and can also indicate by a color if it is time to get away from your computer and get active.

View file

@ -0,0 +1,70 @@
---
id: withings
title: Withings
sidebar_label: Withings
---
import WithingsConnect from '/img/withings.svg';
## What
The [Withings][withings] health ecosystem of connected devices & apps is designed to improve daily wellbeing
and long-term health.
## Accessing your Withings data
To allow Oh My Posh access your Withings data you need to grant access to read your public activities.
This will give you an access and a refresh token. Paste the tokens into your Withings segment configuration.
Click the following link to connect with Withings:
<a href="https://account.withings.com/oauth2_user/authorize2?client_id=93675962e88ddfe53f83c0c900558f72174e0ac70ccfb57e48053530c7e6e494&response_type=code&redirect_uri=https://ohmyposh.dev/api/auth&scope=user.activity,user.metrics&state=withings">
<WithingsConnect className="withings"/>
</a>
## Sample Configuration
```json
{
"type": "withings",
"style": "powerline",
"powerline_symbol": "\uE0B0",
"foreground": "#ffffff",
"background": "#000000",
"template": "{{.Name}} {{.Ago}} {{.Icon}}",
"properties": {
"access_token":"11111111111111111",
"refresh_token":"1111111111111111",
"http_timeout": 1500
}
}
```
## Properties
- access_token: `string` - token from Withings login, see login link in section above. It has the following format: `1111111111111111111111111`
- refresh_token: `string` - token from Withings login, see login link in section above. It has the following format: `1111111111111111111111111`
- expires_in: `int` - the default timeout of the token from the Withings login
- http_timeout: `int` - how long do you want to wait before you want to see your prompt more than your Withings data? - defaults to 500ms
- CacheTimeout: `int` in minutes - How long do you want your numbers cached? - defaults to 5 min
## Template ([info][templates])
:::note default template
``` template
{{ if .Error }}{{ .Error }}{{ else }}{{ .Ago }}{{ end }}
```
:::
### Properties
The properties below are available for use in your template
- `.ID`: `time` - The id of the entry
Now, go out and be active!
[templates]: /docs/configuration/templates
[withings]: http://www.ithings.com/

View file

@ -124,3 +124,15 @@ iframe.youtube {
border-radius: 8px;
margin-top: 25px;
}
.withings {
width: 150px;
}
[data-theme='light'] .withings path {
fill: black;
}
[data-theme='dark'] .withings path {
fill: white;
}

View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 350 44.96"><title>Withings logo - Brandlogos.net</title><path d="M337,490.23h8.3l10.35,32.26,10.71-32.4h6.45l10.71,32.4,10.35-32.26h8l-15.17,43.82h-6.59l-10.64-31.27-10.71,31.27h-6.59Z" transform="translate(-337 -489.52)" /><path d="M413.3,490.23h7.59v43.54H413.3Z" transform="translate(-337 -489.52)" /><path d="M446.34,497.32H432.51v-7.09h35.31v7.09H454.07v36.45h-7.73V497.32Z" transform="translate(-337 -489.52)" /><path d="M479,490.23h7.59v18.08h20.92V490.23h7.59v43.54h-7.59V515.4H486.62v18.37H479Z" transform="translate(-337 -489.52)" /><path d="M529.87,490.23h7.59v43.54h-7.59Z" transform="translate(-337 -489.52)" /><path d="M552.2,490.23h7.09l23.26,30.06V490.23h7.59v43.54h-6.38l-24-31v31H552.2Z" transform="translate(-337 -489.52)" /><path d="M625.1,534.48a24.35,24.35,0,0,1-9.29-1.7,19.82,19.82,0,0,1-7.09-4.75,20.61,20.61,0,0,1-4.54-7.09,24,24,0,0,1-1.63-8.86V512a23.74,23.74,0,0,1,1.63-8.65,21.26,21.26,0,0,1,4.68-7.16,22.2,22.2,0,0,1,16.24-6.67,28.84,28.84,0,0,1,5.18.43,25.69,25.69,0,0,1,4.33,1.13,24,24,0,0,1,7.23,4.4L637,501.29a29.92,29.92,0,0,0-2.55-1.91,17.15,17.15,0,0,0-2.77-1.49,13.38,13.38,0,0,0-3.12-.92,20.51,20.51,0,0,0-3.76-.35,12.19,12.19,0,0,0-5.53,1.28,14.74,14.74,0,0,0-4.47,3.33,15.63,15.63,0,0,0-3,4.89,16.34,16.34,0,0,0-1.06,5.89v.14a17.74,17.74,0,0,0,1.06,6.24,14.17,14.17,0,0,0,3,4.89,13.88,13.88,0,0,0,4.68,3.26,15,15,0,0,0,6,1.21,17.53,17.53,0,0,0,5.81-.92,18.19,18.19,0,0,0,4.68-2.41v-8.15H624.88v-6.67h18.51v18.29a35.35,35.35,0,0,1-3.55,2.62,24,24,0,0,1-4.25,2.2,24.68,24.68,0,0,1-4.89,1.49C628.86,534.34,627,534.48,625.1,534.48Z" transform="translate(-337 -489.52)" /><path d="M671.61,534.41a27.62,27.62,0,0,1-9.64-1.7,26.89,26.89,0,0,1-8.51-5.25l4.61-5.46a27.35,27.35,0,0,0,6.38,4.18,17.23,17.23,0,0,0,7.3,1.49,9.38,9.38,0,0,0,5.6-1.49,4.75,4.75,0,0,0,2.06-4v-.14A4.63,4.63,0,0,0,679,520a4.69,4.69,0,0,0-1.49-1.7,9.69,9.69,0,0,0-3-1.49,42.67,42.67,0,0,0-5-1.35,46.22,46.22,0,0,1-6.17-1.84,17.17,17.17,0,0,1-4.54-2.48A11.4,11.4,0,0,1,656,507.6a12.36,12.36,0,0,1-1-5.11v-.14a11.83,11.83,0,0,1,4.18-9.08,13.51,13.51,0,0,1,4.68-2.55,18.08,18.08,0,0,1,6-.92,24.3,24.3,0,0,1,15.74,5.46l-4.11,5.81a29.35,29.35,0,0,0-5.89-3.33,15.51,15.51,0,0,0-5.89-1.13,8.25,8.25,0,0,0-5.25,1.49,4.41,4.41,0,0,0-1.84,3.69v.14a5.29,5.29,0,0,0,.43,2.2,5.12,5.12,0,0,0,1.56,1.77,17.07,17.07,0,0,0,3.19,1.49c1.35.43,3,.92,5.18,1.42a47.88,47.88,0,0,1,6.1,1.91,15.28,15.28,0,0,1,4.4,2.55,9.85,9.85,0,0,1,2.62,3.55,10.82,10.82,0,0,1,.92,4.68v.14a12.34,12.34,0,0,1-1.13,5.46,12.14,12.14,0,0,1-3.19,4.11,13.39,13.39,0,0,1-4.89,2.55A39.31,39.31,0,0,1,671.61,534.41Z" transform="translate(-337 -489.52)" /></svg>

After

Width:  |  Height:  |  Size: 2.6 KiB