This documentation is for WSO2 App Factory 1.0.0. View documentation for the latest release.

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

The sample is described in the following topics.:

Table of Contents
maxLevel3
minLevel3

Introduction

<Describe what the sample does. What is the business use case it demonstrates>This sample demonstrates how to invoke yahoo weather API and displays results in a Web application.

Prerequisites

...

  1. Register and log in to WSO2 App Factory

...

<Anything else?>

Executing the Sample

<Please make sure you provide information as to WHY a particular step is executed. For example, rather than just mentioning "copy file x to location, also mention the purpose of that step>

1. Create a API that gives Washington whether details in sandbox and London whether details in the production environment. Lets call this Weather API. Internally it calls the API of  www.worldweatheronline.com

 

2. Create an application in AppFactory. Lets call it Customer Portal - cportal application. When you create an application in App Factory it will be automatically created in API-M.

...

3. Subscribe the application to the Weather API as follows.

...

4. Generate keys by clicking on the create dependencies.

...

  1. here: https://appfactorypreview.wso2.com/appmgt . See instructions in Getting Started.
  2. 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.


    For more information on creating an API, see APIs.
  3. 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.
    Image Added

You now have a sample API created in App Factory. Let's see how to consume this from your Web app.

Executing the sample

  1. Log in to App Factory portal and click Add New Application to create a new application with the following specifications:

    Name: API-Invoker
    Application Type: Web Application
    Repository Type: Git

    For more information on creating applications, see Creating an Application.
  2. Check out the API-Invoker source code using the Git clone command. It's folder structure is as follows:
    Image Added
  3. Replace the WebContent/index.jsp file you find in the above folder structure with the sample index.jsp file found in <AF_HOME>/samples/API_Invoker/WebContent directory. You can find the same file here: /WebContent/index.jsp.

    The 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

...

  1. (consumer key and consumer secret) are used depending on where the application is deployed.

...


 

  1. Code Block

...

  1. language

...

  1. html/xml
    CarbonContext cCtx = CarbonContext.getCurrentContext();
            Registry registry = (Registry) cCtx.getRegistry(RegistryType.SYSTEM_GOVERNANCE);
        	//curl -v -H "Authorization: Bearer KY7QoTynVZShrfguQGTNPST88X8a" http://apimanager.appfactorypreview.wso2.com:8280/twitter/1.0.0/search.atom?q=wso2  	
            Resource resource = registry.get("

...

  1. dependencies/consumerkey");
            if(resource.getContent() instanceof String){
        

...

  1.     	consumerkey = (String) resource.getContent();
            }else{
        

...

...

7. Promote the application to "Production" environment and view results.

...

  1.     	consumerkey = new String((byte[]) resource.getContent());
            }
    		
            resource = registry.get("dependencies/consumersecret");
            if(resource.getContent() instanceof String){
            	secretkey = (String) resource.getContent();
            }else{
            	secretkey = new String((byte[]) resource.getContent());
            }

  6. Deploy the application to "Development" environment and view results

  1. After replacing the file, commit the changes to Git repository.  

  2. 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.
      Image Added
     
  3. After subscribing, you get options to generate access keys. Generate keys for sandbox and production separately.    
  4. 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.
  5. Launch the application by clicking the Test button and providing the following parameters.
    Image Added

  6. Submit the details to receive response with weather information printed as an XML file.
  7. 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 unaware about generally not aware of the sandbox and production keys, and everything . This is handled underneathunder the hood.