This sample demonstrates how to invoke yahoo weather API and displays results in a Web application.
- Register and log in to WSO2 App Factory here: https://appfactorypreview.wso2.com/appmgt . See instructions in Getting Started.
- Log in to API Publisher ( https://apimanager.appfactorypreview.wso2.com/publisher ) and add API by the name yahooweather - 1.0.0. Provide the following values in Add New API window.
- Produciton URL - http://weather.yahooapis.com/forecastrss?w=2502265
- Sandbox URL - http://weather.yahooapis.com/forecastrss?w=2502265
- Auth Type of API Resource as Application & Application User
For more information on creating an API, see APIs.
- After creating the API, go to the Life Cycle tab in the API Publisher. Then, select the State to PUBLISHED and update. This publishes the API to App Factory's storefront so that application developers can find and subscribe to it.
You now have a sample API created in App Factory. Let's see how to consume this from your Web app.
Executing the sample
- Log in to App Factory portal and click Add New Application to create a new application with the following specifications:
Application Type: Web Application
Repository Type: Git
For more information on creating applications, see Creating and Viewing Applications.
- C heck out the API-Invoker source code using the Git clone command. It's folder structure is as follows:
Replace the WebContent/index.jsp file you find in the above folder structure with the sample index.jsp file found in either
<AF_HOME>/samples/API_Invokerdirectory. You can find the same file here: .
This sample index.jsp has the following code segment added to get keys by calling the CarbonContext and Registry APIs. The correct sandbox and production keys (consumer key and consumer secret) are used depending on where the application is deployed.
After replacing the file, commit the changes to Git repository.
- Next, subscribe to the yahooweather API using the application created above. Steps below cover this:
- Log in to the App Factory portal and select the API-Invoker application created before.
- Select the Resources tab from the left panel and scroll down to select APIs section. Then, click Subscribe to APIs.
- The API Manager's storefront opens in a separate window: https://apimanager.appfactorypreview.wso2.com/store. It lists all APIs published to the store, including yahooweather - 1.0.0 API you created and published earlier.
- Click the yahooweather - 1.0.0 API to open its details. Se lect API-Invoker from Applications drop down and Subscribe.
- After subscribing, you get options to generate access keys. Generate keys for sandbox and production separately.
- Next, come back to the App Factory portal and select the Repos and Builds tab. Build and Deploy API-Invoker application in the development environment by simply clicking the relevant buttons in the UI screen.
- Launch the application by clicking the Test button and providing the following parameters.
- API Manager Url must be HTTP: http://apimanager.appfactorypreview.wso2.com
- End point Url of the Yahoo Weather API: http://apimanager.appfactorypreview.wso2.com:8280/yahooweather/1.0.0
- Username & Password of the logged user
- Submit the details to receive response with weather information printed as an XML file.
- Promote the application to Production environment and view results. For details on how to promote applications, see Promoting an application.
The results you get in development environment differ from the same in production environment, because the keys used by the two environments are different. The programmer is generally not aware of the sandbox and production keys. This is handled under the hood.