use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.
the class WebhooksIdRequestHandler method patch.
@Override
public ApiRequestHandlerResponse patch(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsServices) throws Exception {
String siteId = authorizer.getSiteId();
String id = getPathParameter(event, "webhookId");
WebhooksService webhookService = awsServices.webhookService();
if (webhookService.findWebhook(siteId, id) == null) {
throw new NotFoundException("Webhook 'id' not found");
}
DynamicObject obj = fromBodyToDynamicObject(logger, event);
Map<String, Object> map = new HashMap<>();
if (obj.containsKey("name")) {
map.put("name", obj.getString("name"));
}
if (obj.containsKey("enabled")) {
map.put("enabled", obj.getBoolean("enabled"));
}
Date ttlDate = null;
if (obj.containsKey("ttl")) {
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC).plusSeconds(Long.parseLong(obj.getString("ttl")));
ttlDate = Date.from(now.toInstant());
map.put("TimeToLive", ttlDate);
}
webhookService.updateWebhook(siteId, id, new DynamicObject(map));
if (ttlDate != null) {
webhookService.updateTimeToLive(siteId, id, ttlDate);
}
return new ApiRequestHandlerResponse(SC_OK, new ApiMessageResponse("'" + id + "' object updated"));
}
use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.
the class WebhooksRequestHandler method get.
@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsServices) throws Exception {
String siteId = authorizer.getSiteId();
String url = awsServices.ssmService().getParameterValue("/formkiq/" + awsServices.appEnvironment() + "/api/DocumentsPublicHttpUrl");
List<DynamicObject> list = awsServices.webhookService().findWebhooks(siteId);
List<Map<String, Object>> webhooks = list.stream().map(m -> {
String path = "private".equals(m.getString("enabled")) ? "/private" : "/public";
Map<String, Object> map = new HashMap<>();
String u = url + path + "/webhooks/" + m.getString("documentId");
if (siteId != null && !DEFAULT_SITE_ID.equals(siteId)) {
u += "?siteId=" + siteId;
}
map.put("siteId", siteId != null ? siteId : DEFAULT_SITE_ID);
map.put("id", m.getString("documentId"));
map.put("name", m.getString("path"));
map.put("url", u);
map.put("insertedDate", m.getString("inserteddate"));
map.put("userId", m.getString("userId"));
map.put("enabled", m.getString("enabled"));
map.put("ttl", m.getString("ttl"));
return map;
}).collect(Collectors.toList());
return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(Map.of("webhooks", webhooks)));
}
use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.
the class WebhooksRequestHandler method saveWebhook.
private String saveWebhook(final ApiGatewayRequestEvent event, final AwsServiceCache awsservice, final String siteId, final DynamicObject o) {
Date ttlDate = getTtlDate(awsservice, siteId, o);
String name = o.getString("name");
String userId = getCallingCognitoUsername(event);
String enabled = o.containsKey("enabled") ? o.getString("enabled") : "true";
String id = awsservice.webhookService().saveWebhook(siteId, name, userId, ttlDate, enabled);
if (o.containsKey("tags")) {
List<DynamicObject> dtags = o.getList("tags");
Date date = new Date();
Collection<DocumentTag> tags = dtags.stream().map(d -> new DocumentTag(null, d.getString("key"), d.getString("value"), date, userId)).collect(Collectors.toList());
awsservice.webhookService().addTags(siteId, id, tags, ttlDate);
}
return id;
}
use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.
the class WebhooksRequestHandler method getTtlDate.
private Date getTtlDate(final AwsServiceCache awsservice, final String siteId, final DynamicObject o) {
Date ttlDate = null;
String ttl = o.getString("ttl");
if (ttl == null) {
DynamicObject config = awsservice.config(siteId);
ttl = config.getString(WEBHOOK_TIME_TO_LIVE);
}
if (ttl != null) {
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC).plusSeconds(Long.parseLong(ttl));
ttlDate = Date.from(now.toInstant());
}
return ttlDate;
}
use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.
the class WebhooksServiceImplTest method testDeleteWebhooksAndTags01.
/**
* Test Delete Webhook and Tags.
*/
@Test
public void testDeleteWebhooksAndTags01() {
// given
final int numberOfTags = 100;
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
String id0 = this.service.saveWebhook(siteId, "test", "joe", null, "true");
for (int i = 0; i < numberOfTags; i++) {
DocumentTag tag = new DocumentTag(id0, UUID.randomUUID().toString(), null, new Date(), "joe");
this.service.addTags(siteId, id0, Arrays.asList(tag), null);
}
// when
this.service.deleteWebhook(siteId, id0);
// then
PaginationResults<DynamicObject> tags = this.service.findTags(siteId, id0, null);
assertEquals(0, tags.getResults().size());
}
}
Aggregations