Search in sources :

Example 76 with ApiGatewayRequestEvent

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)));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) WEBHOOK_TIME_TO_LIVE(com.formkiq.stacks.dynamodb.ConfigService.WEBHOOK_TIME_TO_LIVE) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) Collection(java.util.Collection) ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) DEFAULT_SITE_ID(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.DEFAULT_SITE_ID) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) HashMap(java.util.HashMap) AwsServiceCache(com.formkiq.lambda.apigateway.AwsServiceCache) ApiAuthorizer(com.formkiq.lambda.apigateway.ApiAuthorizer) Collectors(java.util.stream.Collectors) BadException(com.formkiq.lambda.apigateway.exception.BadException) TooManyRequestsException(com.formkiq.lambda.apigateway.exception.TooManyRequestsException) MAX_WEBHOOKS(com.formkiq.stacks.dynamodb.ConfigService.MAX_WEBHOOKS) List(java.util.List) ApiGatewayRequestEventUtil(com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) Map(java.util.Map) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ZoneOffset(java.time.ZoneOffset) SC_OK(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) HashMap(java.util.HashMap) Map(java.util.Map)

Example 77 with ApiGatewayRequestEvent

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;
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) WEBHOOK_TIME_TO_LIVE(com.formkiq.stacks.dynamodb.ConfigService.WEBHOOK_TIME_TO_LIVE) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) Collection(java.util.Collection) ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) DEFAULT_SITE_ID(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.DEFAULT_SITE_ID) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) HashMap(java.util.HashMap) AwsServiceCache(com.formkiq.lambda.apigateway.AwsServiceCache) ApiAuthorizer(com.formkiq.lambda.apigateway.ApiAuthorizer) Collectors(java.util.stream.Collectors) BadException(com.formkiq.lambda.apigateway.exception.BadException) TooManyRequestsException(com.formkiq.lambda.apigateway.exception.TooManyRequestsException) MAX_WEBHOOKS(com.formkiq.stacks.dynamodb.ConfigService.MAX_WEBHOOKS) List(java.util.List) ApiGatewayRequestEventUtil(com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) Map(java.util.Map) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ZoneOffset(java.time.ZoneOffset) SC_OK(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Date(java.util.Date)

Example 78 with ApiGatewayRequestEvent

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));
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Test(org.junit.jupiter.api.Test)

Example 79 with ApiGatewayRequestEvent

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());
    }
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Date(java.util.Date) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Example 80 with ApiGatewayRequestEvent

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));
    }
}
Also used : DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)131 Test (org.junit.jupiter.api.Test)121 Date (java.util.Date)49 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)46 Map (java.util.Map)45 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)34 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)28 HashMap (java.util.HashMap)13 List (java.util.List)11 S3Client (software.amazon.awssdk.services.s3.S3Client)10 ZonedDateTime (java.time.ZonedDateTime)9 ApiMessageResponse (com.formkiq.lambda.apigateway.ApiMessageResponse)8 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)7 ApiAuthorizer (com.formkiq.lambda.apigateway.ApiAuthorizer)7 ApiGatewayRequestEventUtil (com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil)7 ApiGatewayRequestHandler (com.formkiq.lambda.apigateway.ApiGatewayRequestHandler)7 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)7 SC_OK (com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK)7 AwsServiceCache (com.formkiq.lambda.apigateway.AwsServiceCache)7 LocalDate (java.time.LocalDate)6