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="/static/common/rpc/rpc-client.html"> |
| 10 |
| 11 <link rel="import" href="rpc-descriptor-util.html"> |
| 12 <link rel="import" href="rpc-editor.html"> |
| 13 |
| 14 <!-- The `rpc-method` is a service method page --> |
| 15 <dom-module id="rpc-method"> |
| 16 <template> |
| 17 <style> |
| 18 rpc-editor { |
| 19 height: 300px; |
| 20 } |
| 21 button { |
| 22 margin: 5px; |
| 23 } |
| 24 </style> |
| 25 |
| 26 <div on-keypress="_onKeypress"> |
| 27 <rpc-client |
| 28 id="client" |
| 29 service="[[service]]" |
| 30 method="[[method]]" |
| 31 request="[[requestObject]]" |
| 32 on-response="_onCallComplete" |
| 33 on-error="_onCallComplete"> |
| 34 </rpc-client> |
| 35 <div>[[methodDesc.source_code_info.leading_comments]]</div> |
| 36 <hr> |
| 37 |
| 38 <p>Request:</p> |
| 39 <div class="row"> |
| 40 <div class="col-md-7"> |
| 41 <rpc-editor value="{{requestText}}" |
| 42 description="[[description]]" |
| 43 root-type-name="[[requestTypeName]]"></rpc-editor> |
| 44 </div> |
| 45 <div class="col-md-3"> |
| 46 <p>Ctrl+Space for Autocomplete</p> |
| 47 <p>Shift+Enter for Send</p> |
| 48 </div> |
| 49 </div> |
| 50 |
| 51 <div> |
| 52 <button on-tap="send">Send</button> |
| 53 </div> |
| 54 |
| 55 <div class="alert alert-danger" role="alert" hidden="[[!error]]"> |
| 56 <template is="dom-if" if="[[error.isGrpcError]]"> |
| 57 <div> |
| 58 Code: [[error.code]] |
| 59 <template is="dom-if" if="[[error.codeName]]"> |
| 60 ([[error.codeName]]) |
| 61 </template> |
| 62 </div> |
| 63 <div>Description: [[error.description]]</div> |
| 64 </template> |
| 65 |
| 66 <template is="dom-if" if="[[!error.isGrpcError]]"> |
| 67 [[error]] |
| 68 </template> |
| 69 </div> |
| 70 |
| 71 <div class="row"> |
| 72 <div class="col-md-7"> |
| 73 <rpc-editor value="[[responseText]]"></rpc-editor> |
| 74 </div> |
| 75 </div> |
| 76 </div> |
| 77 </template> |
| 78 |
| 79 <script> |
| 80 'use strict'; |
| 81 |
| 82 Polymer({ |
| 83 is: 'rpc-method', |
| 84 |
| 85 properties: { |
| 86 /** @type {FileDescriptorSet} */ |
| 87 description: Object, |
| 88 |
| 89 service: String, |
| 90 |
| 91 method: String, |
| 92 |
| 93 /** @type {MethodDescriptorProto} */ |
| 94 methodDesc: { |
| 95 type: Object, |
| 96 computed: '_resolveMethod(description, service, method)' |
| 97 }, |
| 98 |
| 99 requestTypeName: { |
| 100 type: String, |
| 101 computed: '_getRequestTypeName(methodDesc)' |
| 102 }, |
| 103 |
| 104 /** "request" query string parameter. */ |
| 105 request: { |
| 106 type: String, |
| 107 value: '{}', |
| 108 observer: '_onRequestChanged', |
| 109 notify: true |
| 110 }, |
| 111 |
| 112 /** Request editor text. */ |
| 113 requestText: String, |
| 114 |
| 115 /** Parsed from requestText. */ |
| 116 requestObject: Object, |
| 117 |
| 118 /** Response editor text. */ |
| 119 responseText: String, |
| 120 |
| 121 error: { |
| 122 type: Object, |
| 123 value: null |
| 124 } |
| 125 }, |
| 126 |
| 127 _resolveMethod: function(desc, service, method) { |
| 128 if (!desc || !service || !method) { |
| 129 return null; |
| 130 } |
| 131 var methodDesc = rpcExplorer.descUtil.resolve( |
| 132 desc, service + '.' + method); |
| 133 return methodDesc && methodDesc.type === 'method' && methodDesc.desc; |
| 134 }, |
| 135 |
| 136 _getRequestTypeName: function(methodDesc) { |
| 137 return (methodDesc && |
| 138 rpcExplorer.descUtil.trimPrefixDot(methodDesc.input_type)); |
| 139 }, |
| 140 |
| 141 _onRequestChanged: function() { |
| 142 try { |
| 143 this.requestObject = JSON.parse(this.request); |
| 144 } catch (e) { |
| 145 console.error('Invalid request: ' + this.request); |
| 146 this.requestText = this.request; |
| 147 return; |
| 148 } |
| 149 |
| 150 // Reformat the request read from query string parameter |
| 151 // because it gets corrupted there. |
| 152 this.requestText = JSON.stringify(this.requestObject, null, 4); |
| 153 }, |
| 154 |
| 155 _onKeypress: function(e) { |
| 156 if (e.key === 'Enter' && e.shiftKey) { |
| 157 this.send(); |
| 158 e.preventDefault(); |
| 159 } |
| 160 }, |
| 161 |
| 162 send: function() { |
| 163 this.error = null; |
| 164 try { |
| 165 this.requestObject = JSON.parse(this.requestText); |
| 166 |
| 167 // Reformat request |
| 168 this.requestText = JSON.stringify(this.requestObject, null, 4); |
| 169 |
| 170 // Update URL without a refresh. |
| 171 history.replaceState( |
| 172 history.state, document.title, "?request=" + this.requestText); |
| 173 |
| 174 // Actually send the request. |
| 175 this.$.client.call(); |
| 176 } catch (e) { |
| 177 this.error = e; |
| 178 console.error(this.error) |
| 179 } |
| 180 }, |
| 181 |
| 182 _onCallComplete: function() { |
| 183 var client = this.$.client; |
| 184 if (client.lastError) { |
| 185 console.error(client.lastError); |
| 186 } |
| 187 |
| 188 if (client.lastResponse) { |
| 189 this.responseText = JSON.stringify(client.lastResponse, null, 4); |
| 190 } else { |
| 191 this.responseText = ''; |
| 192 } |
| 193 |
| 194 this.error = client.lastError; |
| 195 if (this.error instanceof luci.rpc.GrpcError) { |
| 196 this.error = { |
| 197 isGrpcError: true, |
| 198 code: this.error.code, |
| 199 codeName: luci.rpc.CodeName(this.error.code), |
| 200 description: this.error.description |
| 201 }; |
| 202 } |
| 203 } |
| 204 }); |
| 205 </script> |
| 206 </dom-module> |
OLD | NEW |