One API proxy calling two different target endpoints

假如想象 提交于 2019-12-01 11:29:35

问题


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 ?


回答1:


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>



回答2:


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:

(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>
    ....


来源:https://stackoverflow.com/questions/22783986/one-api-proxy-calling-two-different-target-endpoints

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!