This repository has been archived by the owner on May 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
generic.js
108 lines (93 loc) · 4.22 KB
/
generic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of ThingEngine
//
// Copyright 2015 The Board of Trustees of the Leland Stanford Junior University
//
// Author: Giovanni Campagna <gcampagn@cs.stanford.edu>
//
// See LICENSE for details
"use strict";
const ThingTalk = require('thingtalk');
const Tp = require('thingpedia');
const Utils = require('./utils');
const Base = require('./base_generic');
// FIXME webhooks
// FIXME websockets
function get(obj, propchain) {
for (let prop of propchain.split('.'))
obj = obj[prop];
return obj;
}
module.exports = class GenericRestModule extends Base {
constructor(kind, ast) {
super(kind, ast);
const authfn = Utils.makeAuth(ast);
for (let action in ast.actions) {
const block = ast.actions[action];
this._loaded.prototype['do_' + action] = function(params) {
const url = Utils.formatString(block.url, this.state, params);
let defaultobj = block['default'] || {};
let method = block.method || 'POST';
var obj = {};
Object.assign(obj, defaultobj);
Object.assign(obj, params);
return Tp.Helpers.Http.request(url, method, JSON.stringify(obj),
{ auth: authfn(this),
useOAuth2: this,
dataContentType: 'application/json' });
};
}
for (let query in ast.queries) {
const block = ast.queries[query];
let pollInterval = ast.queries[query].poll_interval;
if (!pollInterval)
pollInterval = ast.queries[query]['poll-interval'];
this._loaded.prototype['get_' + query] = function(params, filter, count) {
// ignore count and filter
let url = Utils.formatString(block.url, this.state, params);
return Tp.Helpers.Http.get(url, {
auth: authfn(this),
useOAuth: this,
accept: 'application/json' }).then((response) => {
let parsed = JSON.parse(response);
function extractOne(result) {
let extracted = {};
for (let arg of block.args) {
if (arg.is_input)
continue;
if (arg.json_key)
extracted[arg.name] = get(result, arg.json_key);
else
extracted[arg.name] = result[arg.name];
if (arg.type === 'Date')
extracted[arg.name] = new Date(extracted[arg.name]);
else if (arg.type === 'Currency' && typeof extracted[arg.name] === 'number')
extracted[arg.name] = new ThingTalk.Builtin.Currency(extracted[arg.name], 'usd');
else if (arg.type === 'Currency' && typeof extracted[arg.name] === 'string')
extracted[arg.name] = new ThingTalk.Builtin.Currency(parseFloat(extracted[arg.name]), 'usd');
}
return extracted;
}
if (block.json_key)
parsed = get(parsed, block.json_key);
if (Array.isArray(parsed))
return parsed.map(extractOne);
else
return [extractOne(parsed)];
});
};
// FIXME webhooks
// FIXME websockets
this._loaded.prototype['subscribe_' + query] = function(params, state, filter) {
return new Tp.Helpers.PollingStream(state, pollInterval, () => this['get_' + query](params));
};
this._loaded.prototype['history_' + query] = function(params, base, delta, filters) {
return null; // no history
};
this._loaded.prototype['sequence_' + query] = function(params, base, limit, filters) {
return null; // no sequence history
};
}
}
};