Search in sources :

Example 41 with DynamicObject

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"));
}
Also used : ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) HashMap(java.util.HashMap) ZonedDateTime(java.time.ZonedDateTime) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) WebhooksService(com.formkiq.stacks.dynamodb.WebhooksService) Date(java.util.Date)

Example 42 with DynamicObject

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)));
}
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 43 with DynamicObject

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;
}
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 44 with DynamicObject

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;
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ZonedDateTime(java.time.ZonedDateTime) Date(java.util.Date)

Example 45 with DynamicObject

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());
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)93 Test (org.junit.jupiter.api.Test)55 Map (java.util.Map)46 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)39 Date (java.util.Date)26 HashMap (java.util.HashMap)17 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)12 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)10 ZonedDateTime (java.time.ZonedDateTime)10 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)8 List (java.util.List)8 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)7 S3Client (software.amazon.awssdk.services.s3.S3Client)7 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)6 BadException (com.formkiq.lambda.apigateway.exception.BadException)6 Collectors (java.util.stream.Collectors)6 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)5 FormKiqClientV1 (com.formkiq.stacks.client.FormKiqClientV1)5 LocalDate (java.time.LocalDate)5 Test (org.junit.Test)5