This repository has been archived by the owner on Feb 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raml-request-panel.html
822 lines (761 loc) · 29.6 KB
/
raml-request-panel.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
<!--
@license
Copyright 2016 The Advanced REST client authors <arc@mulesoft.com>
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="../paper-styles/typography.html">
<link rel="import" href="../paper-tabs/paper-tabs.html">
<link rel="import" href="../paper-tabs/paper-tab.html">
<link rel="import" href="../iron-pages/iron-pages.html">
<link rel="import" href="../raml-body-editor-panel/raml-body-editor-panel.html">
<link rel="import" href="../raml-request-headers-editor/raml-request-headers-editor.html">
<link rel="import" href="../raml-docs-response-panel/raml-docs-response-panel.html">
<link rel="import" href="../raml-docs-method-viewer/raml-docs-method-viewer.html">
<link rel="import" href="../raml-request-parameters-editor/raml-request-parameters-editor.html">
<link rel="import" href="../iron-media-query/iron-media-query.html">
<link rel="import" href="../raml-request-url-editor/raml-request-url-editor.html">
<link rel="import" href="../paper-button/paper-button.html">
<link rel="import" href="../paper-spinner/paper-spinner.html">
<link rel="import" href="../authorization-panel/authorization-panel.html">
<link rel="import" href="../oauth-authorization/oauth2-authorization.html">
<link rel="import" href="../oauth-authorization/oauth1-authorization.html">
<link rel="import" href="../promise-polyfill/promise-polyfill-lite.html">
<link rel="import" href="../fetch-polyfill/fetch-polyfill.html">
<link rel="import" href="../paper-dialog-scrollable/paper-dialog-scrollable.html">
<link rel="import" href="../paper-dialog/paper-dialog.html">
<link rel="import" href="../paper-toast/paper-toast.html">
<link rel="import" href="../raml-request-parameters-editor/raml-request-parameters-model.html">
<link rel="import" href="raml-request-panel-simple-xhr.html">
<!--
The request panel view for the request defined as a RAML method.
It is a main view element for the API console to display the request panel related to the RAML
specification.
The element has it's own XHR / Fetch transport method and it will be used if the hostng application
do not handle the `api-console-request` event.
When the user request to make the HTTP request then cancellable `api-console-request` event will
be fired with the request details (see below).
The hosting application, if it about to use different transport method, should cancel the event by
calling `preventDefault()` function on the event (and possibly `stopPropagation()`) and handle the
request. If the event was not prevented (canceled) then internall Fetch/XHR will be used.
When the request is ready then the hosting app must fire the `api-console-response` event with
created Request and Response objects. This element listens on the `window` property for the
`api-console-response` event.
## Events
### api-console-request
This event is fired when the user request to make a HTTP request.
This event will have the following properties set on the `detail` object:
Property | Type | Description
----------------|-------------|----------
`url` | String | The request URL
`method` | String | The HTTP method
`headers` | String | Headers to send
`payload` | String | Payload to send
`auth` | Object | Optional. For some authorization methodss (like NTLM) the authorization header or query param can't be set and the authorization must be made on the connection. In this cases the auth object will be set with `type` and `settings` properties. While `type` is the name of the authorization method, the `settings` object depends on the authorization method and may vary. Detailed documentation for the auth methods is in the `auth-methods` element.
### api-console-response
This event must be fired when the hosring app finish the request. It must contains generated Request
and Response object according to the [Fetch specification](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch).
Becaue the Fetch API is a new API not all browsers support it. In this case the polyfill must be used
in the hosted app. Add the `fetch-polyfill` element (advanced-rest-client/fetch-polyfill) to the
hosted app to have the support. It is recommended to use this element so the polyfill will be loaded
only once. See the `raml-request-panel-simple-xhr` element for example implementation.
Property | Type | Description
----------------|-------------|----------
`request` | Object | The request object as defined in the Fetch API spec.
`response` | Object | The response object as defined in the Fetch API spec.
`isXhr` | Boolean | If not set the element assumes it's true. Indicated if the transport method doesn't support advanced timings and redirects information. See below.
`error` | Error | When the request / response is errored (`request.ok` equals `false`) then the error object should be set with the human readable message that will be displayed to the user.
`loadingTime` | Number | The response full loading time
See `Advanced transport options` for more event options.
### Example
```
<raml-request-panel
method="[[ramlMethod]]"
redirect-url="http://oauth.redirect.url"
request="{{request}}"
response="{{response}}"
response-error="{{responseError}}"
loading-time="{{loadingTime}}"></raml-request-panel>
```
## Advanced transport options
The response panel in the ARC elements is able to display the response in simple and advanced view.
Simple is meant to be used when the HTTP request has been made by the simple transports like XHR or
Fetch. It just displays the response status, headers and paylaod.
Advanced view is reserved for transport methods that are able to generate additional informations
about the request and resposne. This information is timings for the request/response, timings for
the redirects and information about redirects.
When the advanced options are set then the `isXhr` flag on the `api-console-response` event's detail
object must be set to true.
#### timings
The `timings` propery added to the `api-console-response` is the request / response timings as
defined in HAR 1.2 spec. For example:
```
"timings": {
"blocked": 0,
"dns": -1,
"connect": 15,
"send": 20,
"wait": 38,
"receive": 12,
"ssl": -1,
"comment": ""
}
```
If the `timings` property is set the `loadingTime` property is optional since it will be calculated
from the detailed timing.
#### redirectTimings
The `redirectTimings` propery added to the `api-console-response` is the list of the `timings`
objects as defined in HAR 1.2 specification.
The list should be ordered list of redirections. For example:
```
"redirect-timings": [{
"blocked": 0,
"dns": -1,
"connect": 15,
"send": 20,
"wait": 38,
"receive": 12,
"ssl": -1,
"comment": ""
}]
```
#### redirects
The `redirects` property added to the `api-console-response` event is the list of objects. Each
object should have the `headers` property as a HTTP headers string, `status` as a HTTP status
and optionally `statusText`. It is consisted with the `Response` object except the headers are
String instead of the Headers object.
```
"redirects": [Response {
"status": 301,
"statusText": "Moved Permanently",
"headers": "Content-lenght: 0"
}]
```
#### sourceMessage
The HTTP source message sent to the server. It should be full message from the message header to
the request body.
### Advanced event example
```
var event = new CustomEvent('api-console-response', {
cancelable: true,
bubbles: true,
composed: true,
detail: {
isXhr: true,
request: request,
response: response,
error: new Error('Dummy error'), // Has the response details so it shouldn't be set.
loadingTime: 125, // This is optional because timings is set
timings: { dns: 123, ... }
redirectTimings: [{ dns: 123, ... }],
redirects: [redirectResponse1, ...],
sourceMessage: 'HTTP/1.1 200 OK\n ....'
}
});
document.body.dispatchEvent(event);
```
### Styling
`<raml-request-panel>` provides the following custom properties and mixins for styling:
Custom property | Description | Default
----------------|-------------|----------
`--raml-request-panel` | Mixin applied to the element | `{}`
`--raml-request-panel-panel-border-color` | Border color of each block in the tabs | `rgba(0, 0, 0, 0.24)`
`--raml-request-panel-container` | Mixin applied to the main content container | `{}`
`--raml-request-panel-container-narrow` | Mixin applied to the main content container when layout is narrow | `{}`
`--action-button` | Mixin applied to the action button | `{}`
`--action-button-hover` | Mixin applied to :hover state for the action button | `{}`
`--action-button-disabled` | Mixin applied to disbaled action button | `{}`
`--primary-color` | background-color of the main action button | `--primary-color`
`--primary-action-color` | Color of the main action button | `--primary-action-color`
`--action-accent-button-disabled-color` | Color of disabled action button | ``
`--primary-button-background-color` | Background color of the primary button. |`--accent-color`
`--primary-button-color` | Font color of the primary button | `#fff`
`--primary-button-hover-background-color` | Background color of the primary button when hovered. |`--accent-color`
`--primary-button-hover-color` | Font color of the primary button when hovered. | `#fff`
You can set the `narrow` property so the element will be rendered in the mobile view.
This property will be propagated to all sub-elements that uses this property to change layout.
@group RAML Elements
@element raml-request-panel
@demo demo/index.html
-->
<dom-module id="raml-request-panel">
<template>
<style>
:host {
display: block;
@apply(--raml-request-panel);
}
*[hidden] {
display: none !important;
}
.content {
height: 100%;
@apply(--layout-vertical);
@apply(--raml-request-panel-container);
}
h2 {
@apply(--paper-font-subhead);
margin: 8px 12px;
font-weight: var(--raml-request-panel-navigation-title-font-weight, 500);
@apply --raml-request-panel-popup-header;
}
.buttons {
@apply --raml-request-panel-popup-btn-container;
}
[dialog-confirm] {
@apply --raml-request-panel-popup-btn-confirm;
}
[dialog-confirm]:hover {
@apply --raml-request-panel-popup-btn-confirm-hover;
}
[dialog-dismiss] {
@apply --raml-request-panel-popup-btn-dismiss;
}
[dialog-dismiss]:hover {
@apply --raml-request-panel-popup-btn-dismiss-hover;
}
*[hidden] {
display: none !important;
}
iron-pages>* {
border: 1px var(--raml-request-panel-panel-border-color, transparent) solid;
min-height: 120px;
}
.action-bar {
@apply(--layout-horizontal);
@apply(--layout-end-justified);
@apply(--layout-center);
margin-top: 8px;
}
.action-button {
background-color: var(--primary-button-background-color, --accent-color);
color: var(--primary-button-color, #fff);
@apply(--action-button);
}
.action-button:hover {
background-color: var(--primary-button-hover-background-color, --accent-color);
color: var(--primary-button-hover-color, #fff);
@apply(--action-button-hover);
}
.action-button[disabled] {
background-color: var(--action-accent-button-disabled-color);
color: #fff;
@apply(--action-button-disabled);
}
.url-editor {
@apply(--layout-horizontal);
@apply(--layout-center);
}
raml-request-url-editor {
@apply(--layout-flex);
}
:host([narrow]) .content {
@apply(--layout-vertical);
@apply(--raml-request-panel-container-narrow);
}
:host([narrow]) raml-request-url-editor {
width: auto;
}
paper-spinner {
margin-right: 8px;
}
paper-dialog {
@apply(--raml-request-panel-popup);
}
.panel-warning {
width: 16px;
height: 16px;
margin-left: 4px;
color: #FF5722;
}
.url-invalid-info {
@apply --arc-font-body1;
}
</style>
<iron-media-query query="(max-width: [[narrowWidth]])" query-matches="{{narrow}}"></iron-media-query>
<raml-request-parameters-model query-parameters="[[method.queryParameters]]" uri-parameters="[[method.allUriParameters]]" query-model="{{queryModel}}" uri-model="{{uriModel}}" has-query-parameters="{{hasQueryParameters}}" has-uri-parameters="{{hasUriParameters}}" has-parameters="{{hasParameters}}"></raml-request-parameters-model>
<div class="content">
<div class="url-editor" hidden$="[[noUrlEditor]]">
<raml-request-url-editor auto-validate required value="{{url}}" invalid="{{urlInvalid}}" base-uri="[[_baseUri]]" endpoint-uri="[[method.relativeUri]]" query-model="[[queryModel]]" uri-model="[[uriModel]]"></raml-request-url-editor>
</div>
<paper-tabs selected="{{selectedTab}}">
<paper-tab hidden$="[[noAuth]]">
Authorization
<iron-icon icon="arc:warning" class="panel-warning" hidden$="[[authValid]]" title="Fill up missing auth data"></iron-icon>
</paper-tab>
<paper-tab>Parameters</paper-tab>
<paper-tab>Headers</paper-tab>
<paper-tab hidden$="[[!isPayloadRequest]]">Body</paper-tab>
</paper-tabs>
<iron-pages selected="{{selectedTab}}">
<authorization-panel hidden$="[[noAuth]]" secured-by="[[method.securedBy]]" redirect-url="[[redirectUrl]]" auth-required="{{authRequired}}" auth-valid="{{authValid}}"></authorization-panel>
<raml-request-parameters-editor query-model="[[queryModel]]" uri-model="[[uriModel]]" has-query-parameters="[[hasQueryParameters]]" has-uri-parameters="[[hasUriParameters]]" has-parameters="[[hasParameters]]"></raml-request-parameters-editor>
<raml-request-headers-editor narrow="[[narrow]]" raml-headers="[[method.headers]]" content-type="{{contentType}}" is-payload="[[isPayloadRequest]]" value="{{headers}}"></raml-request-headers-editor>
<raml-body-editor-panel narrow="[[narrow]]" body="[[method.body]]" hidden$="[[!isPayloadRequest]]" content-type="{{contentType}}" value="{{payload}}"></raml-body-editor-panel>
</iron-pages>
<div class="action-bar">
<paper-spinner alt="Loading contacts list" active="[[loadingRequest]]"></paper-spinner>
<span class="url-invalid-info" hidden$="[[!urlInvalid]]">Request URL is invalid.</span>
<paper-button class="action-button" on-tap="execute" hidden$="[[!authValid]]" disabled="[[urlInvalid]]">Send</paper-button>
<template is="dom-if" if="[[!authValid]]">
<paper-button class="action-button" on-tap="authAndExecute" disabled="[[urlInvalid]]">Authorize and send</paper-button>
</template>
</div>
</div>
<oauth2-authorization></oauth2-authorization>
<oauth1-authorization></oauth1-authorization>
<raml-request-panel-simple-xhr id="xhr"></raml-request-panel-simple-xhr>
<paper-toast text="Authorization is required for this endpoint" id="authRequired" horizontal-align="right" horizontal-offset="12"></paper-toast>
<paper-toast text="Fill in the authorization form first" id="authFormError" horizontal-align="right" horizontal-offset="12"></paper-toast>
</template>
<script>
Polymer({
is: 'raml-request-panel',
/**
* Fired when the response has been recorded and request, response, redirects and timings data
* are set.
*
* @event api-console-response-ready
*
* @param {Boolean} isXhr True if the transport method is a basic transport.
* @param {Response} response The response object
* @param {Error} responseError Error object if the response is errored
* @param {Request} request The request object.
* @param {Number} loadingTime Request loading time
* @param {Object} timings As defined in HAR 1.2 timnings object
* @param {Array<Object>} redirectTimings List of redirect timings
* @param {Array} redirects List of redirect Responses
* @param {String} sourceMessage Source HTTP message sent to the server.
*/
/**
* Event fired when the console want to make a request to an endpoint.
* If the hosting application can handle the event and will make a request on behalf of the
* console, this event must be cancelled. Otherwise internal XHR request will be made.
*
* @event api-console-request
* @param {String} method The HTTP method name
* @param {String} url Endpoint's URL
* @param {String} headers A HTTP headers string (as defined in the spec)
* @param {String} payload A message body (encoded if necessary).
* @param {Object} auth Collected authorization data. In most cases it is
* unused. It is required for OAuth1 to pass this data to handle request signing
* by the `oauth-authorization` element.
*/
properties: {
/**
* A RAML node representing a method node in RAML definition.
* It should be obtained from the `raml-path-to-object` element.
*/
method: {
type: Object,
observer: '_methodChanged'
},
// Selected request tab.
selectedTab: {
type: Number,
value: 0,
notify: true
},
// Current content type.
contentType: {
type: String,
notify: true
},
// Computed value if the method can carry a payload
isPayloadRequest: {
type: Boolean,
value: false,
computed: '_computeIsPayloadRequest(method.*)'
},
// Headers for the request.
headers: String,
// Body for the request
payload: String,
// Current URL
url: {
type: String,
notify: true
},
/**
* If set it will renders the view in the narrow layout.
*/
narrow: {
type: Boolean,
notify: true,
reflectToAttribute: true
},
// A widith below which the `narrow` property will be set to true.
narrowWidth: {
type: String,
value: '768px'
},
// If true then the request is currently loaded.
loadingRequest: {
type: Boolean,
notify: true,
readOnly: true,
value: false
},
// If true then the request was made using the XHR object (it has less data in the response).
responseIsXhr: {
type: Boolean,
notify: true,
readOnly: true
},
/**
* The request object created by the transport.
* It should be the `Request` object as defined in the Fetch API spec.
* This element provides the polyfill for this API.
*/
request: {
type: Request,
notify: true,
readOnly: true
},
/**
* The response object from the transport.
* It should be the `Response` object as defined in the Fetch API spec.
* This element provides the polyfill for this API.
*/
response: {
type: Response,
notify: true,
readOnly: true
},
// Set when the response errored.
responseError: {
type: Object,
notify: true,
readOnly: true
},
// Response full loading time.
loadingTime: {
type: Number,
notify: true,
readOnly: true
},
/**
* If the transport method is able to collect detailed information about request timings
* then this value will be set. It's the `timings` property from the HAR 1.2 spec.
*/
timings: {
type: Object,
notify: true,
readOnly: true
},
/**
* If the transport method is able to collect detailed information about redirects timings
* then this value will be set. It's a list of `timings` property from the HAR 1.2 spec.
*/
redirectTimings: {
type: Array,
notify: true,
readOnly: true
},
/**
* It will be set if the transport method can generate information about redirections.
*/
redirects: {
type: Array,
notify: true,
readOnly: true
},
/**
* Http message sent to the server.
*
* This information should be available only in case of advanced HTTP transport.
*/
sourceMessage: {
type: String,
notify: true,
readOnly: true
},
// Received from the authorization panel state if authorization is required
authRequired: Boolean,
// Received from the authorization panel state if authorization data is valid
authValid: Boolean,
// OAuth2 redirect URL
redirectUrl: String,
// Selected by the user auth method (if any)
authMethod: String,
// Current authorization settings.
authSettings: Object,
/**
* Computed value when the URL change.
* If not valid form submission won't be possible.
*/
urlInvalid: Boolean,
/**
* Hides the URL editor from the view.
* The editor is still in the DOM and the `urlInvalid` property still will be set.
*/
noUrlEditor: Boolean,
/**
* A base URI for the API. To be set if RAML spec is missing `baseUri`
* declaration and this produces invalid URL input. This information
* is passed to the URL editor that prefixes the URL with `baseUri` value
* if passed URL is a relative URL.
*/
baseUri: {
type: String,
observer: '_baseUriChanged'
},
// Computed baseUri value passed to the URL editor.
_baseUri: String,
// Computed model for query parameters
queryModel: Object,
// Computed model for URI parameters
uriModel: Object,
// Computed value from the parameters model
hasUriParameters: Boolean,
// Computed value from the parameters model
hasQueryParameters: Boolean,
// Computed value from the parameters model
hasParameters: Boolean,
/**
* Value computed when RAML method change.
* It is set to true when authorization is not defined for current
* endpoint's method.
*/
noAuth: {
type: Boolean,
readOnly: true,
value: true
}
},
observers: [
'_isPayloadRequestChanged(isPayloadRequest)',
'_noAuthChanged(noAuth)',
'_selectedTabChanged(selectedTab)',
'_methodBaseUriChanged(method.baseUri)'
],
listeners: {
'authorization-settings-changed': '_authSettingsChanged'
},
attached: function() {
this.listen(window, 'api-console-response', '_responseHandler');
},
detached: function() {
this.unlisten(window, 'api-console-response', '_responseHandler');
},
// Called when RAML method definition changed.
_methodChanged: function(method) {
this._updateAuthorizationState(method);
},
/**
* Updates `noAuth` property depending on number of security schemes
* defined in RAML for this method.
*
* @param {Object} ramlMethod RAML method definition.
*/
_updateAuthorizationState: function(ramlMethod) {
var state = true;
if (ramlMethod && ramlMethod.securedBy && ramlMethod.securedBy.length) {
var methods = [];
var allowed = ['Digest Authentication', 'Basic Authentication',
'OAuth 1.0', 'OAuth 2.0', 'x-custom'];
ramlMethod.securedBy.forEach(function(item) {
if (!item) {
return;
}
if (allowed.indexOf(item.type) !== -1) {
methods.push(item.type);
}
});
state = methods.length < 1;
}
this._setNoAuth(state);
},
// Computes boolean value if the selected object (method) can carry a payload
_computeIsPayloadRequest: function(record) {
var base = record.base;
if (!base || !base.method) {
return false;
}
return ['get', 'head'].indexOf(base.method) === -1;
},
/**
* Refreshes tabs selection. To be called when number of tabs changes.
*/
_refreshTabs: function() {
var tabs = this.$$('paper-tabs');
if (!tabs) {
return;
}
tabs.notifyResize();
},
_isPayloadRequestChanged: function(required) {
if (!required && this.selectedTab === 3) {
if (this.authRequired) {
this.selectedTab = 0;
} else {
this.selectedTab = 1;
}
}
this._refreshTabs();
},
// Handler for `noAuth` property change.
_noAuthChanged: function(noAuth) {
if (!noAuth && this.selectedTab === 2) {
this.selectedTab = 0;
} else if (noAuth && this.selectedTab === 0) {
this.selectedTab = 1;
}
this._refreshTabs();
},
/**
* Execute the request with current settings.
* This method fires the `api-console-request` so the request can be handled by the
* hosting app.
* Hosting app must call `event.preventDefault()` on the event otherwise the console
* will attempt to make a request usnig XHR object.
*/
execute: function() {
this._clearRequestData();
this._setLoadingRequest(true);
var event = new CustomEvent('api-console-request', {
cancelable: true,
bubbles: true,
composed: true,
detail: this.serializeRequest()
});
this.dispatchEvent(event);
if (!event.defaultPrevented) {
this._executeRequest(event.detail);
}
},
/**
* Performs an authorization by bringing up the authorization
* form in a popup and after successful authorization executes the
* request.
*/
authAndExecute: function() {
if (this.authMethod === 'oauth1' || this.authMethod === 'oauth2') {
var panel = this.$$('authorization-panel');
panel.forceTokenAuthorization();
this.__pendingRequest = true;
return;
}
if (this.selectedTab !== 0) {
this.selectedTab = 0;
}
this.$.authFormError.opened = true;
},
/**
* Returns an object with the request properties.
* The object contains:
* - method (String)
* - url (String)
* - headers (String)
* - payload (String)
* - auth (Object)
*
* The `auth` property is optional and is only added to the request if simple `authorization`
* header will not work. For example NTLM auth method has to be made on a single socket
* connection (authorization and the request) so it can't be made before the request.
*
* The `auth` object contains 2 properties:
* - type (String) the authorization type - one of from the `auth-methods` element
* - settings (Object) Authorization parameters entered by the user. It vary and depends on
* selected auth method. For example in case of the NTLM it will be: `username`, `password` and
* `domain`.
*/
serializeRequest: function() {
var result = {
method: this.method.method.toUpperCase(),
url: this.url,
headers: this.headers,
payload: this.payload
};
if (this.authMethod && this.authSettings) {
result.auth = this.authSettings;
}
return result;
},
_executeRequest: function(data) {
this.$.xhr.execute(data);
},
// A handler for the `api-console-response` event.
_responseHandler: function(e) {
var isXhr = typeof e.detail.isXhr === 'boolean' ? e.detail.isXhr : true;
this._setResponseIsXhr(isXhr);
this._setResponse(e.detail.response);
this._setResponseError(e.detail.error);
this._setRequest(e.detail.request);
this._setLoadingTime(e.detail.loadingTime);
this._setTimings(e.detail.timings);
this._setRedirectTimings(e.detail.redirectTimings);
this._setRedirects(e.detail.redirects);
this._setSourceMessage(e.detail.sourceMessage);
this.fire('api-console-response-ready', {
isXhr: e.detail.isXhr,
response: e.detail.response,
responseError: e.detail.error,
request: e.detail.request,
loadingTime: e.detail.loadingTime,
timings: e.detail.timings,
redirectTimings: e.detail.redirectTimings,
redirects: e.detail.redirects,
sourceMessage: e.detail.sourceMessage
});
this._setLoadingRequest(false);
},
_clearRequestData: function() {
this._setResponseIsXhr(false);
this._setResponse(undefined);
this._setResponseError(undefined);
this._setRequest(undefined);
this._setLoadingTime(undefined);
this._setTimings(undefined);
this._setRedirectTimings(undefined);
this._setRedirects(undefined);
},
_authSettingsChanged: function(e) {
this.authMethod = e.detail.type;
this.authSettings = e.detail.settings;
if (this.__pendingRequest) {
this.__pendingRequest = false;
this.execute();
}
},
/**
* Updates the body editor if selected.
*/
_selectedTabChanged: function(selectedTab) {
if (selectedTab === 3) {
this.$$('raml-body-editor-panel').notifyResize();
}
},
/**
* Sets the `_baseUri` property to the RAML method `baseUri` only if
* `baseUri` property is not set.
*
* @param {String} methodBaseUri RAML method base URI.
*/
_methodBaseUriChanged: function(methodBaseUri) {
if (this.baseUri) {
return;
}
this._baseUri = methodBaseUri;
},
_baseUriChanged: function(value) {
if (!value) {
if (this.method) {
this._baseUri = this.method.baseUri;
}
} else {
this._baseUri = value;
}
}
});
</script>
</dom-module>