以下範例使用三項「指派訊息」政策:

  1. 在要求中建立三個流程變數,並使用靜態值
  2. 在要求流程的第二項政策中動態取得流程變數
  3. 在回應的酬載中設定這些值
<!-- Policy #1: Set variables in the request -->

<AssignMessage name="AM-set-variables">
    <!-- Create a variable named myAppSecret -->
    <AssignVariable>
        <Name>myAppSecret</Name>
        <Value>42</Value>
    </AssignVariable>
    <!-- Create a variable named config.environment -->
    <AssignVariable>
        <Name>config.environment</Name>
        <Value>test</Value>
    </AssignVariable>
    <!-- Create a variable named config.protocol -->
    <AssignVariable>
        <Name>config.protocol</Name>
        <Value>gopher</Value>
    </AssignVariable>
</AssignMessage>

在第一個政策中,<AssignVariable> 元素會在要求中建立及設定三個變數。每個 <Name> 元素都會指定變數名稱,而 <Value> 則會指定值。

第二項政策會使用 <AssignVariable> 元素讀取值,並建立三個新變數:

<!-- Policy #2: Get variables from the request -->
<AssignMessage continueOnError="false" enabled="true" name="get-variables">
  <AssignTo createNew="false" transport="http" type="request"/>
  <!-- Get the value of myAppSecret and create a new variable, secret -->
  <AssignVariable>
    <Name>secret</Name>
    <Ref>myAppSecret</Ref>
    <Value>0</Value>
  </AssignVariable>
  <!-- Get the value of config.environment and create a new variable, environment -->
  <AssignVariable>
    <Name>environment</Name>
    <Ref>config.environment</Ref>
    <Value>default</Value>
  </AssignVariable>
  <!-- Get the value of config.protocol and create a new variable, protocol -->
  <AssignVariable>
    <Name>protocol</Name>
    <Ref>config.protocol</Ref>
    <Value>default</Value>
  </AssignVariable>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>

在第二項政策中,<Ref> 元素會參照來源變數,而 <Name> 元素則會指定新變數的名稱。如果 <Ref> 元素參照的變數無法存取,可以使用 <Value> 元素指定的值。

如要試用這組政策:

  1. 將政策 #1 和 #2 新增至要求流程。請務必將政策 1 放在政策 2 之前
  2. 回覆流程中新增第三項政策。
  3. 第三項政策會使用 <Set> 元素,將變數新增至回應。以下範例會在 Edge 回傳給用戶端的回應中建構 XML 酬載:
    <!-- Policy #3: Add variables to the response -->
    <AssignMessage continueOnError="false" enabled="true" name="put-em-in-the-payload">
      <DisplayName>put-em-in-the-payload</DisplayName>
      <Set>
        <Payload contentType="application/xml">
          <wrapper>
            <secret>{secret}</secret>
            <config>
              <environment>{environment}</environment>
              <protocol>{protocol}</protocol>
            </config>
          </wrapper>
        </Payload>
      </Set>
      <IgnoreUnresolvedVariables>true
      </IgnoreUnresolvedVariables>
      <AssignTo createNew="false" transport="http" type="response"/>
    </AssignMessage>

    請注意,在 <Set> 中存取流程變數的語法,是將變數包在半形大括號中。

    請務必將 <Payload> 元素的 contentType 屬性設為「application/xml」。

  4. 傳送要求至 API Proxy,例如:
    curl -vL https://ahamilton-eval-test.apigee.net/myproxy

    您可以選擇透過 xmllint 等公用程式傳送結果,讓 XML 以格式良好的結構顯示:

    curl -vL https://ahamilton-eval-test.apigee.net/myproxy | xmllint --format -

    回應主體應如下所示:

    <wrapper>
      <secret>42</secret>
      <config>
        <environment>test</environment>
        <protocol>gopher</protocol>
      </config>
    </wrapper>