Search in sources :

Example 71 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class WebhooksRequestHandler method isOverMaxWebhooks.

private boolean isOverMaxWebhooks(final LambdaLogger logger, final AwsServiceCache awsservice, final String siteId) {
    boolean over = false;
    DynamicObject config = awsservice.config(siteId);
    String maxString = config.getString(MAX_WEBHOOKS);
    if (maxString != null) {
        try {
            int max = Integer.parseInt(maxString);
            int numberOfWebhooks = awsservice.webhookService().findWebhooks(siteId).size();
            if (awsservice.debug()) {
                logger.log("found config for maximum webhooks " + maxString);
                logger.log("found " + numberOfWebhooks + " webhooks");
            }
            if (numberOfWebhooks >= max) {
                over = true;
            }
        } catch (NumberFormatException e) {
            over = false;
            e.printStackTrace();
        }
    }
    return over;
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject)

Example 72 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class WebhooksTagsRequestHandler 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 id = getPathParameter(event, "webhookId");
    PaginationResults<DynamicObject> list = awsServices.webhookService().findTags(siteId, id, null);
    List<Map<String, Object>> tags = list.getResults().stream().map(m -> {
        Map<String, Object> map = new HashMap<>();
        map.put("insertedDate", m.getString("inserteddate"));
        map.put("webhookId", id);
        map.put("type", m.getString("type"));
        map.put("userId", m.getString("userId"));
        map.put("value", m.getString("tagValue"));
        map.put("key", m.getString("tagKey"));
        return map;
    }).collect(Collectors.toList());
    return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(Map.of("tags", tags)));
}
Also used : SC_CREATED(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_CREATED) Arrays(java.util.Arrays) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Date(java.util.Date) ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) HashMap(java.util.HashMap) AwsServiceCache(com.formkiq.lambda.apigateway.AwsServiceCache) ApiAuthorizer(com.formkiq.lambda.apigateway.ApiAuthorizer) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) Collectors(java.util.stream.Collectors) BadException(com.formkiq.lambda.apigateway.exception.BadException) ApiResponse(com.formkiq.lambda.apigateway.ApiResponse) 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) ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) SC_OK(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK) DocumentTagType(com.formkiq.stacks.dynamodb.DocumentTagType) PaginationResults(com.formkiq.stacks.dynamodb.PaginationResults) 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 73 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class WebhooksTagsRequestHandler method post.

@Override
public ApiRequestHandlerResponse post(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsServices) throws Exception {
    DocumentTag tag = fromBodyToObject(logger, event, DocumentTag.class);
    if (tag.getKey() == null || tag.getKey().length() == 0) {
        throw new BadException("invalid json body");
    }
    String siteId = authorizer.getSiteId();
    String id = getPathParameter(event, "webhookId");
    tag.setType(DocumentTagType.USERDEFINED);
    tag.setInsertedDate(new Date());
    tag.setUserId(getCallingCognitoUsername(event));
    DynamicObject webhook = awsServices.webhookService().findWebhook(siteId, id);
    if (webhook == null) {
        throw new NotFoundException("Webhook 'id' not found");
    }
    Date ttl = null;
    String ttlString = webhook.getString("TimeToLive");
    if (ttlString != null) {
        long epoch = Long.parseLong(ttlString);
        ttl = new Date(epoch * TO_MILLIS);
    }
    awsServices.webhookService().addTags(siteId, id, Arrays.asList(tag), ttl);
    ApiResponse resp = new ApiMessageResponse("Created Tag '" + tag.getKey() + "'.");
    return new ApiRequestHandlerResponse(SC_CREATED, resp);
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) BadException(com.formkiq.lambda.apigateway.exception.BadException) Date(java.util.Date) ApiResponse(com.formkiq.lambda.apigateway.ApiResponse)

Example 74 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class AbstractRequestHandler method handleRequest.

/**
 * Handle Request.
 *
 * @param file {@link String}
 * @param siteId {@link String}
 * @param username {@link String}
 * @param cognitoGroups {@link String}
 * @return {@link DynamicObject}
 * @throws IOException IOException
 */
@SuppressWarnings("unchecked")
public DynamicObject handleRequest(final String file, final String siteId, final String username, final String cognitoGroups) throws IOException {
    ApiGatewayRequestEvent event = toRequestEvent(file);
    addParameter(event, "siteId", siteId);
    if (username != null) {
        setUsername(event, username);
    }
    if (cognitoGroups != null) {
        setCognitoGroup(event, cognitoGroups);
    }
    String response = handleRequest(event);
    return new DynamicObject(fromJson(response, Map.class));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) HashMap(java.util.HashMap) Map(java.util.Map)

Example 75 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class ApiDocumentsRequestTest method testHandleGetDocuments10.

/**
 * Test user with 'Finance' role with no siteid.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocuments10() throws Exception {
    // given
    String siteId = null;
    ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents03.json");
    addParameter(event, "siteId", siteId);
    setCognitoGroup(event, "Finance");
    // when
    String response = handleRequest(event);
    // then
    Map<String, String> m = fromJson(response, Map.class);
    final int mapsize = 3;
    assertEquals(mapsize, m.size());
    assertEquals("200.0", String.valueOf(m.get("statusCode")));
    assertEquals(getHeaders(), "\"headers\":" + GsonUtil.getInstance().toJson(m.get("headers")));
    DynamicObject resp = new DynamicObject(fromJson(m.get("body"), Map.class));
    List<DynamicObject> documents = resp.getList("documents");
    assertEquals(0, documents.size());
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Map(java.util.Map) 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