Multiple proxy and target endpoints

I want to have the following API structure, with ONE ProxyEndpoint and 2 TargetEndpoints which I already have:

https://myapigeeurl/services/v1/product/quote map to /api/quote-service

and

https://myapigeeurl/services/v1/product/ref-data map to /api/reference-data-service

I understand that I can do that easily with TWO ProxyEndpoints and TWO Target Endpoints, but I would have to repeat the Pre and Post flows etc in each ProxyEndpoint and that’s not nice as the whole process is the same.

The issue right now is since the base path in the ProxyEndpoint is set to /services/v1/product

And the route rules are configured like that:

<RouteRule name="Quote Service Rule">
  <Condition>(proxy.pathsuffix MatchesPath "/quote/**")</Condition>
  <TargetEndpoint>targetQuote</TargetEndpoint>
</RouteRule>
<RouteRule name="Reference Data Service Rule">
  <Condition>(proxy.pathsuffix MatchesPath "/ref-data/**")</Condition>
  <TargetEndpoint>targetRefData</TargetEndpoint>
</RouteRule>

With the TargetEndpoints setup like:

<TargetEndpoint name="targetQuote">
        <HTTPTargetConnection>
        <LoadBalancer>
            <Server name="service-gateway"/>
        </LoadBalancer>
        <Path>/api/quote-service</Path>
    </HTTPTargetConnection>
</TargetEndpoint>
<TargetEndpoint name="targetRefData">
        <HTTPTargetConnection>
        <LoadBalancer>
            <Server name="service-gateway"/>
        </LoadBalancer>
        <Path>/api/reference-data-service</Path>
    </HTTPTargetConnection>
</TargetEndpoint>

When I invoke:

https://myapigeeurl/services/v1/product/ref-data/stuff the target url invoked is /api/reference-data-service/ref-data/stuff because the proxy.pathsuffix is obviously “ref-data/stuff”

How to strip that off? The target URL invoked should be /api-reference-data/stuff

Many thanks in advance for your help.

1 Like

@Roman Rodov , Welcome to Apigee Community,

You can use shared flows if you don’t want to repeat all the policies in different proxy endpoints. Optionally, You can override target url in target endpoint preflow using javascript policy like below,

context.setVariable("target.copy.pathsuffix", false);
var proxySuffix = context.getVariable("proxy.pathsuffix");
var proxySuffixFragments = proxySuffix.split("/");
var targetpathsuffix = proxySuffixFragments.splice(2).join("/");
context.setVariable("targetpathsuffix", targetpathsuffix);

<TargetEndpoint name="targetRefData">
        <HTTPTargetConnection>
        <LoadBalancer>
            <Server name="service-gateway"/>
        </LoadBalancer>
        <Path>/api/reference-data-service/{targetpathsuffix}</Path>
    </HTTPTargetConnection>
</TargetEndpoint>

Hope it helps. Keep us posted moving forward if any.

1 Like

Thank you very much for looking, @Anil Sagar. As you can see, I am using a named target server, I am certain setting the target.url variable has no effect.

@Roman Rodov

Yes, You are right, You cannot override using target.url , but you can append suffix to path in the targetendpoint HTTPTargetConnection element something like below,

context.setVariable("target.copy.pathsuffix", false);
var targetserver = context.getVariable("target.url");
var proxySuffix = context.getVariable("proxy.pathsuffix");
var proxySuffixFragments = proxySuffix.split("/");
// remove the first part
proxySuffixFragments.splice(0, 2);
var targetpathsuffix = proxySuffixFragments.join("/");
var targeturl = targetserver + targetpathsuffix
context.setVariable("targetpathsuffix", targetpathsuffix);

<TargetEndpoint name="targetRefData">
        <HTTPTargetConnection>
        <LoadBalancer>
            <Server name="service-gateway"/>
        </LoadBalancer>
        <Path>/api/reference-data-service/{targetpathsuffix}</Path>
    </HTTPTargetConnection>
</TargetEndpoint>

Also, You can consider shared flows to implement your requirement. Hope it helps.

1 Like

Bingo, please update your original answer as I will mark it as Accepted. That fixed it.

My final code for the JavaScript policy:

context.setVariable("target.copy.pathsuffix", false);
var proxySuffix = context.getVariable("proxy.pathsuffix");
var proxySuffixFragments = proxySuffix.split("/");
var targetpathsuffix = proxySuffixFragments.splice(2).join("/");
context.setVariable("targetpathsuffix", targetpathsuffix);

@Roman Rodov , Awesome, Yes, I have updated the answer. Glad to know your issue is resolved. Keep us posted moving forward in community if any.