1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 * REST API for host-list management.
10 /** @suppress {duplicate} */
11 var remoting
= remoting
|| {};
19 * @implements {remoting.HostListApi}
21 remoting
.HostListApiImpl = function() {
25 remoting
.HostListApiImpl
.prototype.register = function(
26 newHostId
, hostName
, publicKey
, hostClientId
) {
27 var newHostDetails
= { data
: {
33 return new remoting
.Xhr({
35 url
: remoting
.settings
.DIRECTORY_API_BASE_URL
+ '/@me/hosts',
37 hostClientId
: hostClientId
39 jsonContent
: newHostDetails
,
42 }).start().then(function(response
) {
43 if (response
.status
== 200) {
44 var result
= response
.getJson();
46 return base
.getStringAttr(result
['data'], 'authorizationCode', '');
52 'Failed to register the host. Status: ' + response
.status
+
53 ' response: ' + response
.getText());
54 throw new remoting
.Error(remoting
.Error
.Tag
.REGISTRATION_FAILED
);
60 remoting
.HostListApiImpl
.prototype.get = function() {
62 return new remoting
.Xhr({
64 url
: remoting
.settings
.DIRECTORY_API_BASE_URL
+ '/@me/hosts',
66 }).start().then(function(/** !remoting.Xhr.Response */ response
) {
67 return that
.parseHostListResponse_(response
);
72 remoting
.HostListApiImpl
.prototype.put
=
73 function(hostId
, hostName
, hostPublicKey
) {
74 return new remoting
.Xhr({
76 url
: remoting
.settings
.DIRECTORY_API_BASE_URL
+ '/@me/hosts/' + hostId
,
81 'publicKey': hostPublicKey
85 }).start().then(remoting
.HostListApiImpl
.defaultResponse_());
89 remoting
.HostListApiImpl
.prototype.remove = function(hostId
) {
90 return new remoting
.Xhr({
92 url
: remoting
.settings
.DIRECTORY_API_BASE_URL
+ '/@me/hosts/' + hostId
,
94 }).start().then(remoting
.HostListApiImpl
.defaultResponse_(
95 [remoting
.Error
.Tag
.NOT_FOUND
]));
99 * Handle the results of the host list request. A success response will
100 * include a JSON-encoded list of host descriptions, which is parsed and
101 * passed to the callback.
103 * @param {!remoting.Xhr.Response} response
104 * @return {!Array<!remoting.Host>}
107 remoting
.HostListApiImpl
.prototype.parseHostListResponse_ = function(response
) {
108 if (response
.status
== 200) {
109 var obj
= /** @type {{data: {items: Array}}} */
110 (base
.jsonParseSafe(response
.getText()));
111 if (!obj
|| !obj
.data
) {
112 console
.error('Invalid "hosts" response from server.');
113 throw remoting
.Error
.unexpected();
115 var items
= obj
.data
.items
|| [];
116 var hosts
= items
.map(
117 function(/** Object */ item
) {
118 var host
= new remoting
.Host(base
.getStringAttr(item
, 'hostId', ''));
119 host
.hostName
= base
.getStringAttr(item
, 'hostName', '');
120 host
.status
= base
.getStringAttr(item
, 'status', '');
121 host
.jabberId
= base
.getStringAttr(item
, 'jabberId', '');
122 host
.publicKey
= base
.getStringAttr(item
, 'publicKey', '');
123 host
.hostVersion
= base
.getStringAttr(item
, 'hostVersion', '');
124 host
.tokenUrlPatterns
=
125 base
.getArrayAttr(item
, 'tokenUrlPatterns', []);
126 host
.updatedTime
= base
.getStringAttr(item
, 'updatedTime', '');
127 host
.hostOfflineReason
=
128 base
.getStringAttr(item
, 'hostOfflineReason', '');
134 throw remoting
.Error
.fromHttpStatus(response
.status
);
139 * Generic success/failure response proxy.
141 * @param {Array<remoting.Error.Tag>=} opt_ignoreErrors
142 * @return {function(!remoting.Xhr.Response):void}
145 remoting
.HostListApiImpl
.defaultResponse_ = function(opt_ignoreErrors
) {
146 /** @param {!remoting.Xhr.Response} response */
147 var result = function(response
) {
148 var error
= remoting
.Error
.fromHttpStatus(response
.status
);
149 if (error
.isNone()) {
153 if (opt_ignoreErrors
&& error
.hasTag
.apply(error
, opt_ignoreErrors
)) {
162 /** @type {remoting.HostListApi} */
163 remoting
.hostListApi
= new remoting
.HostListApiImpl();