OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * REST API for host-list management. | 7 * REST API for host-list management. |
8 */ | 8 */ |
9 | 9 |
10 /** @suppress {duplicate} */ | 10 /** @suppress {duplicate} */ |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 * Update the information for a host. | 46 * Update the information for a host. |
47 * | 47 * |
48 * @param {function():void} onDone | 48 * @param {function():void} onDone |
49 * @param {function(!remoting.Error):void} onError | 49 * @param {function(!remoting.Error):void} onError |
50 * @param {string} hostId | 50 * @param {string} hostId |
51 * @param {string} hostName | 51 * @param {string} hostName |
52 * @param {string} hostPublicKey | 52 * @param {string} hostPublicKey |
53 */ | 53 */ |
54 remoting.HostListApiImpl.prototype.put = | 54 remoting.HostListApiImpl.prototype.put = |
55 function(hostId, hostName, hostPublicKey, onDone, onError) { | 55 function(hostId, hostName, hostPublicKey, onDone, onError) { |
56 /** @param {string} token */ | 56 new remoting.Xhr({ |
57 var onToken = function(token) { | 57 method: 'PUT', |
58 var newHostDetails = { | 58 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, |
| 59 jsonContent: { |
59 'data': { | 60 'data': { |
60 'hostId': hostId, | 61 'hostId': hostId, |
61 'hostName': hostName, | 62 'hostName': hostName, |
62 'publicKey': hostPublicKey | 63 'publicKey': hostPublicKey |
63 } | 64 } |
64 }; | 65 }, |
65 new remoting.Xhr({ | 66 useIdentity: true |
66 method: 'PUT', | 67 }).start().then(remoting.HostListApiImpl.defaultResponse_(onDone, onError)); |
67 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, | |
68 jsonContent: newHostDetails, | |
69 oauthToken: token | |
70 }).start().then(remoting.Xhr.defaultResponse(onDone, onError)); | |
71 }; | |
72 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); | |
73 }; | 68 }; |
74 | 69 |
75 /** | 70 /** |
76 * Delete a host. | 71 * Delete a host. |
77 * | 72 * |
78 * @param {function():void} onDone | 73 * @param {function():void} onDone |
79 * @param {function(!remoting.Error):void} onError | 74 * @param {function(!remoting.Error):void} onError |
80 * @param {string} hostId | 75 * @param {string} hostId |
81 */ | 76 */ |
82 remoting.HostListApiImpl.prototype.remove = function(hostId, onDone, onError) { | 77 remoting.HostListApiImpl.prototype.remove = function(hostId, onDone, onError) { |
83 /** @param {string} token */ | 78 new remoting.Xhr({ |
84 var onToken = function(token) { | 79 method: 'DELETE', |
85 new remoting.Xhr({ | 80 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, |
86 method: 'DELETE', | 81 useIdentity: true |
87 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, | 82 }).start().then(remoting.HostListApiImpl.defaultResponse_( |
88 oauthToken: token | 83 onDone, onError, [remoting.Error.Tag.NOT_FOUND])); |
89 }).start().then(remoting.Xhr.defaultResponse( | |
90 onDone, onError, [remoting.Error.Tag.NOT_FOUND])); | |
91 }; | |
92 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); | |
93 }; | 84 }; |
94 | 85 |
95 /** | 86 /** |
96 * Handle the results of the host list request. A success response will | 87 * Handle the results of the host list request. A success response will |
97 * include a JSON-encoded list of host descriptions, which is parsed and | 88 * include a JSON-encoded list of host descriptions, which is parsed and |
98 * passed to the callback. | 89 * passed to the callback. |
99 * | 90 * |
100 * @param {function(Array<remoting.Host>):void} onDone | 91 * @param {function(Array<remoting.Host>):void} onDone |
101 * @param {function(!remoting.Error):void} onError | 92 * @param {function(!remoting.Error):void} onError |
102 * @param {!remoting.Xhr.Response} response | 93 * @param {!remoting.Xhr.Response} response |
(...skipping 25 matching lines...) Expand all Loading... |
128 base.getStringAttr(item, 'hostOfflineReason', ''); | 119 base.getStringAttr(item, 'hostOfflineReason', ''); |
129 return host; | 120 return host; |
130 }); | 121 }); |
131 onDone(hosts); | 122 onDone(hosts); |
132 } | 123 } |
133 } else { | 124 } else { |
134 onError(remoting.Error.fromHttpStatus(response.status)); | 125 onError(remoting.Error.fromHttpStatus(response.status)); |
135 } | 126 } |
136 }; | 127 }; |
137 | 128 |
| 129 /** |
| 130 * Generic success/failure response proxy. |
| 131 * |
| 132 * @param {function():void} onDone |
| 133 * @param {function(!remoting.Error):void} onError |
| 134 * @param {Array<remoting.Error.Tag>=} opt_ignoreErrors |
| 135 * @return {function(!remoting.Xhr.Response):void} |
| 136 * @private |
| 137 */ |
| 138 remoting.HostListApiImpl.defaultResponse_ = function( |
| 139 onDone, onError, opt_ignoreErrors) { |
| 140 /** @param {!remoting.Xhr.Response} response */ |
| 141 var result = function(response) { |
| 142 var error = remoting.Error.fromHttpStatus(response.status); |
| 143 if (error.isNone()) { |
| 144 onDone(); |
| 145 return; |
| 146 } |
| 147 |
| 148 if (opt_ignoreErrors && error.hasTag.apply(error, opt_ignoreErrors)) { |
| 149 onDone(); |
| 150 return; |
| 151 } |
| 152 |
| 153 onError(error); |
| 154 }; |
| 155 return result; |
| 156 }; |
| 157 |
138 /** @type {remoting.HostListApi} */ | 158 /** @type {remoting.HostListApi} */ |
139 remoting.hostListApi = new remoting.HostListApiImpl(); | 159 remoting.hostListApi = new remoting.HostListApiImpl(); |
140 | 160 |
141 })(); | 161 })(); |
OLD | NEW |