Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: remoting/webapp/crd/js/host_list_api.js

Issue 840023004: Implement mocks for identity and host-list, add a browser test to test the app in various failure m… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 * API for host-list management.
8 */ 8 */
9 9
10 'use strict'; 10 'use strict';
11 11
12 /** @suppress {duplicate} */ 12 /** @suppress {duplicate} */
13 var remoting = remoting || {}; 13 var remoting = remoting || {};
14 14
15 /** @constructor */ 15 /** @interface */
16 remoting.HostListApi = function() { 16 remoting.HostListApi = function() {
17 }; 17 };
18 18
19 /** 19 /**
20 * Fetch the list of hosts for a user. 20 * Fetch the list of hosts for a user.
21 * 21 *
22 * @param {function(Array.<remoting.Host>):void} onDone 22 * @param {function(Array.<remoting.Host>):void} onDone
23 * @param {function(remoting.Error):void} onError 23 * @param {function(remoting.Error):void} onError
24 */ 24 */
25 remoting.HostListApi.prototype.get = function(onDone, onError) { 25 remoting.HostListApi.prototype.get = function(onDone, onError) {
26 /** @type {function(XMLHttpRequest):void} */
27 var parseHostListResponse =
28 this.parseHostListResponse_.bind(this, onDone, onError)
29 /** @param {string} token */
30 var onToken = function(token) {
31 var headers = { 'Authorization': 'OAuth ' + token };
32 remoting.xhr.get(remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts',
33 parseHostListResponse, '', headers);
34 };
35 remoting.identity.callWithToken(onToken, onError);
36 }; 26 };
37 27
38 /** 28 /**
39 * Update the information for a host. 29 * Update the information for a host.
40 * 30 *
41 * @param {function():void} onDone 31 * @param {function():void} onDone
42 * @param {function(remoting.Error):void} onError 32 * @param {function(remoting.Error):void} onError
43 * @param {string} hostId 33 * @param {string} hostId
44 * @param {string} hostName 34 * @param {string} hostName
45 * @param {string} hostPublicKey 35 * @param {string} hostPublicKey
46 */ 36 */
47 remoting.HostListApi.prototype.put = 37 remoting.HostListApi.prototype.put =
48 function(hostId, hostName, hostPublicKey, onDone, onError) { 38 function(hostId, hostName, hostPublicKey, onDone, onError) {
49 /** @param {string} token */
50 var onToken = function(token) {
51 var headers = {
52 'Authorization': 'OAuth ' + token,
53 'Content-type' : 'application/json; charset=UTF-8'
54 };
55 var newHostDetails = {
56 'data': {
57 'hostId': hostId,
58 'hostName': hostName,
59 'publicKey': hostPublicKey
60 }
61 };
62 remoting.xhr.put(
63 remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId,
64 remoting.xhr.defaultResponse(onDone, onError),
65 JSON.stringify(newHostDetails),
66 headers);
67 };
68 remoting.identity.callWithToken(onToken, onError);
69 }; 39 };
70 40
71 /** 41 /**
72 * Delete a host. 42 * Delete a host.
73 * 43 *
74 * @param {function():void} onDone 44 * @param {function():void} onDone
75 * @param {function(remoting.Error):void} onError 45 * @param {function(remoting.Error):void} onError
76 * @param {string} hostId 46 * @param {string} hostId
77 */ 47 */
78 remoting.HostListApi.prototype.remove = function(hostId, onDone, onError) { 48 remoting.HostListApi.prototype.remove = function(hostId, onDone, onError) {
79 /** @param {string} token */
80 var onToken = function(token) {
81 var headers = { 'Authorization': 'OAuth ' + token };
82 remoting.xhr.remove(
83 remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId,
84 remoting.xhr.defaultResponse(onDone, onError),
85 '', headers);
86 };
87 remoting.identity.callWithToken(onToken, onError);
88 }; 49 };
89
90 /**
91 * Handle the results of the host list request. A success response will
92 * include a JSON-encoded list of host descriptions, which is parsed and
93 * passed to the callback.
94 *
95 * @param {function(Array.<remoting.Host>):void} onDone
96 * @param {function(remoting.Error):void} onError
97 * @param {XMLHttpRequest} xhr
98 * @private
99 */
100 remoting.HostListApi.prototype.parseHostListResponse_ =
101 function(onDone, onError, xhr) {
102 if (xhr.status == 200) {
103 var response = /** @type {{data: {items: Array}}} */
104 (base.jsonParseSafe(xhr.responseText));
105 if (!response || !response.data) {
106 console.error('Invalid "hosts" response from server.');
107 onError(remoting.Error.UNEXPECTED);
108 } else {
109 var hosts = response.data.items || [];
110 onDone(hosts);
111 }
112 } else {
113 onError(remoting.Error.fromHttpError(xhr.status));
114 }
115 };
116
117 /** @type {remoting.HostListApi} */
118 remoting.hostListApi = new remoting.HostListApi();
OLDNEW
« no previous file with comments | « remoting/webapp/browser_test/unauthenticated_browser_test.js ('k') | remoting/webapp/crd/js/host_list_api_impl.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698