The sample is described in the following topics.
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 yahoo weather 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.
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.
- Check out the API-Invoker source code using the Git clone command. It's folder structure is as follows:
- Write your application 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
- You can find the source code of this sample in
<AF_HOME>/samplesdirectory and the sample index.jsp with above changes in API_Invoker/API_Invoker/WebContent/index.jsp. Replace this file in API-Invoker.
- Commit the changes to Git repository.
- Subscribe the application to the Yahoo Weather API using steps below:
- In App Factory portal, select the API-Invoker application.
- 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.
- Click the yahoo weather - 1.0.0 API you created earlier. For example,
- Select API-Invoker from 'Applications' drop down and click 'Subscribe' button. You will get the 'Subscription Successful' pop-up.
6. Generate keys for API-Invoker from App Factory.
- Go under 'Subscribe to an API' section and click 'Add Api' button.
- Go under 'Saved Keys' section and click 'Generate Keys' button and keys will be generated for sandbox and production separately.
7. Deploy the application to "Development" environment. Launched app look as follows.
# Give APIManager Url (Eg:- http://apimanager.appfactorypreview.wso2.com - must be a HTTP url).
# Give end point Url of the Yahoo Weather API. (Eg:- http://apimanager.appfactorypreview.wso2.com:8280/yahooweather/1.0.0 ).
# Give Username & Password of the logged user.
# Click on Submit.
# Response with weather information will be printed as an XML.
8. Promote the application to "Production" environment and view results.
Note: The results of Development environment differs from Production environment because the keys that is used by the two environments are different. The programmer is unaware about the sandbox and production keys, and everything is handled underneath.