Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webhook: Use Maps instead of Lists in configuration #168

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions plugins/webhook/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ This plugin sends HTTP requests to third-party endpoints to trigger further proc
## Configuration

```yaml
- webhook:
endpoints:
- url: "http://example.localhost/trigger" # URL to send the request to. Required.
method: "POST" # HTTP method to use. Default is POST.
includeDataSet: false # Send the Sauron Data Set as the body of the request. Default is false.
webhook:
endpoints:
example:
url: "http://example.localhost/trigger" # URL to send the request to. Required.
method: "POST" # HTTP method to use. Default is POST.
includeDataSet: false # Send the Sauron Data Set as the body of the request. Default is false.
```

## Input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,39 +58,39 @@ public Webhook(RestTemplate restTemplate)
*/
@Override
public DataSet apply(PluginsConfigurationProperties properties, DataSet input) {
properties.getPluginConfigurationProperty(PLUGIN_ID, PROPERTY_ENDPOINTS).ifPresent(endpoints -> {
for (Map<String, Object> endpoint : (List<Map<String, Object>>) endpoints)
{
String url = (String) endpoint.getOrDefault(PROPERTY_URL, "");
if (Objects.equals(url, ""))
{
logger.error("URL of webhook endpoint not set");
continue;
}
properties.getPluginConfigurationProperty(PLUGIN_ID, PROPERTY_ENDPOINTS).ifPresent(endpointProperty -> {
Map<String, Object> endpoints = (Map<String, Object>) endpointProperty;
endpoints.forEach((name, data) -> {
Map<String, Object> config = (Map<String, Object>) data;
String url = (String) config.getOrDefault(PROPERTY_URL, "");
String methodRaw = (String) config.getOrDefault(PROPERTY_METHOD, "POST");
Boolean includeDataSet = (Boolean) config.getOrDefault(PROPERTY_INCLUDE_DATASET, false);
send(name, url, methodRaw, includeDataSet, input);
});
});

String methodRaw = (String) endpoint.getOrDefault(PROPERTY_METHOD, "POST");
HttpMethod method = HttpMethod.resolve(methodRaw);
if (method == null)
{
logger.error("HTTP method {} of webhook endpoint is invalid", methodRaw);
continue;
}
return input;
}

HttpEntity<DataSet> payload = null;
Boolean includeDataSet = (Boolean) endpoint.getOrDefault(PROPERTY_INCLUDE_DATASET, false);
if (includeDataSet)
{
payload = new HttpEntity<>(input);
}
private void send(String name, String url, String methodRaw, Boolean includeDataSet, DataSet dataSet)
{
HttpMethod method = HttpMethod.resolve(methodRaw);
if (method == null)
{
logger.error("HTTP method {} of webhook endpoint {} is invalid", name, methodRaw);
return;
}

ResponseEntity<Object> response = restTemplate.exchange(url, method, payload, Object.class);
if (!response.getStatusCode().is2xxSuccessful())
{
logger.error("Sending webhook to {} failed with HTTP status code {}", url, response.getStatusCode());
}
}
});
HttpEntity<DataSet> payload = null;
if (includeDataSet)
{
payload = new HttpEntity<>(dataSet);
}

return input;
ResponseEntity<Object> response = restTemplate.exchange(url, method, payload, Object.class);
if (!response.getStatusCode().is2xxSuccessful())
{
logger.error("Sending webhook {} to {} failed with HTTP status code {}", name, url, response.getStatusCode());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ private PluginsConfigurationProperties createPluginConfigurationProperties(Strin
{{
put("webhook", new HashMap<>()
{{
put("endpoints", List.of(endpoint));
put("endpoints", new HashMap<>()
{{
put("unittest", endpoint);
}});
}});
}};
}
Expand Down
Loading