/
WebRequestConfigs.vue
243 lines (215 loc) · 8.37 KB
/
WebRequestConfigs.vue
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
<template>
<div class="component-container">
<nav class="breadcrumb" aria-label="breadcrumbs">
<ul>
<li><a href="/">Home</a></li>
<li><a href="/automations">Automations</a></li>
<li class="is-active"><a href="#" aria-current="page">Web Request</a></li>
</ul>
</nav>
<title-header>
<template v-slot:title>
Web Request
</template>
<template v-slot:subtitle>
Make Http GET requests
</template>
</title-header>
<b-table :data="webRequestConfigs">
<template #empty>
No automations set up yet. Use the form below.
</template>
<b-table-column field="name" label="Name" v-slot="props">
{{ props.row.name }}
</b-table-column>
<b-table-column field="url" label="Url" v-slot="props">
{{ props.row.url }}
</b-table-column>
<b-table-column field="is_post" label="Post" v-slot="props">
<b-icon v-if="props.row.is_post" icon="check"></b-icon>
</b-table-column>
<b-table-column field="body_json" label="Body" v-slot="props">
<span :title="props.row.body_json">{{ props.row.body_json | truncate }}</span>
</b-table-column>
<b-table-column field="profile_count" label="Used By" v-slot="props">
{{ props.row.detection_profiles.length }} profile(s)
</b-table-column>
<b-table-column field="delete" label="Delete" v-slot="props">
<b-button icon-right="trash"
type="is-danger is-outlined"
:loading="props.row.isDeleting"
@click="deleteClick(props.row)">
</b-button>
</b-table-column>
</b-table>
<div class="box is-6">
<p class="subtitle">New Config</p>
<form @submit.prevent="processForm">
<b-field label="Name">
<b-input v-model="name" placeholder="Unique name" name="name" required></b-input>
</b-field>
<div class="px-5 py-5 mb-3 has-background-light">
<h5 class="title is-size-5">Substitution Variables</h5>
<ul>
<li>
<strong>%image_file_name%</strong> - the event image file
</li>
<li>
<strong>%profile_name%</strong> - the triggered profile name
</li>
<li>
<strong>%object_classes%</strong> - object(s) which triggered the profile
</li>
</ul>
</div>
<b-field label="Headers (json)">
<b-input v-model="headersJson" name="headersJson" type="textarea"></b-input>
</b-field>
<b-field label="URL">
<b-input v-model="url" placeholder="http://some.site/api" name="url" required></b-input>
</b-field>
<b-field label="POST">
<b-switch v-model="isPost" @input="changePost"/>
</b-field>
<b-field label="Body (json)">
<b-input v-model="bodyJson" name="bodyJson" type="textarea" :disabled="!isPost"></b-input>
</b-field>
<b-button type="is-primary"
:loading="isSaving"
icon-left="save"
native-type="submit">
Save Profile
</b-button>
</form>
</div>
</div>
</template>
<script>
export default {
name: "WebRequestConfigs",
data() {
return {
webRequestConfigs: [],
name: '',
url: '',
isPost: false,
bodyJson: '',
headersJson: '',
isSaving: false
}
},
mounted() {
this.getData();
},
filters: {
truncate(value) {
let val = value ?? '';
let ret = val.substring(0, 19);
if (val.length > 19) {
ret += '...';
}
return ret;
}
},
methods: {
getData() {
axios.get('/api/automations/webRequest').then(response => {
let configs = response.data.data;
configs.forEach(c => c.isDeleting = false);
this.webRequestConfigs = configs;
});
},
deleteClick(config) {
if (config.detection_profiles.length > 0) {
this.$buefy.dialog.confirm({
title: 'Confirm Delete',
message:
`Deleting this automation will automatically unsubscribe ${config.detection_profiles.length} Detection Profile(s).`,
confirmText: 'Delete',
type: 'is-danger',
hasIcon: true,
onConfirm: () => this.delete(config)
});
}
else {
this.delete(config);
}
},
delete(config) {
config.isDeleting = true;
axios.delete(`/api/automations/webRequest/${config.id}`)
.then(() => {
this.webRequestConfigs = this.webRequestConfigs.filter(c => {
return c.id !== config.id;
});
})
.catch(() => {
config.deleting = false;
this.$buefy.toast.open({
message: 'Something went wrong. Refresh and try again.',
type: 'is-danger'
});
});
},
changePost() {
this.bodyJson = '';
},
processForm() {
let formData = {
'name': this.name,
'url': this.url,
'is_post': this.isPost,
'body_json': this.bodyJson,
'headers_json': this.headersJson
}
this.isSaving = true;
axios.post('/api/automations/webRequest', formData)
.then(response => {
this.webRequestConfigs.push(response.data.data);
this.name = '';
this.url = '';
this.isPost = false;
this.bodyJson = '';
this.headersJson = '';
})
.catch(error => {
let errors = error.response.data.errors;
if (errors && errors.name) {
this.$buefy.toast.open({
message: errors.name[0],
type: 'is-danger'
});
}
else {
this.$buefy.toast.open({
message: 'Something went wrong. Refresh and try again.',
type: 'is-danger'
});
}
})
.finally(() => {
this.isSaving = false;
});
},
checkForm(e) {
if (this.name &&
this.url) {
this.submitForm();
}
else {
this.errors = [];
if (!this.name) {
this.errors.push('Name required.');
}
if (!this.url) {
this.errors.push('URL required.');
}
alert(this.errors.join(' '));
}
e.preventDefault();
}
}
}
</script>
<style scoped>
</style>