בדוגמה הבאה נעשה שימוש בשלוש מדיניות של הקצאת הודעות:
<!-- 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> כדי להוסיף את המשתנים לתשובה. בדוגמה הבאה מוצג מטען ייעודי (payload) של 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>