Search in sources :

Example 1 with Webhook

use of com.erudika.para.core.Webhook in project scoold by Erudika.

the class ApiController method createWebhook.

@PostMapping("/webhooks")
public Webhook createWebhook(HttpServletRequest req, HttpServletResponse res) {
    if (!utils.isWebhooksEnabled()) {
        res.setStatus(HttpStatus.FORBIDDEN.value());
        return null;
    }
    Map<String, Object> entity = readEntity(req);
    if (entity.isEmpty()) {
        badReq("Missing request body.");
        return null;
    }
    String targetUrl = (String) entity.get("targetUrl");
    if (!Utils.isValidURL(targetUrl)) {
        badReq("Property 'targetUrl' must be a valid URL.");
        return null;
    }
    Webhook webhook = pc.create(ParaObjectUtils.setAnnotatedFields(new Webhook(), entity, null));
    if (webhook == null) {
        badReq("Failed to create webhook.");
        return null;
    }
    res.setStatus(HttpStatus.CREATED.value());
    return webhook;
}
Also used : ParaObject(com.erudika.para.core.ParaObject) Webhook(com.erudika.para.core.Webhook) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 2 with Webhook

use of com.erudika.para.core.Webhook in project scoold by Erudika.

the class ApiController method updateWebhook.

@PatchMapping("/webhooks/{id}")
public Webhook updateWebhook(@PathVariable String id, HttpServletRequest req, HttpServletResponse res) {
    if (!utils.isWebhooksEnabled()) {
        res.setStatus(HttpStatus.FORBIDDEN.value());
        return null;
    }
    Webhook webhook = pc.read(id);
    if (webhook == null) {
        res.setStatus(HttpStatus.NOT_FOUND.value());
        return null;
    }
    Map<String, Object> entity = readEntity(req);
    return pc.update(ParaObjectUtils.setAnnotatedFields(webhook, entity, Locked.class));
}
Also used : Locked(com.erudika.para.core.annotations.Locked) Webhook(com.erudika.para.core.Webhook) ParaObject(com.erudika.para.core.ParaObject) PatchMapping(org.springframework.web.bind.annotation.PatchMapping)

Example 3 with Webhook

use of com.erudika.para.core.Webhook in project scoold by Erudika.

the class ScooldUtils method triggerHookEvent.

public void triggerHookEvent(String eventName, Object payload) {
    if (isWebhooksEnabled() && HOOK_EVENTS.contains(eventName)) {
        Para.asyncExecute(() -> {
            Webhook trigger = new Webhook();
            trigger.setTriggeredEvent(eventName);
            trigger.setCustomPayload(payload);
            pc.create(trigger);
        });
    }
}
Also used : Webhook(com.erudika.para.core.Webhook)

Example 4 with Webhook

use of com.erudika.para.core.Webhook in project scoold by Erudika.

the class AdminController method toggleWebhook.

@PostMapping("/toggle-webhook")
public String toggleWebhook(@RequestParam String id, HttpServletRequest req, HttpServletResponse res) {
    Profile authUser = utils.getAuthUser(req);
    if (!StringUtils.isBlank(id) && utils.isAdmin(authUser) && utils.isWebhooksEnabled()) {
        Webhook webhook = pc.read(id);
        if (webhook != null) {
            webhook.setActive(!webhook.getActive());
            pc.update(webhook);
        }
    }
    if (utils.isAjaxRequest(req)) {
        res.setStatus(200);
        return "base";
    } else {
        return "redirect:" + ADMINLINK;
    }
}
Also used : Webhook(com.erudika.para.core.Webhook) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 5 with Webhook

use of com.erudika.para.core.Webhook in project scoold by Erudika.

the class AdminController method createWebhook.

@PostMapping("/create-webhook")
public String createWebhook(@RequestParam String targetUrl, @RequestParam(required = false) String type, @RequestParam Boolean json, @RequestParam Set<String> events, HttpServletRequest req, Model model) {
    Profile authUser = utils.getAuthUser(req);
    if (Utils.isValidURL(targetUrl) && utils.isAdmin(authUser) && utils.isWebhooksEnabled()) {
        Webhook webhook = new Webhook(targetUrl);
        webhook.setCreate(events.contains("create"));
        webhook.setUpdate(events.contains("update"));
        webhook.setDelete(events.contains("delete"));
        webhook.setCreateAll(events.contains("createAll"));
        webhook.setUpdateAll(events.contains("updateAll"));
        webhook.setDeleteAll(events.contains("deleteAll"));
        webhook.setCustomEvents(events.stream().filter(e -> !StringUtils.equalsAny(e, "create", "update", "delete", "createAll", "updateAll", "deleteAll")).collect(Collectors.toList()));
        if (utils.getCoreScooldTypes().contains(type)) {
            webhook.setTypeFilter(type);
        }
        webhook.setUrlEncoded(!json);
        webhook.resetSecret();
        pc.create(webhook);
    } else {
        model.addAttribute("error", Collections.singletonMap("targetUrl", utils.getLang(req).get("requiredfield")));
        return "base";
    }
    return "redirect:" + ADMINLINK;
}
Also used : Webhook(com.erudika.para.core.Webhook) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

Webhook (com.erudika.para.core.Webhook)7 PostMapping (org.springframework.web.bind.annotation.PostMapping)4 Profile (com.erudika.scoold.core.Profile)3 ParaObject (com.erudika.para.core.ParaObject)2 Locked (com.erudika.para.core.annotations.Locked)1 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)1 PatchMapping (org.springframework.web.bind.annotation.PatchMapping)1