يستخدم المثال التالي ثلاث سياسات "تعيين الرسالة":
<!-- 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>.
لتجربة هذه المجموعة من السياسات، اتّبِع الخطوات التالية:
<Set> لإضافة المتغيّرات إلى الردّ. ينشئ المثال التالي حمولة XML في الردّ الذي تعرضه Edge للعميل:
<!-- 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> هي وضعها بين أقواس معقوفة.
احرص على ضبط السمة contentType للعنصر <Payload> على "application/xml".
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>