1 // Copyright 2015 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 * Client for the GCD REST API.
8 * TODO: Add link to GCD docs.
11 /** @suppress {duplicate} */
12 var remoting
= remoting
|| {};
15 * Namespace for GCD definitions
18 remoting
.gcd
= remoting
.gcd
|| {};
23 * robotAccountEmail: string,
24 * robotAccountAuthorizationCode: string,
29 remoting
.gcd
.RegistrationTicket
;
32 * TODO: Flesh out with typical fields.
43 remoting
.gcd
.DevicePatch
;
47 * devices: (Array<remoting.gcd.Device>|undefined)
50 remoting
.gcd
.DeviceListResponse
;
56 * Interprets an HTTP response as a JSON object with a specific value
57 * in the 'kind' field.
58 * @param {remoting.Xhr.Response} response
59 * @param {string} expectedKind
61 * @throws {remoting.Error}
63 var responseAsObject = function(response
, expectedKind
) {
64 if (typeof response
.getJson() != 'object') {
66 'invalid response; expected object, got:', response
.getJson());
67 throw remoting
.Error
.unexpected();
69 var obj
= base
.assertObject(response
.getJson());
70 var kind
= base
.getStringAttr(obj
, 'kind');
71 if (kind
!= expectedKind
) {
73 'invalid resonse kind; expected ' + expectedKind
+ ', got ' + kind
);
74 throw remoting
.Error
.unexpected();
80 * Interprets an HTTP response as containing a GCD registration ticket.
81 * @param {remoting.Xhr.Response} response
82 * @return {!remoting.gcd.RegistrationTicket}
83 * @throws {remoting.Error}
85 var responseAsGcdRegistrationTicket = function(response
) {
86 return /** @type {!remoting.gcd.RegistrationTicket} */ (
88 response
, 'clouddevices#registrationTicket'));
92 * Interprets an HTTP response as containing a GCD device defintion.
93 * @param {remoting.Xhr.Response} response
94 * @return {!remoting.gcd.Device}
95 * @throws {remoting.Error}
97 var responseAsGcdDevice = function(response
) {
98 return /** @type {!remoting.gcd.Device} */ (
99 responseAsObject(response
, 'clouddevices#device'));
103 * Interprets an HTTP response as containing a GCD device list.
104 * @param {remoting.Xhr.Response} response
105 * @return {!remoting.gcd.DeviceListResponse}
106 * @throws {remoting.Error}
108 var responseAsGcdDeviceListResponse = function(response
) {
109 return /** @type {!remoting.gcd.DeviceListResponse} */ (
110 responseAsObject(response
, 'clouddevices#devicesListResponse'));
114 * Creates a new client using a specific API key, and optionall a
115 * specific base URL, and OAuth2 client ID.
118 * apiBaseUrl: (string|undefined)
122 remoting
.gcd
.Client = function(options
) {
124 this.apiKey_
= options
.apiKey
;
126 this.apiBaseUrl_
= options
.apiBaseUrl
||
127 'https://www.googleapis.com/clouddevices/v1';
131 * Creates a new registration ticket.
132 * TODO: Add link to GCD docs.
133 * @return {!Promise<remoting.gcd.RegistrationTicket>}
135 remoting
.gcd
.Client
.prototype.insertRegistrationTicket = function() {
136 return new remoting
.Xhr({
138 url
: this.apiBaseUrl_
+ '/registrationTickets',
139 jsonContent
: { 'userEmail': 'me' },
142 }).start().then(function(/** remoting.Xhr.Response */ response
) {
143 if (response
.isError()) {
144 console
.error('error creating registration ticket');
145 throw remoting
.Error
.unexpected();
147 return responseAsGcdRegistrationTicket(response
);
152 * Updates an existing registration ticket using patch semantics.
153 * TODO: Add link to GCD docs.
154 * @param {string} ticketId
155 * @param {!Object<string,*>} deviceDraft
156 * @param {string} oauthClientId
157 * @return {!Promise<remoting.gcd.RegistrationTicket>}
159 remoting
.gcd
.Client
.prototype.patchRegistrationTicket = function(
160 ticketId
, deviceDraft
, oauthClientId
) {
161 return new remoting
.Xhr({
163 url
: this.apiBaseUrl_
+ '/registrationTickets/' +
164 encodeURIComponent(ticketId
),
169 'deviceDraft': deviceDraft
,
170 'oauthClientId': oauthClientId
173 }).start().then(function(response
) {
174 if (response
.isError()) {
175 console
.error('error patching registration ticket');
176 throw remoting
.Error
.unexpected();
178 return responseAsGcdRegistrationTicket(response
);
183 * Finalizes device registration and returns its credentials.
184 * TODO: Add link to GCD docs.
185 * @param {string} ticketId
186 * @return {!Promise<remoting.gcd.RegistrationTicket>}
188 remoting
.gcd
.Client
.prototype.finalizeRegistrationTicket = function(ticketId
) {
189 return new remoting
.Xhr({
191 url
: this.apiBaseUrl_
+ '/registrationTickets/' +
192 encodeURIComponent(ticketId
) + '/finalize',
197 }).start().then(function(response
) {
198 if (response
.isError()) {
199 console
.error('error finalizing registration ticket');
200 throw remoting
.Error
.unexpected();
202 return responseAsGcdRegistrationTicket(response
);
207 * Lists devices user has access to.
208 * TODO: Add link to GCD docs.
209 * @param {string=} opt_nameSubstring If present, the list of devices
210 * is filtered by GCD such that every device returned contains
211 * this string as as a substring of its |name| or |displayName|.
212 * @return {!Promise<!Array<remoting.gcd.Device>>}
214 remoting
.gcd
.Client
.prototype.listDevices = function(opt_nameSubstring
) {
215 return new remoting
.Xhr({
217 url
: this.apiBaseUrl_
+ '/devices',
219 nameSubstring
: opt_nameSubstring
|| null
223 }).start().then(function(response
) {
224 if (response
.isError()) {
225 console
.error('error getting device list');
226 throw remoting
.Error
.unexpected();
228 var hosts
= responseAsGcdDeviceListResponse(response
);
229 return hosts
.devices
|| [];
234 * Deletes a device from the system.
235 * TODO: Add link to GCD docs.
236 * @param {string} deviceId
237 * @return {!Promise<boolean>} Promise that resolves to true if the
238 * device was deleted, false if there was no such device ID.
240 remoting
.gcd
.Client
.prototype.deleteDevice = function(deviceId
) {
241 return new remoting
.Xhr({
243 url
: this.apiBaseUrl_
+ '/devices/' + deviceId
,
245 }).start().then(function(response
) {
246 if (response
.status
== 404) {
249 if (response
.isError()) {
250 console
.error('error deleting device');
251 throw remoting
.Error
.unexpected();
258 * Updates a device data using patch semantics.
259 * TODO: Add link to GCD docs.
260 * @param {string} deviceId
261 * @param {!Object<string,*>} patch
262 * @return {!Promise<remoting.gcd.Device>}
264 remoting
.gcd
.Client
.prototype.patchDevice = function(deviceId
, patch
) {
265 return new remoting
.Xhr({
267 url
: this.apiBaseUrl_
+ '/devices/' + deviceId
,
271 }).start().then(function(response
) {
272 if (response
.isError()) {
273 console
.error('error patching device');
274 throw remoting
.Error
.unexpected();
276 return responseAsGcdDevice(response
);