|
| 1 | +var assign = require('object-assign'); |
| 2 | +var request = require('request-promise'); |
| 3 | + |
| 4 | +var config = require('./config'); |
| 5 | + |
| 6 | +module.exports = { |
| 7 | + createRequestPromise: createRequestPromise, |
| 8 | + buildTimeSeriesOptions: buildTimeSeriesOptions, |
| 9 | + buildIntradayTimeSeriesOptions: buildIntradayTimeSeriesOptions |
| 10 | +}; |
| 11 | + |
| 12 | +function createRequestPromise(options) { |
| 13 | + return request.get({ |
| 14 | + url: options.url, |
| 15 | + method: 'GET', |
| 16 | + json: true, |
| 17 | + headers: { |
| 18 | + Authorization: 'Bearer ' + options.access_token |
| 19 | + } |
| 20 | + }).then(function(res) { |
| 21 | + res.requestedResource = options.resourcePath.replace('/', '-'); |
| 22 | + return res; |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +function buildTimeSeriesOptions(options) { |
| 27 | + var url = config.FITBIT_BASE_API_URL + '/1/user/{userId}/{resourcePath}/date/{baseDate}/{period}.json'; |
| 28 | + |
| 29 | + options = assign({ |
| 30 | + userId: '-', |
| 31 | + resourcePath: 'activities/steps', |
| 32 | + baseDate: 'today', |
| 33 | + period: '1d' |
| 34 | + }, options); |
| 35 | + |
| 36 | + if (options.endDate) { |
| 37 | + delete options.period; |
| 38 | + } |
| 39 | + |
| 40 | + options.url = url.replace('{userId}', options.userId) |
| 41 | + .replace('{resourcePath}', options.resourcePath) |
| 42 | + .replace('{baseDate}', options.baseDate) |
| 43 | + .replace('{period}', options.endDate ? options.endDate : options.period); |
| 44 | + |
| 45 | + return options; |
| 46 | +} |
| 47 | + |
| 48 | +function buildIntradayTimeSeriesOptions(options) { |
| 49 | + var url = config.FITBIT_BASE_API_URL + '/1/user/{userId}/{resourcePath}/date/{startDate}/{endDate}/{detailLevel}{extra}.json'; |
| 50 | + |
| 51 | + var extra = '/time/{startTime}/{endTime}'; |
| 52 | + |
| 53 | + options = assign({ |
| 54 | + userId: '-', |
| 55 | + resourcePath: 'activities/steps', |
| 56 | + startDate: 'today', |
| 57 | + endDate: 'today', |
| 58 | + detailLevel: '1min' |
| 59 | + }, options); |
| 60 | + |
| 61 | + if (options.startTime && options.endTime) { |
| 62 | + url.replace('{extra}', extra); |
| 63 | + } |
| 64 | + |
| 65 | + options.url = url.replace('{userId}', options.userId) |
| 66 | + .replace('{resourcePath}', options.resourcePath) |
| 67 | + .replace('{startDate}', options.startDate) |
| 68 | + .replace('{endDate}', options.endDate) |
| 69 | + .replace('{detailLevel}', options.detailLevel) |
| 70 | + .replace('{extra}', '') |
| 71 | + .replace('{startTime}', options.startTime) |
| 72 | + .replace('{endTime}', options.endTime); |
| 73 | + |
| 74 | + return options; |
| 75 | +} |
0 commit comments