-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcloud-client.js
More file actions
131 lines (111 loc) · 3.63 KB
/
cloud-client.js
File metadata and controls
131 lines (111 loc) · 3.63 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const _ = require("lodash");
const fetch = require("node-fetch");
const CacheManager = require("cache-manager");
const cache = CacheManager.caching({
store: "memory",
max: 1000,
ttl: 3, // secs
});
// Client for NetsBlox Cloud
class NetsBloxCloud {
constructor(cloudUrl, id, secret) {
this.cloudUrl = cloudUrl;
this.id = id;
this.secret = secret;
}
async whoami(cookie) {
// TODO: look up the username using the cookie
}
async getRoomState(projectId) {
const url = `/network/id/${projectId}`;
return await this.get(url);
}
async getClientInfo(clientId) {
const url = `/network/${clientId}/state`;
return await this.get(url);
}
async userExists(username) {
const user = await this.viewUser(username).catch(nop);
return !!user;
}
async viewUser(username) {
const url = `/users/${username}`;
return await this.get(url);
}
async sendMessage(message) {
const url = `/network/messages/`;
const response = await this.post(url, message);
return response.status > 199 && response.status < 400;
}
async post(urlPath, body) {
const headers = { "Content-Type": "application/json" };
body = JSON.stringify(body);
return await this.fetch(urlPath, { method: "post", body, headers });
}
async get(urlPath) {
return await cache.wrap(urlPath, async () => {
const response = await this.fetch(urlPath);
return await response.json();
});
}
async fetch(urlPath, options = {}) {
const url = `${this.cloudUrl}${urlPath}`;
const headers = options.headers || {};
// TODO: make this a Bearer token?
headers["X-Authorization"] = this.id + ":" + this.secret;
options.headers = headers;
return await fetch(url, options);
}
async viewGroup(groupId) {
const url = `/groups/id/${groupId}`;
return await this.get(url);
}
// Service Settings
async getServiceSettings(username) { // TODO: memoize this
const url = `/services/settings/user/${username}/${this.id}/all`;
const settings = await this.get(url);
// settings fields are strings which we happen to use to store JSON
// so we need to JSON.parse them, too
settings.user = JSON.parse(settings.user);
settings.member = JSON.parse(settings.member);
settings.groups = _.mapValues(settings.groups, JSON.parse);
return settings;
}
async getUserServiceSettings(username) {
const url = `/services/settings/user/${username}/${this.id}`;
const settings = await this.get(url);
return JSON.parse(settings);
}
async setUserServiceSettings(username, settings) {
const url = `/services/settings/user/${username}/${this.id}`;
const response = await this.post(url, settings);
const isOk = response.status > 199 && response.status < 400;
return isOk; // FIXME: throwing might be better...
}
async getGroupServiceSettings(groupId) {
const url = `/services/settings/group/${groupId}/${this.id}`;
const settings = await this.get(url);
return JSON.parse(settings);
}
async setGroupServiceSettings(groupId, settings) {
const url = `/services/settings/group/${groupId}/${this.id}`;
const response = await this.post(url, settings);
const isOk = response.status > 199 && response.status < 400;
return isOk; // FIXME: throwing might be better...
}
// OAuth
async getOAuthClients() {
const url = `/oauth/clients/`;
const clients = await this.get(url);
return clients;
}
isConfigured() {
return this.cloudUrl && this.id && this.secret;
}
}
const config = require("./config");
module.exports = new NetsBloxCloud(
config.NetsBloxCloud,
config.NetsBloxCloudID,
config.NetsBloxCloudSecret,
);