1

i have just started working with Apigee. I want to create one API proxy which will call two target endpoints based on 'if' condition. i have created an API and added resources to it but the problem is in this case i am getting two API's . If thetype='abc' target point should be target1 if thetype='xyz' target point should be target2 Can anyone please tell me how to proceed with it ?

madhurika
  • 191
  • 1
  • 8

2 Answers2

3

Check out the answer to this question. The details of finding the RouteRules is listed there. The ProxyEndpoint documentation will also be helpful.

You can accomplish what you are attempting using this code:

<RouteRule name="routeToTarget1">
    <Condition>thetype == "abc"</Condition>
    <TargetEndpoint>target1</TargetEndpoint>
</RouteRule>
<RouteRule name="routeToTarget2">
    <Condition>thetype == "xyz"</Condition>
    <TargetEndpoint>target2</TargetEndpoint>
</RouteRule>

These RouteRules will be evaluated in order.

Note that you probably want the bottom RouteRule to have no condition, which means it will always match. What happens when thetype does not equal "abc" or "xyz"? Assuming target1 is the default, your code would look like this:

<RouteRule name="routeToTarget2">
    <Condition>thetype == "xyz"</Condition>
    <TargetEndpoint>target2</TargetEndpoint>
</RouteRule>
<RouteRule name="routeToTarget1">
    <TargetEndpoint>target1</TargetEndpoint>
</RouteRule>
Community
  • 1
  • 1
Mike Dunker
  • 1,950
  • 15
  • 17
  • Thanks Mike. I tried with the code you have sent but it's throwing me an error:: Error while Uploading file for API vmife. messaging.config.beans.InvalidBundle. Errors:[Entity : Proxy, Target Not Found: http://url../../;] – madhurika Apr 02 '14 at 05:10
  • 'thetype' here is one column in table . Here , i am creating a Proxy API for two target endpoints. Now these target endpoints are two different API's . I have a filter condition for one of the API's which is working fine in the browser (https://url?$filter=thtype eq 123) Now in my condition i am saying thetype==123 but it is not working. – madhurika Apr 02 '14 at 05:26
  • How are you populating the "thetype" variable? I'm sure you know, but request.queryparam.$filter (which can be accessed in a JavaScript policy by using context.getVariable("request.queryparam.$filter")) will have the value "thtype eq 123" (using your example in the comment above). You'll need to parse that string to get the value 123. – Mike Dunker Apr 02 '14 at 16:13
0

If you are using the API Proxy Editor UI, then you can do the following:

(1) Choose New / New Resource from the API Proxy Editor toolbar.

You will then see this: enter image description here

(2) For the input field, Optional Target URL, enter the target URL that corresponds to that resource.

This tool will then generate both a conditional flow for that resource to which you can optionally attach resource-specific policies.

This tool will also add the needed route rule, and your generated XML will look like this:

<ProxyEndpoint name="default">
    <RouteRule name="Resource-1">
        <Condition>(proxy.pathsuffix MatchesPath &quot;/someResource&quot;) and (request.verb = &quot;GET&quot;)</Condition>
        <HTTPTargetConnection>
            <URL>http://myAlternateEndpoint</URL>
        </HTTPTargetConnection>
    </RouteRule>
    ....
Randy Solton
  • 364
  • 1
  • 3
  • Hello Randy,this method will give me two API's but i want only one API for two target endpoints. i don't want two resources. – madhurika Apr 02 '14 at 05:14