-
Notifications
You must be signed in to change notification settings - Fork 8
/
host_list_api_impl.js
135 lines (126 loc) · 4.22 KB
/
host_list_api_impl.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview
* REST API for host-list management.
*/
'use strict';
/** @suppress {duplicate} */
var remoting = remoting || {};
/**
* @constructor
* @implements {remoting.HostListApi}
*/
remoting.HostListApiImpl = function() {
};
/**
* Fetch the list of hosts for a user.
*
* @param {function(Array<remoting.Host>):void} onDone
* @param {function(remoting.Error):void} onError
*/
remoting.HostListApiImpl.prototype.get = function(onDone, onError) {
/** @type {function(XMLHttpRequest):void} */
var parseHostListResponse =
this.parseHostListResponse_.bind(this, onDone, onError)
/** @param {string} token */
var onToken = function(token) {
var headers = { 'Authorization': 'OAuth ' + token };
remoting.xhr.get(remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts',
parseHostListResponse, '', headers);
};
remoting.identity.callWithToken(onToken, onError);
};
/**
* Update the information for a host.
*
* @param {function():void} onDone
* @param {function(remoting.Error):void} onError
* @param {string} hostId
* @param {string} hostName
* @param {string} hostPublicKey
*/
remoting.HostListApiImpl.prototype.put =
function(hostId, hostName, hostPublicKey, onDone, onError) {
/** @param {string} token */
var onToken = function(token) {
var headers = {
'Authorization': 'OAuth ' + token,
'Content-type' : 'application/json; charset=UTF-8'
};
var newHostDetails = {
'data': {
'hostId': hostId,
'hostName': hostName,
'publicKey': hostPublicKey
}
};
remoting.xhr.put(
remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId,
remoting.xhr.defaultResponse(onDone, onError),
JSON.stringify(newHostDetails),
headers);
};
remoting.identity.callWithToken(onToken, onError);
};
/**
* Delete a host.
*
* @param {function():void} onDone
* @param {function(remoting.Error):void} onError
* @param {string} hostId
*/
remoting.HostListApiImpl.prototype.remove = function(hostId, onDone, onError) {
/** @param {string} token */
var onToken = function(token) {
var headers = { 'Authorization': 'OAuth ' + token };
remoting.xhr.remove(
remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId,
remoting.xhr.defaultResponse(onDone, onError),
'', headers);
};
remoting.identity.callWithToken(onToken, onError);
};
/**
* Handle the results of the host list request. A success response will
* include a JSON-encoded list of host descriptions, which is parsed and
* passed to the callback.
*
* @param {function(Array<remoting.Host>):void} onDone
* @param {function(remoting.Error):void} onError
* @param {XMLHttpRequest} xhr
* @private
*/
remoting.HostListApiImpl.prototype.parseHostListResponse_ =
function(onDone, onError, xhr) {
if (xhr.status == 200) {
var response = /** @type {{data: {items: Array}}} */
(base.jsonParseSafe(xhr.responseText));
if (!response || !response.data) {
console.error('Invalid "hosts" response from server.');
onError(remoting.Error.UNEXPECTED);
} else {
var items = response.data.items || [];
var hosts = items.map(
function(/** Object */ item) {
var host = new remoting.Host();
host.hostName = getStringAttr(item, 'hostName', '');
host.hostId = getStringAttr(item, 'hostId', '');
host.status = getStringAttr(item, 'status', '');
host.jabberId = getStringAttr(item, 'jabberId', '');
host.publicKey = getStringAttr(item, 'publicKey', '');
host.hostVersion = getStringAttr(item, 'hostVersion', '');
host.tokenUrlPatterns = getArrayAttr(item, 'tokenUrlPatterns', []);
host.updatedTime = getStringAttr(item, 'updatedTime', '');
host.hostOfflineReason = getStringAttr(item, 'hostOfflineReason', '');
return host;
});
onDone(hosts);
}
} else {
onError(remoting.Error.fromHttpStatus(xhr.status));
}
};
/** @type {remoting.HostListApi} */
remoting.hostListApi = new remoting.HostListApiImpl();