OLD | NEW |
---|---|
(Empty) | |
1 <!-- | |
2 ~ // Copyright 2016 The Chromium Authors. All rights reserved. | |
3 ~ // Use of this source code is governed by a BSD-style license that can be | |
4 ~ // found in the LICENSE file. | |
5 ~ | |
6 --> | |
7 | |
8 <link rel="import" href="/static/common/third_party/iron-ajax/iron-ajax.html"> | |
9 <link rel="import" href="/static/common/third_party/polymer/polymer.html"> | |
10 | |
11 <link rel="import" href="rpc-codes.html"> | |
12 <link rel="import" href="rpc-error.html"> | |
13 | |
14 <!-- The `rpc-call` element can send a pRPC request. | |
15 pRPC protocol: https://godoc.org/github.com/luci/luci-go/common/prpc#hdr-Protoco l. | |
16 --> | |
17 <dom-module id="rpc-call"> | |
18 <template> | |
19 <iron-ajax | |
20 id="ajax" | |
21 auto="[[auto]]" | |
22 method="POST" url="[[_url]]" | |
Bons
2016/02/13 17:18:23
place each attribute on it's own line. makes it ea
nodir
2016/02/17 02:02:12
Done.
| |
23 body="[[request]]" content-type="application/json" | |
24 debounce-duration="[[debounceDuration]]" | |
25 timeout="[[timeout]]" | |
26 verbose="[[verbose]]" | |
27 handle-as="json" json-prefix=")]}'" | |
Bons
2016/02/13 17:18:25
json is already the default for iron-ajax: https:/
nodir
2016/02/17 02:02:12
Done.
| |
28 on-request="_onRequest" | |
29 on-loading-changed="_loadingChanged" | |
30 on-response="_onResponse" | |
31 on-error="_onError" | |
32 ></iron-ajax> | |
33 </template> | |
34 <script> | |
35 'use strict'; | |
36 | |
37 Polymer({ | |
38 is: 'rpc-call', | |
Bons
2016/02/13 17:18:22
blank lines between top-level properties.
Polymer
nodir
2016/02/17 02:02:12
Done.
| |
39 properties: { | |
40 /** | |
41 * if true, use HTTP instead of HTTPS. | |
42 * If null (default) or undefined, determined automatically: | |
43 * - if host equals current host and current protocol is http, then | |
44 * false | |
Bons
2016/02/13 17:18:24
this will fit on the previous line. also full sent
nodir
2016/02/17 02:02:12
does not fit with "then"
Bons
2016/02/17 19:27:47
Still needs to be a full sentence.
| |
45 * - otherwise true. | |
46 */ | |
47 insecure: { | |
Bons
2016/02/13 17:18:25
the property already has a default value of undefi
nodir
2016/02/17 02:02:12
weirdly enough, if I replace it with `insecure: Bo
Bons
2016/02/17 19:27:47
That’s because it is then `undefined` and observer
| |
48 type: Boolean, | |
49 value: null | |
50 }, | |
51 | |
52 /** | |
53 * pRPC server host, defaults to current document host. | |
54 */ | |
55 host: { | |
56 type: String, | |
57 value: document.location.host, | |
58 }, | |
59 | |
60 /** | |
61 * Full service name, including package name. | |
62 */ | |
63 service: String, | |
64 | |
65 /** | |
66 * Service method name. | |
67 */ | |
68 method: String, | |
69 | |
70 _url: { | |
Bons
2016/02/13 17:18:25
place private properties below public ones to main
nodir
2016/02/17 02:02:12
Done.
| |
71 type: String, | |
72 computed: '_generateUrl(insecure, host, service, method)' | |
73 }, | |
74 | |
75 /** | |
76 * Request object. | |
77 */ | |
78 request: { | |
79 type: Object, | |
80 value: function() { | |
81 return {}; | |
82 } | |
83 }, | |
84 | |
85 /** | |
86 * Request timeout in milliseconds. | |
87 */ | |
88 timeout: { | |
89 type: Number, | |
90 value: 0, | |
91 observer: '_onTimeoutChanged' | |
92 }, | |
93 | |
94 /** | |
95 * If true, automatically sends a request when host, service, method | |
96 * or request changes. | |
97 */ | |
98 auto: { | |
99 type: Boolean, | |
100 value: false | |
101 }, | |
102 | |
103 /** | |
104 * If true, error messages will automatically be logged to the console. | |
105 */ | |
106 verbose: { | |
107 type: Boolean, | |
108 value: false | |
109 }, | |
110 | |
111 /** | |
112 * The most recent request made by this element. | |
113 */ | |
114 lastRequest: { | |
115 type: Object, | |
116 notify: true, | |
117 readOnly: true | |
118 }, | |
119 | |
120 /** | |
121 * True while lastRequest is in flight. | |
122 */ | |
123 loading: { | |
124 type: Boolean, | |
125 notify: true, | |
126 readOnly: true | |
127 }, | |
128 | |
129 /** | |
130 * lastRequest's response. | |
131 * | |
132 * Note that lastResponse and lastError are set when | |
133 * lastRequest finishes, so if loading is true, lastResponse and lastErr or | |
Bons
2016/02/13 17:18:24
> 80 chars.
nodir
2016/02/17 02:02:12
Done.
| |
134 * will correspond to the result of the previous request. | |
135 */ | |
136 lastResponse: { | |
137 type: Object, | |
138 notify: true, | |
139 readOnly: true | |
140 }, | |
141 | |
142 /** | |
143 * lastRequest's error, if any. | |
144 * May be an instance of rpcExplorer.GrpcError which has response code | |
145 * and description. | |
146 */ | |
147 lastError: { | |
148 type: Object, | |
149 notify: true, | |
150 readOnly: true | |
151 }, | |
152 | |
153 /** | |
154 * Length of time in milliseconds to debounce multiple automatically | |
155 * generated requests. | |
156 */ | |
157 debounceDuration: { | |
158 type: Number, | |
159 value: 0, | |
160 notify: true | |
161 } | |
162 }, | |
163 | |
164 _loadingChanged: function() { | |
165 this._setLoading(this.$.ajax.loading); | |
Bons
2016/02/13 17:18:23
why not bind to iron-ajax's loading property? http
nodir
2016/02/17 02:02:12
two-way data-binding does not work with read-only
Bons
2016/02/17 19:27:47
in that case, instead of querying for the element,
| |
166 }, | |
167 | |
168 _onTimeoutChanged: function(val) { | |
169 var headers = this.$.ajax.requestHeaders; | |
170 if (val) { | |
171 headers['X-Prpc-Timeout'] = val + 'm'; | |
172 } else { | |
173 delete headers['X-Prpc-Timeout'] | |
174 } | |
175 }, | |
176 | |
177 _generateUrl: function(insecure, host, service, method) { | |
178 if (!host || !service || !method) { | |
179 return ''; | |
180 } | |
181 | |
182 var protocol; | |
183 switch (insecure) { | |
Bons
2016/02/13 17:18:23
i think this block would read better as a set of i
nodir
2016/02/17 02:02:12
simplified a bit, but I am not sure how to be less
| |
184 case true: | |
185 protocol = 'http:'; | |
186 break; | |
187 case undefined: | |
188 case null: | |
189 if (host == document.location.host) { | |
190 protocol = document.location.protocol; | |
191 break; | |
192 } | |
193 default: | |
194 protocol = 'https:'; | |
195 } | |
196 | |
197 return protocol + '//' + host + '/prpc/' + service + '/' + method; | |
198 }, | |
199 | |
200 send: function() { | |
201 if (!this.$.ajax.url) { | |
202 throw new Error('rpc-call is in invalid state'); | |
Bons
2016/02/13 17:18:25
"new" isn't required.
throw Error(...)
nodir
2016/02/17 02:02:12
Done.
| |
203 } | |
204 return this.$.ajax.generateRequest(); | |
205 }, | |
206 | |
207 _onRequest: function() { | |
208 this._setLastRequest(this.request); | |
209 var e = { | |
210 host: this.host, | |
211 service: this.service, | |
212 method: this.method, | |
213 request: this.request | |
214 }; | |
215 this.fire('request', e, {bubbles: false}); | |
216 }, | |
217 _onError: function(e) { | |
Bons
2016/02/13 17:18:25
put a blank line between top-level function declar
nodir
2016/02/17 02:02:12
Done.
| |
218 this._done(e.detail.request.xhr); | |
219 }, | |
220 _onResponse: function(e) { | |
221 this._done(e.detail.xhr); | |
222 }, | |
223 _done: function(xhr) { | |
224 var ajax = this.$.ajax; | |
225 | |
226 function parseResponse() { | |
227 if (ajax.lastError && typeof xhr.status !== 'number') { | |
Bons
2016/02/13 17:18:25
you mix strict equality in with non strict. I woul
nodir
2016/02/17 02:02:12
I've updated code to use only ===, except for case
| |
228 // Response was not received. | |
229 return { | |
230 error: ajax.lastError | |
231 }; | |
232 } | |
233 | |
234 var codeHeader = xhr.getResponseHeader('X-Prpc-Grpc-Code'); | |
235 if (!codeHeader) { | |
236 return { | |
237 error: new Error( | |
238 'Invalid response: no X-Prpc-Grpc-Code response header') | |
239 }; | |
240 } | |
241 | |
242 var code; | |
243 try { | |
244 code = parseInt(codeHeader, 10); | |
245 if (code == null) { | |
246 throw new Error('code is not defined'); | |
Bons
2016/02/13 17:18:25
throw Error
nodir
2016/02/17 02:02:12
Done.
| |
247 } | |
248 } catch (e) { | |
249 return { | |
250 error: new Error( | |
251 'Invalid X-Prpc-Grpc-Code response header "' + codeHeader + | |
252 '": ' + e | |
253 ) | |
254 }; | |
255 } | |
256 | |
257 if (code !== rpcExplorer.Codes.OK) { | |
258 return { | |
259 error: new rpcExplorer.GrpcError(code, xhr.responseText) | |
260 }; | |
261 } | |
262 return { | |
263 response: ajax.lastResponse | |
264 }; | |
265 } | |
266 | |
267 var result = parseResponse(); | |
268 result.response = result.response || null; | |
269 result.error = result.error || null; | |
270 | |
271 this._setLastResponse(result.response); | |
272 this._setLastError(result.error); | |
273 | |
274 this.fire('response', result, {bubbles: false}); | |
275 } | |
276 }); | |
277 </script> | |
278 </dom-module> | |
OLD | NEW |