制御フロー

適用対象: すべての API Management レベル

choose ポリシーを使用して、ブールの評価の結果に基づいてポリシー ステートメントを条件付きで適用します。 if-then-else または switch コンストラクトのような制御フローのポリシーをプログラミング言語で使用します。

Note

ポリシーの要素と子要素を、ポリシー ステートメントで指定された順序で設定します。 API Management ポリシーを設定または編集する方法について説明します。

ポリシー ステートメント

<choose>
    <when condition="Boolean expression | Boolean constant">
        <!— one or more policy statements to be applied if the above condition is true  -->
    </when>
    <when condition="Boolean expression | Boolean constant">
        <!— one or more policy statements to be applied if the above condition is true  -->
    </when>
    <otherwise>
        <!— one or more policy statements to be applied if none of the above conditions are true  -->
    </otherwise>
</choose>

choose ポリシーは、少なくとも 1 つの <when/> 要素を含まなければなりません。 <otherwise/> 要素は省略可能です。 <when/> 要素内の条件は、ポリシーに記述されている順序で評価されます。 条件属性が true と等しい最初の <when/> 要素に含まれているポリシー ステートメントが適用されます。 <otherwise/> 要素 (存在する場合) に含まれているポリシーは、<when/> 要素の条件属性がすべて false の場合に適用されます。

要素

要素 説明 必須
when choose ポリシーの if または ifelse 部分を指定する 1 つ以上の要素。 複数の when 要素が指定されている場合は、順番に評価されます。 when 要素のいずれかの conditiontrue に評価されると、それ以降の when 条件は評価されません。 Yes
otherwise when 条件のいずれも true に評価されない場合に評価されるポリシー スニペット。 いいえ

when の属性

属性 説明 必須
condition 含んでいる when ポリシー ステートメントが評価されるときに評価されるブール式またはブール型定数。 はい

使用法

ユーザー エージェントに基づいて要求と応答を変更する

次の例は、set-variable ポリシーと 2 つの制御フローのポリシーを示しています。

変数の設定ポリシーは inbound セクションにあり、User-Agent 要求ヘッダーにテキスト iPad または iPhone が含まれる場合に true に設定される isMobile ブール型isMobile変数を作成します。

最初の制御フロー ポリシーも inbound セクションにあり、isMobile コンテキスト変数の値に応じて 2 つのクエリ文字列パラメーターの設定ポリシーのうち 1 つを条件付きで適用します。

2 番目の制御フロー ポリシーは outbound セクションにあり、isMobiletrue に設定されている場合に XML から JSON への変換ポリシーを条件付きで適用します。

<policies>
    <inbound>
        <set-variable name="isMobile" value="@(context.Request.Headers.GetValueOrDefault("User-Agent","").Contains("iPad") || context.Request.Headers.GetValueOrDefault("User-Agent","").Contains("iPhone"))" />
        <base />
        <choose>
            <when condition="@(context.Variables.GetValueOrDefault<bool>("isMobile"))">
                <set-query-parameter name="mobile" exists-action="override">
                    <value>true</value>
                </set-query-parameter>
            </when>
            <otherwise>
                <set-query-parameter name="mobile" exists-action="override">
                    <value>false</value>
                </set-query-parameter>
            </otherwise>
        </choose>
    </inbound>
    <outbound>
        <base />
        <choose>
            <when condition="@(context.Variables.GetValueOrDefault<bool>("isMobile"))">
                <xml-to-json kind="direct" apply="always" consider-accept-header="false"/>
            </when>
        </choose>
    </outbound>
</policies>

製品名に基づいて応答を変更する

次の例に、バックエンド サービスから受信した応答で Starter 製品が使用されている場合にデータ要素を削除して、内容のフィルター処理を行う方法を示します。 このバックエンド応答の例には、OpenWeather One Call API に似たルート レベルのプロパティが含まれています。

<!-- Copy this snippet into the outbound section to remove a number of data elements from the response received from the backend service based on the name of the product -->
<choose>
  <when condition="@(context.Response.StatusCode == 200 && context.Product.Name.Equals("Starter"))">
    <set-body>@{
        var response = context.Response.Body.As<JObject>();
        foreach (var key in new [] {"current", "minutely", "hourly", "daily", "alerts"}) {
          response.Property (key).Remove ();
        }
        return response.ToString();
      }
    </set-body>
  </when>
</choose>

ポリシーに対する処理の詳細については、次のトピックを参照してください。