제어 흐름

적용 대상: 모든 API Management 계층

choose 정책을 사용하여 부울 의 평가 결과에 따라 정책 문을 조건부로 적용합니다. if-then-else 또는 프로그래밍 언어의 switch 구문과 유사한 제어 흐름에 정책을 사용합니다.

참고 항목

정책 문에 제공된 순서대로 정책의 요소 및 자식 요소를 설정합니다. 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 정책에는 <when/> 요소가 한 개 이상 포함되어야 합니다. <otherwise/> 요소는 선택적입니다. 정책 내에 표시되는 순서대로 <when/> 요소의 조건이 평가됩니다. 조건 특성이 true인 첫 번째 <when/> 요소 내에 포함된 정책 문이 적용됩니다. 모든 <when/> 요소 조건 특성이 false인 경우 <otherwise/> 요소 내에 포함된 정책(있는 경우)이 적용됩니다.

Elements

요소 설명 필수
다음과 같은 경우... choose 정책의 if 또는 ifelse 부분을 지정하는 요소가 한 개 이상일 경우. 여러 when 요소를 지정할 경우 순차적으로 평가됩니다. when 요소의 conditiontrue로 평가되면 when 조건이 더 이상 평가되지 않습니다.
otherwise true로 평가되는 when 조건이 없을 경우 평가할 정책 조각 모음. 아니요

when 특성

attribute 설명 필수
condition 포함하는 when 정책 문이 평가될 때 평가할 부울 식 또는 부울 상수.

사용

예제

사용자 에이전트에 따른 요청 및 응답 수정

다음 예에서는 set-variable 정책과 두 개의 제어 흐름 정책을 보여 줍니다.

변수 설정 정책은 인바운드 섹션에 있으며 User-Agent 요청 헤더에 iPad 또는 iPhone 텍스트가 포함되는 경우 true로 설정되는 isMobile 부울 컨텍스트 변수를 생성합니다.

첫 번째 제어 흐름 정책도 인바운드 섹션에 있으며 isMobile 컨텍스트 변수 값에 따라 두 쿼리 문자열 매개 변수 설정 정책 중 하나를 조건부로 적용합니다.

두 번째 제어 흐름 정책은 아웃바운드 섹션에 있으며 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>

정책 작업에 대한 자세한 내용은 다음을 참조하세요.