Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions variables/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Variables

This sample demonstrates how you can use the ExtractVariables policy to
populate variables. The sample ExtractVariables policy sets the variable values
as HTTP headers in the response.

The sample policy demonstrates how to populate variable from the context,
API proxy configuration, request message, and m by parsing message content.

The sample converts the XML response to from Yahoo! weather to demonstrate
how to configure the ExtractVariables policy with XPath and JSONPath
to work with message content.

You can also refer to the API Platform variables reference:

http://apigee.com/docs/enterprise/content/predefined-variables

# Configure

Update `/setup/setenv.sh` with your environment details

# Import and deploy sample project

To deploy, run `$ sh deploy.sh`

To test, run `$ sh invoke.sh`

# Get help

For assistance, please use [StackOverflow](http://stackoverflow.com/tags/apigee) and add the tag "apigee".

Copyright © 2014 Apigee Corporation

Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy
of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
23 changes: 23 additions & 0 deletions variables/apiproxy/policies/parseforecast.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<ExtractVariables name="parseforecast">
<!-- Parse the XML weather report using XPath. -->
<Source>message</Source>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<VariablePrefix>weather</VariablePrefix>
<XMLPayload stopPayloadProcessing="false">
<Namespaces>
<Namespace prefix="yweather">http://xml.weather.yahoo.com/ns/rss/1.0</Namespace>
</Namespaces>
<Variable name="location" type="string">
<XPath>/rss/channel/yweather:location/@city</XPath>
</Variable>
<Variable name="condition" type="string">
<XPath>/rss/channel/item/yweather:condition/@text</XPath>
</Variable>
<Variable name="forecast_today" type="string">
<XPath>/rss/channel/item/yweather:forecast[1]/@text</XPath>
</Variable>
<Variable name="forecast_tomorrow" type="string">
<XPath>/rss/channel/item/yweather:forecast[2]/@text</XPath>
</Variable>
</XMLPayload>
</ExtractVariables>
124 changes: 124 additions & 0 deletions variables/apiproxy/policies/parsejsonforecast.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<ExtractVariables name="parsejsonforecast">
<Source>message</Source>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<VariablePrefix>weather</VariablePrefix>
<JSONPayload>
<Variable name="description">
<JSONPath>$.rss.channel.description</JSONPath>
</Variable>
<Variable name="condition">
<JSONPath>$.rss.atmosphere.humidity</JSONPath>
</Variable>
</JSONPayload>
</ExtractVariables>



<!-- Sample JSON response

{
"rss": {
"channel": {
"astronomy": {
"sunrise": "6:16 am",
"sunset": "8:10 pm"
},
"atmosphere": {
"humidity": "82",
"pressure": "30.02",
"rising": "1",
"visibility": "10"
},
"description": "Yahoo! Weather for Palo Alto, CA",
"image": {
"height": "18",
"link": "http://weather.yahoo.com",
"title": "Yahoo! Weather",
"url": "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif",
"width": "142"
},
"item": {
"condition": {
"code": "26",
"date": "Tue, 06 Aug 2013 7:46 am PDT",
"temp": "59",
"text": "Cloudy"
},
"description": "\n<img src=\"http://l.yimg.com/a/i/us/we/52/26.gif\"/><br />\n<b>Current Conditions:</b><br />\nCloudy, 59 F<BR />\n<BR /><b>Forecast:</b><BR />\nTue - AM Clouds/PM Sun. High: 73 Low: 55<br />\nWed - AM Clouds/PM Sun. High: 73 Low: 54<br />\nThu - AM Clouds/PM Sun. High: 72 Low: 54<br />\nFri - Partly Cloudy. High: 73 Low: 54<br />\nSat - AM Clouds/PM Sun. High: 73 Low: 53<br />\n<br />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Palo_Alto__CA/*http://weather.yahoo.com/forecast/USCA1093_f.html\">Full Forecast at Yahoo! Weather</a><BR/><BR/>\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)<br/>\n",
"forecast": [
{
"code": "30",
"date": "6 Aug 2013",
"day": "Tue",
"high": "73",
"low": "55",
"text": "AM Clouds/PM Sun"
},
{
"code": "30",
"date": "7 Aug 2013",
"day": "Wed",
"high": "73",
"low": "54",
"text": "AM Clouds/PM Sun"
},
{
"code": "30",
"date": "8 Aug 2013",
"day": "Thu",
"high": "72",
"low": "54",
"text": "AM Clouds/PM Sun"
},
{
"code": "30",
"date": "9 Aug 2013",
"day": "Fri",
"high": "73",
"low": "54",
"text": "Partly Cloudy"
},
{
"code": "30",
"date": "10 Aug 2013",
"day": "Sat",
"high": "73",
"low": "53",
"text": "AM Clouds/PM Sun"
}
],
"guid": {
"": "USCA1093_2013_08_10_7_00_PDT",
"isPermaLink": "false"
},
"lat": "37.4",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Palo_Alto__CA/*http://weather.yahoo.com/forecast/USCA1093_f.html",
"long": "-122.17",
"pubDate": "Tue, 06 Aug 2013 7:46 am PDT",
"title": "Conditions for Palo Alto, CA at 7:46 am PDT"
},
"language": "en-us",
"lastBuildDate": "Tue, 06 Aug 2013 7:46 am PDT",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Palo_Alto__CA/*http://weather.yahoo.com/forecast/USCA1093_f.html",
"location": {
"city": "Palo Alto",
"country": "United States",
"region": "CA"
},
"title": "Yahoo! Weather - Palo Alto, CA",
"ttl": "60",
"units": {
"distance": "mi",
"pressure": "in",
"speed": "mph",
"temperature": "F"
},
"wind": {
"chill": "59",
"direction": "340",
"speed": "8"
}
},
"version": "2.0"
}
} -->
38 changes: 38 additions & 0 deletions variables/apiproxy/policies/variables.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<AssignMessage name="variables">
<AssignTo createNew="false" type="response"></AssignTo>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<Set>
<Headers>
<Header name="system.timestamp">{system.timestamp}</Header>
<Header name="system.time">{system.time}</Header>
<Header name="organization.name">{organization.name}</Header>
<Header name="apiproxy.name">{apiproxy.name}</Header>
<Header name="apiproxy.revision">{apiproxy.revision}</Header>
<Header name="proxy.basepath">{proxy.basepath}</Header>
<Header name="proxy.name">{apiproxy.name}</Header>
<Header name="proxy.pathsuffix">{proxy.pathsuffix}</Header>
<Header name="message.headers.count">{message.headers.count}</Header>
<Header name="message.headers.names">{message.headers.names}</Header>
<Header name="client.ip">{client.ip}</Header>
<Header name="request.uri">{request.uri}</Header>
<Header name="request.headers.names">{request.headers.names}</Header>
<Header name="request.header.user-agent.values">{request.header.user-agent.values}</Header>
<Header name="request.path">{request.path}</Header>
<Header name="request.querystring">{request.querystring}</Header>
<Header name="request.queryparams.names">{request.queryparams.names}</Header>
<Header name="request.queryparam.w">{request.queryparam.w}</Header>
<Header name="request.verb">{request.verb}</Header>
<Header name="target.url">{target.url}</Header>
<Header name="target.host">{target.host}</Header>
<Header name="target.ip">{target.ip}</Header>
<!-- Variables populated by parsing XML response with an ExtractVariables policy -->
<Header name="weather.location">{weather.location}</Header>
<Header name="weather.condition">{weather.condition}</Header>
<Header name="weather.forecast_today">{weather.forecast_today}</Header>
<Header name="weather.forecast_tomorrow">{weather.forecast_tomorrow}</Header>
<!-- Variables populated by parsing JSON response with an ExtractVariables policy -->
<Header name="weather.description">{weather.description}</Header>
<Header name="weather.humidity">{weather.humidity}</Header>
</Headers>
</Set>
</AssignMessage>
4 changes: 4 additions & 0 deletions variables/apiproxy/policies/xmltojson.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<XMLToJSON name="xmltojson">
<Options>
</Options>
</XMLToJSON>
19 changes: 19 additions & 0 deletions variables/apiproxy/proxies/default.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<ProxyEndpoint name="default">
<Flows>
<Flow name="default">
<Response>
<Step><Name>parseforecast</Name></Step>
<Step><Name>xmltojson</Name></Step>
<Step><Name>parsejsonforecast</Name></Step>
<Step><Name>variables</Name></Step>
</Response>
</Flow>
</Flows>
<HTTPProxyConnection>
<BasePath>/variables</BasePath>
<VirtualHost>default</VirtualHost>
</HTTPProxyConnection>
<RouteRule name="Route">
<TargetEndpoint>default</TargetEndpoint>
</RouteRule>
</ProxyEndpoint>
9 changes: 9 additions & 0 deletions variables/apiproxy/resources/jsc/minimize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var orig = JSON.parse(response.content);
var channel = orig.rss.channel;
var nr = { city: channel.yweather_location.a.city,
state: channel.yweather_location.a.region,
country: channel.yweather_location.a.country,
temperature: channel.item.yweather_condition.a.temp,
conditions: channel.item.yweather_condition.a.text,
windSpeed: channel.yweather_wind.a.speed };
response.content = JSON.stringify(nr);
5 changes: 5 additions & 0 deletions variables/apiproxy/targets/default.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<TargetEndpoint name="default">
<HTTPTargetConnection>
<URL>http://weather.yahooapis.com</URL>
</HTTPTargetConnection>
</TargetEndpoint>
2 changes: 2 additions & 0 deletions variables/apiproxy/variables.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<APIProxy name="variables">
</APIProxy>
15 changes: 15 additions & 0 deletions variables/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

source ../../setup/setenv.sh

echo "Enter your password for the Apigee Enterprise organization $org, followed by [ENTER]:"

read -s password

echo Deploying $proxy to $env on $url using $username and $org

../../tools/deploy.py -n variables -u $username:$password -o $org -h $url -e $env -p / -d ../variables

echo "If 'State: deployed', then your API Proxy is ready to be invoked."

echo "Run 'invoke.sh'"
7 changes: 7 additions & 0 deletions variables/invoke.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

echo Using org and environment configured in /setup/setenv.sh, $org and $env

source ../../setup/setenv.sh

curl http://$org-$env.$api_domain/variables/forecastrss?"w=12797282" -i