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 <link rel="import" href="../bower_components/polymer/polymer.html"> |
| 8 |
| 9 <link rel="import" href="rpc-code.html"> |
| 10 <link rel="import" href="rpc-error.html"> |
| 11 <link rel="import" href="rpc-call.html"> |
| 12 |
| 13 <!-- |
| 14 The `rpc-client` element can send a RPC request. |
| 15 Supports pRPC. |
| 16 Protocol: https://godoc.org/github.com/luci/luci-go/common/prpc#hdr-Protocol |
| 17 --> |
| 18 <dom-module id="rpc-client"> |
| 19 <script> |
| 20 'use strict'; |
| 21 |
| 22 Polymer({ |
| 23 is: 'rpc-client', |
| 24 |
| 25 properties: { |
| 26 /** |
| 27 * If true, use HTTP instead of HTTPS. |
| 28 * If null or undefined (default), determined automatically: |
| 29 * - If host equals current host and current protocol is http, then |
| 30 * false. |
| 31 * - otherwise true. |
| 32 */ |
| 33 insecure: { |
| 34 type: Boolean, |
| 35 value: null |
| 36 }, |
| 37 |
| 38 /** |
| 39 * pRPC server host, defaults to current document host. |
| 40 */ |
| 41 host: { |
| 42 type: String, |
| 43 value: document.location.host, |
| 44 }, |
| 45 |
| 46 /** |
| 47 * Full service name, including package name. |
| 48 */ |
| 49 service: String, |
| 50 |
| 51 /** |
| 52 * Service method name. |
| 53 */ |
| 54 method: String, |
| 55 |
| 56 /** |
| 57 * Request object. |
| 58 */ |
| 59 request: { |
| 60 type: Object, |
| 61 value: function() { |
| 62 return {}; |
| 63 } |
| 64 }, |
| 65 |
| 66 /** |
| 67 * Request timeout in milliseconds. |
| 68 */ |
| 69 timeout: { |
| 70 type: Number, |
| 71 value: 0, |
| 72 }, |
| 73 |
| 74 /** |
| 75 * If true, automatically sends a request when host, service, method |
| 76 * or request changes. |
| 77 */ |
| 78 auto: { |
| 79 type: Boolean, |
| 80 value: false |
| 81 }, |
| 82 |
| 83 /** |
| 84 * If true, error messages will automatically be logged to the console. |
| 85 */ |
| 86 verbose: { |
| 87 type: Boolean, |
| 88 value: false |
| 89 }, |
| 90 |
| 91 /** |
| 92 * The most recent call made by this client. |
| 93 */ |
| 94 lastCall: { |
| 95 type: Object, |
| 96 notify: true, |
| 97 readOnly: true |
| 98 }, |
| 99 |
| 100 /** |
| 101 * True while lastCall is in flight. |
| 102 */ |
| 103 loading: { |
| 104 type: Boolean, |
| 105 notify: true, |
| 106 readOnly: true, |
| 107 value: false |
| 108 }, |
| 109 |
| 110 /** |
| 111 * lastCall's response. |
| 112 * |
| 113 * Note that lastResponse, lastCode and lastError are set when |
| 114 * lastCall finishes, so if loading is true, they will correspond to the |
| 115 * result of the previous request. |
| 116 */ |
| 117 lastResponse: { |
| 118 type: Object, |
| 119 notify: true, |
| 120 readOnly: true |
| 121 }, |
| 122 |
| 123 /** |
| 124 * lastCall's response code. |
| 125 */ |
| 126 lastCode: { |
| 127 type: Number, |
| 128 notify: true, |
| 129 readOnly: true |
| 130 }, |
| 131 |
| 132 /** |
| 133 * lastCall's error, if any. |
| 134 * May be an instance of luci.rpc.GrpcError which has response code |
| 135 * and description. |
| 136 */ |
| 137 lastError: { |
| 138 type: Number, |
| 139 notify: true, |
| 140 readOnly: true |
| 141 }, |
| 142 |
| 143 /** |
| 144 * Length of time in milliseconds to debounce multiple automatically |
| 145 * generated requests. |
| 146 */ |
| 147 debounceDuration: { |
| 148 type: Number, |
| 149 value: 0, |
| 150 notify: true |
| 151 } |
| 152 }, |
| 153 |
| 154 observers: [ |
| 155 '_callOptionsChanged(host, service, method, request.*, auto)' |
| 156 ], |
| 157 |
| 158 _canCall: function () { |
| 159 return this.host && this.service && this.method; |
| 160 }, |
| 161 |
| 162 _callOptionsChanged: function() { |
| 163 this.debounce('call', function() { |
| 164 if (this.auto && this._canCall()) { |
| 165 this.call(); |
| 166 } |
| 167 }, this.debounceDuration); |
| 168 }, |
| 169 |
| 170 /** |
| 171 * Send an RPC request. |
| 172 * @return {!RpcCallElement} |
| 173 */ |
| 174 call: function() { |
| 175 var call = document.createElement('rpc-call'); |
| 176 call.send({ |
| 177 insecure: this.insecure, |
| 178 host: this.host, |
| 179 service: this.service, |
| 180 method: this.method, |
| 181 request: this.request, |
| 182 timeout: this.timeout |
| 183 }); |
| 184 this._setLastCall(call); |
| 185 |
| 186 call.completes.then( |
| 187 this._onResponse.bind(this) |
| 188 ).catch( |
| 189 this._onError.bind(this, call) |
| 190 ); |
| 191 |
| 192 this._setLoading(true); |
| 193 this.fire('request', {call: call}, {bubbles: false}); |
| 194 return call; |
| 195 }, |
| 196 |
| 197 _onError: function(call, error) { |
| 198 this._onCallComplete(call); |
| 199 if (this.verbose) { |
| 200 console.error(error); |
| 201 } |
| 202 this.fire('error', { |
| 203 call: call, |
| 204 error: error |
| 205 }, {bubbles: false}); |
| 206 }, |
| 207 |
| 208 _onResponse: function(call) { |
| 209 this._onCallComplete(call); |
| 210 this.fire('response', {call: call}, {bubbles: false}); |
| 211 }, |
| 212 |
| 213 _onCallComplete: function(call) { |
| 214 if (call === this.lastCall) { |
| 215 this._setLastError(call.error); |
| 216 this._setLastResponse(call.response); |
| 217 this._setLastCode(call.code); |
| 218 this._setLoading(false); |
| 219 } |
| 220 } |
| 221 |
| 222 }); |
| 223 </script> |
| 224 </dom-module> |
OLD | NEW |