-
Notifications
You must be signed in to change notification settings - Fork 45
/
thingpediaclient.js
88 lines (76 loc) · 2.78 KB
/
thingpediaclient.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
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of ThingEngine
//
// Copyright 2016 The Board of Trustees of the Leland Stanford Junior University
//
// Author: Giovanni Campagna <gcampagn@cs.stanford.edu>
//
// See COPYING for details
"use strict";
const ThingTalk = require('thingtalk');
const THINGPEDIA_URL = '/thingpedia';
function httpRequest(url) {
return Promise.resolve($.ajax(url));
}
module.exports = class ThingpediaClientBrowser {
constructor(developerKey, locale) {
this.developerKey = developerKey;
this.locale = locale || 'en_US';
}
_simpleRequest(to, noAppend) {
if (!noAppend) {
to += '?locale=' + this.locale;
to += '&thingtalk_version=' + ThingTalk.version;
if (this.developerKey)
to += '&developer_key=' + this.developerKey;
}
return httpRequest(to);
}
getDeviceCode(id) {
var to = THINGPEDIA_URL + '/api/v3/devices/code/' + id;
return this._simpleRequest(to);
}
getSchemas(kinds, withMetadata) {
var to = THINGPEDIA_URL + '/api/v3/schema/' + kinds.join(',');
to += '?locale=' + this.locale;
to += '&thingtalk_version=' + ThingTalk.version;
if (withMetadata)
to += '&meta=1';
if (this.developerKey)
to += '&developer_key=' + this.developerKey;
return this._simpleRequest(to, true);
}
getDeviceFactories(klass) {
var to = THINGPEDIA_URL + '/api/v3/devices/setup';
if (klass) {
to += '?class=' + klass;
to += '&thingtalk_version=' + ThingTalk.version;
if (this.developerKey)
to += '&developer_key=' + this.developerKey;
return this._simpleRequest(to, true);
} else {
return this._simpleRequest(to);
}
}
getDeviceSetup(kinds) {
var to = THINGPEDIA_URL + '/api/v3/devices/setup/' + kinds.join(',');
return this._simpleRequest(to);
}
getExamplesByKey(key, isBase) {
var to = THINGPEDIA_URL + '/api/v3/examples/search?locale=' + this.locale + '&q=' + encodeURIComponent(key)
+ '&base=' + (isBase ? '1' : '0');
to += '&thingtalk_version=' + ThingTalk.version;
if (this.developerKey)
to += '&developer_key=' + this.developerKey;
return this._simpleRequest(to, true);
}
getExamplesByKinds(kinds, isBase) {
var to = THINGPEDIA_URL + '/api/v3/examples/by-kinds/' + kinds.join(',') + '?locale=' + this.locale
+ '&base=' + (isBase ? '1' : '0');
to += '&thingtalk_version=' + ThingTalk.version;
if (this.developerKey)
to += '&developer_key=' + this.developerKey;
return this._simpleRequest(to, true);
}
};