| 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 'use strict'; | 10 'use strict'; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 /** | 22 /** |
| 23 * Fetch the list of hosts for a user. | 23 * Fetch the list of hosts for a user. |
| 24 * | 24 * |
| 25 * @param {function(Array.<remoting.Host>):void} onDone | 25 * @param {function(Array.<remoting.Host>):void} onDone |
| 26 * @param {function(remoting.Error):void} onError | 26 * @param {function(remoting.Error):void} onError |
| 27 */ | 27 */ |
| 28 remoting.HostListApiImpl.prototype.get = function(onDone, onError) { | 28 remoting.HostListApiImpl.prototype.get = function(onDone, onError) { |
| 29 /** @type {function(XMLHttpRequest):void} */ | 29 /** @type {function(XMLHttpRequest):void} */ |
| 30 var parseHostListResponse = | 30 var parseHostListResponse = |
| 31 this.parseHostListResponse_.bind(this, onDone, onError) | 31 this.parseHostListResponse_.bind(this, onDone, onError); |
| 32 /** @param {string} token */ | 32 /** @param {string} token */ |
| 33 var onToken = function(token) { | 33 var onToken = function(token) { |
| 34 var headers = { 'Authorization': 'OAuth ' + token }; | 34 remoting.xhr.start({ |
| 35 remoting.xhr.get(remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts', | 35 method: 'GET', |
| 36 parseHostListResponse, '', headers); | 36 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts', |
| 37 onDone: parseHostListResponse, |
| 38 oauthToken: token |
| 39 }); |
| 37 }; | 40 }; |
| 38 remoting.identity.callWithToken(onToken, onError); | 41 remoting.identity.callWithToken(onToken, onError); |
| 39 }; | 42 }; |
| 40 | 43 |
| 41 /** | 44 /** |
| 42 * Update the information for a host. | 45 * Update the information for a host. |
| 43 * | 46 * |
| 44 * @param {function():void} onDone | 47 * @param {function():void} onDone |
| 45 * @param {function(remoting.Error):void} onError | 48 * @param {function(remoting.Error):void} onError |
| 46 * @param {string} hostId | 49 * @param {string} hostId |
| 47 * @param {string} hostName | 50 * @param {string} hostName |
| 48 * @param {string} hostPublicKey | 51 * @param {string} hostPublicKey |
| 49 */ | 52 */ |
| 50 remoting.HostListApiImpl.prototype.put = | 53 remoting.HostListApiImpl.prototype.put = |
| 51 function(hostId, hostName, hostPublicKey, onDone, onError) { | 54 function(hostId, hostName, hostPublicKey, onDone, onError) { |
| 52 /** @param {string} token */ | 55 /** @param {string} token */ |
| 53 var onToken = function(token) { | 56 var onToken = function(token) { |
| 54 var headers = { | |
| 55 'Authorization': 'OAuth ' + token, | |
| 56 'Content-type' : 'application/json; charset=UTF-8' | |
| 57 }; | |
| 58 var newHostDetails = { | 57 var newHostDetails = { |
| 59 'data': { | 58 'data': { |
| 60 'hostId': hostId, | 59 'hostId': hostId, |
| 61 'hostName': hostName, | 60 'hostName': hostName, |
| 62 'publicKey': hostPublicKey | 61 'publicKey': hostPublicKey |
| 63 } | 62 } |
| 64 }; | 63 }; |
| 65 remoting.xhr.put( | 64 remoting.xhr.start({ |
| 66 remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, | 65 method: 'PUT', |
| 67 remoting.xhr.defaultResponse(onDone, onError), | 66 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, |
| 68 JSON.stringify(newHostDetails), | 67 onDone: remoting.xhr.defaultResponse(onDone, onError), |
| 69 headers); | 68 jsonContent: newHostDetails, |
| 69 oauthToken: token |
| 70 }); |
| 70 }; | 71 }; |
| 71 remoting.identity.callWithToken(onToken, onError); | 72 remoting.identity.callWithToken(onToken, onError); |
| 72 }; | 73 }; |
| 73 | 74 |
| 74 /** | 75 /** |
| 75 * Delete a host. | 76 * Delete a host. |
| 76 * | 77 * |
| 77 * @param {function():void} onDone | 78 * @param {function():void} onDone |
| 78 * @param {function(remoting.Error):void} onError | 79 * @param {function(remoting.Error):void} onError |
| 79 * @param {string} hostId | 80 * @param {string} hostId |
| 80 */ | 81 */ |
| 81 remoting.HostListApiImpl.prototype.remove = function(hostId, onDone, onError) { | 82 remoting.HostListApiImpl.prototype.remove = function(hostId, onDone, onError) { |
| 82 /** @param {string} token */ | 83 /** @param {string} token */ |
| 83 var onToken = function(token) { | 84 var onToken = function(token) { |
| 84 var headers = { 'Authorization': 'OAuth ' + token }; | 85 remoting.xhr.start({ |
| 85 remoting.xhr.remove( | 86 method: 'DELETE', |
| 86 remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, | 87 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, |
| 87 remoting.xhr.defaultResponse(onDone, onError), | 88 onDone: remoting.xhr.defaultResponse(onDone, onError), |
| 88 '', headers); | 89 oauthToken: token |
| 90 }); |
| 89 }; | 91 }; |
| 90 remoting.identity.callWithToken(onToken, onError); | 92 remoting.identity.callWithToken(onToken, onError); |
| 91 }; | 93 }; |
| 92 | 94 |
| 93 /** | 95 /** |
| 94 * Handle the results of the host list request. A success response will | 96 * Handle the results of the host list request. A success response will |
| 95 * include a JSON-encoded list of host descriptions, which is parsed and | 97 * include a JSON-encoded list of host descriptions, which is parsed and |
| 96 * passed to the callback. | 98 * passed to the callback. |
| 97 * | 99 * |
| 98 * @param {function(Array.<remoting.Host>):void} onDone | 100 * @param {function(Array.<remoting.Host>):void} onDone |
| (...skipping 13 matching lines...) Expand all Loading... |
| 112 var hosts = response.data.items || []; | 114 var hosts = response.data.items || []; |
| 113 onDone(hosts); | 115 onDone(hosts); |
| 114 } | 116 } |
| 115 } else { | 117 } else { |
| 116 onError(remoting.Error.fromHttpStatus(xhr.status)); | 118 onError(remoting.Error.fromHttpStatus(xhr.status)); |
| 117 } | 119 } |
| 118 }; | 120 }; |
| 119 | 121 |
| 120 /** @type {remoting.HostListApi} */ | 122 /** @type {remoting.HostListApi} */ |
| 121 remoting.hostListApi = new remoting.HostListApiImpl(); | 123 remoting.hostListApi = new remoting.HostListApiImpl(); |
| OLD | NEW |