This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 26 Next »

Sometimes, the backend implementation of a web service does not exactly match your desired API design. In that case, you do various message transformations and orchestrate multiple backend services to achieve the design you want.

In this tutorial, you create a custom sequence using the WSO2 Tooling Plug-in and use it in your APIs to mediate the incoming API calls.

The API Cloud comes with a powerful mediation engine that can transform and orchestrate API calls on the fly. It is built on WSO2 ESB and supports a variety of mediators that you can use as building blocks for your sequences. See the list of mediators supported in the API Cloud and WSO2 ESB.

You can extend the API Gateway's default mediation flow to do custom mediation by providing an extension as a synapse mediation sequence. You need to design all sequences using a tool like the WSO2 Tooling Plug-in and then store the sequence in the Gateway's registry.

Let's get started. See the video tutorial here or a step-by-step walk-through of the video tutorial below.

Here's a step-by-step walk-though of the video tutorial:

  1. Log in to the API Publisher.
    https://api.cloud.wso2.com/publisher/ 

  2. Click Add New API, Design a new API,  and Start creating to create an API with the following information and then click Implement.

    Field Sample value
    Name YahooWeather
    Context /weather
    Version 1.0
    ResourcesURL patterncurrent/{country}/{zipcode}
     Request types

    GET method to return the current weather conditions of a ZIP code that belongs to a particular country

    The Implement tab opens. 

  3. Select Managed API, provide the information given in the table below and click Next: Manage >.

    FieldSample value
    Endpoint typeHTTP/REST endpoint
    Production endpoint

    You can find the Yahoo weather API's endpoint from https://developer.yahoo.com/weather/. Copy the part before the '?' sign to get this URL: https://query.yahooapis.com/v1/public/yql

    To verify the URL, click the Test button next to it.

    Sandbox endpoint

    https://query.yahooapis.com/v1/public/yql. To verify the URL, click the Test button next to it.

  4. Provide the following information in the Manage tab and click Save & Publish once you are done.

    FieldSample value
    Tier AvailabilityGold
    Keep the default values for the other attributes 

  5. Download and install the WSO2 API Manager Tooling Plug-in if you have not done so already. 

  6. Open Eclipse.

  7. On the Window menu click Perspective, Open Perspective, Other to open the Eclipse perspective selection window. Alternatively, click the Open Perspective icon shown below at the top right corner.
     

  8. On the dialog box that appears, click WSO2 API Manager and click OK.
  9. On the APIM perspective, click the Login icon as shown below.
     

  10. On the dialog box that appears, enter the cloud username (in the format <email@company_name>) and password, and click OK.

    Make sure that the Connect to WSO2 App Cloud checkbox is not selected.

  11. On the tree view that appears, expand the folder structure of the existing API.
  12. Right-click on the in sequence folder and click Create to create a new in sequence.



    This is because you want the custom sequence to be invoked in the In direction or the request path. If you want it to be involved in the Out or Fault paths, select the respective folder under customsequences.

     Tip : If you prefer not to use the registry to upload the sequence or want to engage a sequence to all APIs in the APIM at once, you can do so by saving the mediation sequence XML file in the file system. See Adding Mediation Extensions for details.
  13. Name the sequence YahooWeatherSequence.
  14. Your sequence now appears on the Developer Studio console. From under the Mediators section, drag and drop a Property mediator to your sequence and give the following values to the property mediator.

    Property NameNew Property
    New Property NameYQL
    Value TypeExpression
    Value Expression

    For the XPath expression, we take a part of the query in the Yahoo API's endpoint (https://developer.yahoo.com/weather/) and concatenate the zip code and country to it using the synapse get-property XPath expression:

    concat('?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22',syn:get-property('uri.var.zipcode'),',',syn:get-property('uri.var.country'),'%22)&format=json')

  15. Similarly, add another property mediator with the following values. This is an HTTP transport property that appends its value to the address endpoint URL. Once you are done, save the sequence.

    Property NameNew Property
    New Property NameREST_URL_POSTFIX
    Value TypeExpression
    Value Expressionget-property('YQL')
    Property ScopeAxis2

  16. Save the sequence.
  17. Right-click on the sequence and click Commit File to push the changes to the Publisher server.
  18. Log back in to the API Publisher, select the API that you created earlier, and click the Edit link right next to its name to go to the edit wizard. 
  19. Navigate to the API's Implement tab, click the Enable Message Mediation checkbox and select the sequence that you created for the In flow. Next, Save the API.

    Tip : It might take a few minutes for the sequence to be uploaded into the API Publisher. If it isn't there, please check again later.

  20. Open the API Store, subscribe to the API that you just published and generate the access tokens in order to invoke the API.
  21. Click the API Console tab of the API. It opens the integrated API Console using which you can invoke the API.
  22. Give the following values for the parameters and invoke the API. You can also give any other value of your choice.

    country

    usa

    zipcode95004

  23. Note the response that you get as a JSON object from Yahoo.

In this tutorial, you created a sequence to change the default mediation flow of API requests, deployed it in the API Gateway and invoked an API using the custom mediation flow.

  • No labels