0

I am trying to implement a Mule 4.x policy using Mule SDK. In doing so, I need to call an external API in the policy operations implementation. The result returned by the external API response would determine the policy output.

public class MyMulePolicyOperations
{
    @MediaType( value = ANY, strict = false )
    public void handle(
            @Config MyMulePolicyConfiguration configuration,
            @Alias( "httpRequestMethod" ) String httpRequestMethod,
            CompletionCallback<String, HttpResponse> callback ) throws Exception
    {
        HttpResponseBuilder httpResponseBuilder = HttpResponse.builder();

        String result = // call an external API and extract "result" from the response

        if ( result.equals( configuration.getMyConfigValue() ) )
        {
            httpResponseBuilder.addHeader( "allow_request", "true" );
        }
        else
        {
            httpResponseBuilder.addHeader( "allow_request", "false" );
        }

        Result<String, HttpResponse> result = Result.<String, HttpResponse> builder()
                .attributes( httpResponseBuilder.build() )
                .build();

        callback.success( result );
    }

}

Can someone tell me how I can implement the REST client using Mule SDK?

techjourneyman
  • 1,701
  • 3
  • 33
  • 53

1 Answers1

0

If you want to implement an HTTP request inside a custom module, created with the Mule SDK, then you have to use the HTTP Client as described in the documentation: https://docs.mulesoft.com/mule-sdk/1.1/HTTP-based-connectors#use-the-mule-http-client

You didn't provide any reason or needs to implement the request inside a custom module. It would be way easier just to perform the HTTP request using the HTTP Requester inside the custom policy.

aled
  • 21,330
  • 3
  • 27
  • 34
  • Thanks aled. As I mentioned above, I am writing a policy in Mule and using the SDK. In the policy I need to make a call to an external API. I am trying to find how simply this can be done using the SDK. – techjourneyman Mar 15 '20 at 01:57
  • Hi, yes you mentioned it. it is not clear why do you want to use the ask in this way. Why not just use a custom policy? – aled Mar 15 '20 at 03:51
  • Yes, I am using a custom policy. And in the custom policy itself I need to call an external API in order to validate something. Are you suggesting that I can do this without using Mule SDK? If yes, that would work perfectly. Can you please elaborate? – techjourneyman Mar 15 '20 at 20:00
  • Yes, you just need to add an HTTP request and config inside the policy, similar to how it would be done in a flow. – aled Mar 17 '20 at 02:22