Hi, I'm trying to implement a custom WebhookTransf...
# help
c
Hi, I'm trying to implement a custom WebhookTransformer to sign a request body with a secretKey. The transformer is porperly invoked before dispatching the webhook. But it is always invoked as soon as the transformer is registered. Is it possible to limit invocation only to stubs where it is explicitly defined? Code of the transformer:
Copy code
public class WebhookSigningExtension implements WebhookTransformer {


    @Override
    public WebhookDefinition transform(ServeEvent serveEvent, WebhookDefinition webhookDefinition) {
        System.out.println("WebhookSigningExtension: transform called");
        return webhookDefinition;
    }

    @Override
    public String getName() {
        return "webhook-signer";
    }
}
Code to test the transformer:
Copy code
public class Main {

    public static void main(String[] args) {
        WireMockServer wireMockServer = new WireMockServer(
                WireMockConfiguration.options().port(8080).extensions(new WebhookSigningExtension())
        );

        wireMockServer.start();

        wireMockServer.stubFor(<http://WireMock.post|WireMock.post>(WireMock.urlPathEqualTo("/webhook"))
                .willReturn(WireMock.ok())
                .withServeEventListener("webhook", Webhooks.webhook()
                        .withMethod(<http://RequestMethod.POST|RequestMethod.POST>)
                        .withUrl("<http://localhost:9898/webhook/test>")
                        .withHeader("Content-Type", "application/json")
                        .withBody("{ \"result\": \"SUCCESS\" }"))
        );

    }
}
Do I have to implement the logic to enabled/disable the transformer by passing custom parameter? Thanks in advance, Claudio
t
You can get the stub mapping that was matched for the specific request via
serveEvent.getStubMapping()
so you could put some conditional logic in there based on the ID/name/whatever from that.
a
I did this by wrapping the code inside my extended transform method with:
Copy code
if (webhookDefinition.getOtherFields().get("transformers").toString().contains("my-extension-name")) {

  blah
}
and adding this to the mapping
Copy code
"transformers": ["my-extension-name"]
c
Great, thanks 🙂