【问题标题】:API Management Conditional Flow PolicyAPI 管理条件流策略
【发布时间】:2020-01-28 09:33:36
【问题描述】:

我有以下请求数据

{
   "id": "1115e297-12de-4189-8e11-75a8354f77d6",
   "type": "transaction",
   "version": "1.0.0",
   "data": {
      "account_id": "999",
      "customer_info": {
         "client_id": 999
      },
      "company_id": "999999",
      "event": {
         "id": 99999999,
         "type_code": "3098"
      }
   }
}

我想编写一个忽略请求的策略,除非类型是“事务”并且 type_code 是“3098”或“3099”

如果不满足,那么它应该只是将请求的 id 返回给调用者,而不是转发请求

这是我想出来的

    <choose>
            <when condition="@((string)context.Request.Body?.As<JObject>(true)["type"] != "transaction")">
                <return-response>
                    <set-status code="200" reason="OK" />
                    <set-header name="Content-Type" exists-action="override">
                        <value>application/json</value>
                    </set-header>
                    <set-body>@{ 
                        var json = new JObject( 
                            new JProperty("id", context.Request.Body?.As<JObject>(true)["id"])
                        ); 

                        return json.ToString(); 
                    }</set-body>
                </return-response>
            </when>
            <when condition="@(context.Request.Body?.As<JObject>(true)["data"] == null || context.Request.Body?.As<JObject>(true)["data"]["event"] == null)">
                <return-response>
                    <set-status code="200" reason="OK" />
                    <set-header name="Content-Type" exists-action="override">
                        <value>application/json</value>
                    </set-header>
                    <set-body>@{ 
                        var json = new JObject( 
                            new JProperty("id", context.Request.Body?.As<JObject>(true)["id"])
                        ); 

                        return json.ToString(); 
                    }</set-body>
                </return-response>
            </when>
            <when condition="@((int)context.Request.Body?.As<JObject>(true)["data"]["event"]["type_code"] != 3098 && (int)context.Request.Body?.As<JObject>(true)["data"]["event"]["type_code"] != 3099)">
                <return-response>
                    <set-status code="200" reason="OK" />
                    <set-header name="Content-Type" exists-action="override">
                        <value>application/json</value>
                    </set-header>
                    <set-body>@{ 
                        var json = new JObject( 
                            new JProperty("id", context.Request.Body?.As<JObject>(true)["id"])
                        ); 

                        return json.ToString(); 
                    }</set-body>
                </return-response>
            </when>
        </choose>

我提示有一种更优雅的方法可以做到这一点?

【问题讨论】:

    标签: azure-api-management api-management


    【解决方案1】:

    当然。如果您的所有return-response 策略都相同,您可以使用多行表达式:

    <set-variable name="payload" value="@((string)context.Request.Body?.As<JObject>(true))" />
    <choose>
        <when condition="@{
            var payload = (JObject)context.Variables["payload"];
            if (payload?["type"] != "transaction"
                || payload?["data"]?["event"]?["type_code"] != 3099)
            {
                return true;
            }
        }">
            <return-response>
                <set-status code="200" reason="OK" />
                <set-header name="Content-Type" exists-action="override">
                    <value>application/json</value>
                </set-header>
                <set-body>@{ 
                    var payload = (JObject)context.Variables["payload"];
    
                    var json = new JObject( 
                        new JProperty("id", payload?["id"])
                    ); 
    
                    return json.ToString(); 
                }</set-body>
            </return-response>
        </when>
    </choose>
    

    【讨论】: