Monday, February 17, 2014

Accessing a API via WSO2 API Manager

In this blog post i'm going to demonstrate how we could access a api via wso2 APIM. For this we have to have an REST service/ API. You can create a REST service using WSO2 Developer Studio[1] and host it on WSO2 AS[2]. But for the time being and to make this post short (and sweet :)) I'll create a mock api within WSO2 ESB.


Implementing a mock Api in WSO2 ESB


Start WSO2 ESB 4.8.0 using ./wso2server.sh or (wso2server.bat if windows )command in the /bin directory. Wait till the server starts and login to the management console.
Copy Paste the following code in ESB's management consoles source view. It will create a simple api for you. This api is just a hypothetical one which would give the price of pizza when u specify the size and type as query parameters. Note that this is not fully implement to give the price but it would give a hypothetical soap response constructed from inputs as the output (see the bottom line).


   
      
         
            
            
         
         
            
               
                  
                     $1
                     $2
                  
               
            
            
               
               
            
         
         
         
         
      
   
                

Publishing API in APIM

Download the latest WSO2 APIM (1.6) unzip and navigate to /bin directory and start the server with port offset of 1 (one). Here we need to have a port offset since we have ESB is running on same machine. Use command like follows
./wso2server.sh -DportOffset=1   (wso2server.bat -DportOffset=1 if windows)

Then log into the APIM publisher (https://localhost:9444/).

Click on Add link in the left panel and fill the 'Add New API' form fields as follows



Name = TestPizaaApi                                                           // Just give a name u wish
Context = /pizza                                                                    // As you wish
Version = 1.0.0                                                                     // Since this is my initial round
Production Endpoint = http://localhost:8280/order        // This need to be the endpoint adress of API we deployed in ESB, i have copy this from the ESB management console

And click on create button at the bottom. The you will see your Api named TestPizzaApi-1.0.0 with a following icon and information
(admin)
0 Users
CREATED

It says that the api is in created state and no user subscriptions yet. We will set this to PUBLISHED state now.
Click on the above icon and navigate to Lifecycle tab and select the 'PUBLISHED' from 'State' dropdown. Then click on Update. And you may see a message at the bottom saying "admin changed the API status from 'CREATED ' to 'PUBLISHED'".



Subscribing To API


To subscribe to an api we need to log into api store, Navigate to https://localhost:9444/store/ and you will see the api you created is listed there. Click on that icon and you would see a page like follows





Then go to "My Subscriptions" you will have to provide the credentials for this. Give default admin credentials (username admin, password admin) and subscribe using Default Application, Then you will get Access token, Consumer key and Consumer secret for your subscription. It would look something like follows 
Access Token
be0bb848919f630473b1ca998122422
 Token Validity:  Seconds
Consumer Key
dEgShq0acvPYoViHz68scyZ2Gkka
Consumer Secret
j03yOscbTHUy2oDt4RCfldNOdqka

Copy the access token, since we need this to invoke the api.


Invoke the API
You can use a rest client to invoke this API, You can construct the endpoint using following information.

  • Production url  http://localhost:8281/pizza/1.0.0    // can obtain from APIM (figure 2)
  • uri-template is /getPrice?Size={size}&Type={type} // As per the API definition(in ESB)

So our endpoint would be something like follows,

http://localhost:8281/pizza/1.0.0/order/getPrice?Size=Large&Type=CheilleChicken

Here i'm trying to get the price for a Large CheilleChicken pizza :)

Execute the following command in terminal, replace the access_token with your one.

curl -k -H "Authorization: Bearer " http://localhost:8281/pizza/1.0.0/getPrice?Size=Large&Type=CheilleChicken

Once you execute you would get the following output.

ChickenLarge

enjoy it :)

[1] - http://wso2.com/products/developer-studio/
[2] - http://wso2.com/products/application-server/

Friday, February 14, 2014

WSO2 ESB - Access query parameters in REST API

Not only SOAP, WSO2 ESB also provides the REST support, you can define a REST API within wso2 ESB or you can define this using WSO2 Developer Studio and then deploy it to ESB using .car file. Sample 800[1] of WSO2 ESB docs describes how you can access a REST API, however the sample only describes uri-parameters style, not the query string parameter style. If you access it as described in [1] i.e http://127.0.0.1:8280/stockquote/view/IBM style, the sample would work.
However if you want to access the service like http://127.0.0.1:8280/stockquote/view?Name=IBM&Param2=dummy format, you have do to some minor modifications to the sample[1] to make it work. Its only a single line, just replace the line #4 as follows.

 
Here there is no usage of para2 in this example, but i have just added this to demonstrate how you can access multiple parameters. Note the log mediator line #6, which is use to access the query parameters and log them. You can use property mediators to get the query parameters using expressions.


    
    

Here is the modified code for the sample 800

   
      
         
            
            
         
         
            
               
                  
                     $1
                  
               
            
            
               
            
         
         
Just add this API to esb and invoke using curl or simply you can use the browser. Paste http://127.0.0.1:8280/stockquote/view?Name=IBM&Param2=dummy in the address bar and hit 'Enter'. You will see the StockQuote response for the symbol IBM. If you looked at the server console you may see the following log.

INFO - LogMediator To: /stockquote/view?Name=IBM&Param2=dummy, MessageID: urn:uuid:1489b1cf-9a43-4fc6-9904-c6cd2244c87f, Direction: request, SymboValue = IBM, Param2Value = dummy 

[1] - http://docs.wso2.org/display/ESB480/Sample+800:+Introduction+to+REST+API