-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcopy.js.erb
More file actions
51 lines (45 loc) · 2.29 KB
/
Copy pathcopy.js.erb
File metadata and controls
51 lines (45 loc) · 2.29 KB
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
// Build the cURL command with line breaks and formatted JSON
var jsonPayload = JSON.parse('<%= raw j(@json_payload) %>');
var prettifiedJson = JSON.stringify(jsonPayload, null, 2);
var command = "curl -i -X POST '<%= raw j(@broker_url) %>' \\\n" +
"-H 'Content-Type: <%= raw j(@subscription_request.content_type) %>' \\\n" +
<% @subscription_request.tenant_headers.each do |name, value| %>
"-H '<%= raw j(name) %>: <%= raw j(value) %>' \\\n" +
<% end %>
"--data-binary '" + prettifiedJson + "'";
// Same connection-defined token header scheme as publish.js.erb (#99).
// The token box only exists for browser/proxied connections; for a stored
// connection the token stays on the server, so the copied command carries a
// <token> placeholder instead of the secret.
var tokenBox = document.getElementById('subscription_auth_token');
var authToken = tokenBox ? tokenBox.value : '';
if (authToken) {
command += " \\\n-H '<%= raw j(@subscription_template.broker_connection.token_header_name) %>: " +
"<%= raw j(@subscription_template.broker_connection.token_value_prefix) %>" + authToken + "'";
}
<% if @subscription_template.broker_connection.stored_auth? && @subscription_template.broker_connection.auth_token.present? %>
else {
command += " \\\n-H '<%= raw j(@subscription_template.broker_connection.token_header_name) %>: " +
"<%= raw j(@subscription_template.broker_connection.token_value_prefix) %><token>'";
}
<% end %>
console.log(command);
// Function to copy command to clipboard
function copyToClipboard(command) {
if (navigator.clipboard) {
// If Clipboard API is available
navigator.clipboard.writeText(command).then(function() {
showNotification("<%= raw j l(:command_copied) %>"); // Show success message
}).catch(function(err) {
console.error('Could not copy cURL command to clipboard: ', err);
showNotification("<%= raw j l(:command_copy_failed) %>"); // Show error message
});
} else {
// If Clipboard API is not available
showNotification("<%= raw j l(:command_clipboard_unavailable) %>");
}
}
// showNotification comes from the shared gtt_fiware.js asset, loaded on
// every page by the layout hook.
// Copy the cURL command to the clipboard
copyToClipboard(command);