use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent 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.lambda.apigateway.ApiGatewayRequestEvent 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.lambda.apigateway.ApiGatewayRequestEvent in project formkiq-core by formkiq.
the class ApiDocumentsTagsRequestTest method testHandlePostDocumentTags01.
/**
* POST /documents/{documentId}/tags request missing 'tagvalue' field.
*
* @throws Exception an error has occurred
*/
@Test
public void testHandlePostDocumentTags01() throws Exception {
// given
ApiGatewayRequestEvent event = toRequestEvent("/request-post-documents-documentid-tags-invalid1.json");
String expected = "{" + getHeaders() + ",\"body\":\"{\\\"message\\\":\\\"invalid json body\\\"}\"" + ",\"statusCode\":400}";
// when
String response = handleRequest(event);
// then
assertEquals(expected, response);
assertTrue(getLogger().containsString("response: " + expected));
}
use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent in project formkiq-core by formkiq.
the class ApiDocumentsTagsRequestTest method testHandlePutTags06.
/**
* PUT /documents/{documentId}/tags/{tagKey} change VALUES to VALUE request.
*
* @throws Exception an error has occurred
*/
@Test
public void testHandlePutTags06() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
String documentId = UUID.randomUUID().toString();
String userId = "jsmith";
final String expected = "{" + getHeaders() + "," + "\"body\":\"" + "{\\\"message\\\":\\\"Updated tag 'category' on document '" + documentId + "'.\\\"}\"" + ",\"statusCode\":200}";
DocumentTag tag = new DocumentTag(null, "category", null, new Date(), userId);
tag.setValues(Arrays.asList("abc", "xyz"));
getDocumentService().saveDocument(siteId, new DocumentItemDynamoDb(documentId, new Date(), userId), Arrays.asList(tag));
ApiGatewayRequestEvent event = toRequestEvent("/request-put-documents-documentid-tags01.json");
addParameter(event, "siteId", siteId);
setPathParameter(event, "documentId", documentId);
// when
String response = handleRequest(event);
// then
assertEquals(expected, response);
PaginationResults<DocumentTag> tags = getDocumentService().findDocumentTags(siteId, documentId, null, MAX_RESULTS);
assertEquals(1, tags.getResults().size());
assertEquals("category", tags.getResults().get(0).getKey());
assertEquals("active", tags.getResults().get(0).getValue());
assertNull(tags.getResults().get(0).getValues());
assertEquals("8a73dfef-26d3-43d8-87aa-b3ec358e43ba@formkiq.com", tags.getResults().get(0).getUserId());
}
}
use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent in project formkiq-core by formkiq.
the class ApiDocumentsTagsRequestTest method testHandlePostDocumentTags06.
/**
* POST /documents/{documentId}/tags testing "untagged" gets removed.
*
* @throws Exception an error has occurred
*/
@Test
public void testHandlePostDocumentTags06() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
final String documentId = "test" + UUID.randomUUID().toString() + ".pdf";
String username = UUID.randomUUID() + "@formkiq.com";
DynamicDocumentItem doc = new DynamicDocumentItem(Map.of("documentId", documentId, "userId", username, "insertedDate", new Date()));
getDocumentService().saveDocumentItemWithTag(siteId, doc);
PaginationResults<DocumentTag> tags = getDocumentService().findDocumentTags(siteId, documentId, null, MAX_RESULTS);
assertEquals(2, tags.getResults().size());
assertEquals("untagged", tags.getResults().get(0).getKey());
assertEquals("userId", tags.getResults().get(1).getKey());
final String tagname = "category";
final String tagvalue = "";
ApiGatewayRequestEvent event = toRequestEvent("/request-post-documents-documentid-tags03.json");
addParameter(event, "siteId", siteId);
setPathParameter(event, "documentId", documentId);
String expected = "{" + getHeaders() + ",\"body\":\"" + "{\\\"message\\\":\\\"Created Tag 'category'.\\\"}\",\"statusCode\":201}";
// when
String response = handleRequest(event);
// then
assertEquals(expected, response);
tags = getDocumentService().findDocumentTags(siteId, documentId, null, MAX_RESULTS);
assertEquals(2, tags.getResults().size());
assertEquals(tagname, tags.getResults().get(0).getKey());
assertEquals(tagvalue, tags.getResults().get(0).getValue());
assertEquals("userId", tags.getResults().get(1).getKey());
assertEquals(username, tags.getResults().get(1).getValue());
assertTrue(getLogger().containsString("response: " + expected));
}
}
Aggregations