Child pages
  • Change the Default Mediation Flow of API Requests v2
Skip to end of metadata
Go to start of metadata
  1. Log in to the API Publisher.

  2. Click Add 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

  3. The Implement tab opens. Provide the information given in the table below and click Manage.

    FieldSample value
    Implementation methodBackend
    Endpoint typeHTTP 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

  4. Click Manage to go to the Manage tab, provide the following information and click Save & Publish once you are done.

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

     

  5. Download WSO2 Developer Studio (version 3.7.1 is used here) from http://wso2.com/products/developer-studio/ and open it by double clicking the Eclipse.app file inside the downloaded folder. 

    Tip: To start Eclipse on a Mac for the first time, open a terminal and execute the following commands:

    cd <DevStudio_Home>/Eclipse.app/Contents/MacOS/
    chmod +x eclipse
    ./eclipse

    Thereafter, you can start Eclipse by double-clicking the Eclipse icon in <DevStudio_Home>.

  6. Click the Developer Studio menu and choose Open Dashboard. When the dashboard opens, click the ESB Config Project link.
  7. Create a new ESB project by the name YahooConfigProject.
  8. Click the Sequence link on the Developer Studio Dashboard and create a new sequence by the name YahooWeatherSequence and the YahooConfigProject project.
  9. 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 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')

  10. 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 NameREST_URL_POSTFIX
    Value TypeExpression
    Value ExpressionSet the value of the property mediator that you created earlier as get-property('YQL')
    Property ScopeAxis2

  11. Log in to the API Gateway's management console. If you are using WSO2 Cloud, the Gateway URL is https://gateway.api.cloud.wso2.com/carbon/admin/login.jsp. If you are using a local setup, the URL is https://localhost:9443/carbon. You get the user name from the top right-hand corner of the API Publisher.
  12. After logging in, click the Browse menu under the Resources menu. 
     
  13. When the API Gateway's registry opens, navigate to the registry path /_system/governance/apimgt/customsequences/in. This is because you want the custom sequence to be invoked in the In direction or the request path.
  14. Click Add Resource and upload the XML file of the sequence that you created earlier.
     

  15. 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. 
     
  16. Navigate to the API's Manage tab, click the check box next to sequences and select the sequence that you created for the In flow. Next, Save & Publish the API again.

    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.

  17. Open the API Store, subscribe to the API that you just published and generate the access tokens in order to invoke the API.
     
  18. Open the integrated REST client using the Tools -> REST Client menu.
     
  19. Invoke the API by selecting the GET method and providing the following information in the rest of the fields:

    API URL

    To get the URL, go to the API's Overview tab in the API Store. Then append the resource to it. For example, http://gateway.api.cloud.wso2.com:8280/t/yashiracom/weather/1.0/current/usa/95004 where /weather is the context, 1.0.0 is the version and current/country/zipcode is the resource.

    HeaderAuthorization:Bearer <give the access token that you got when subscribing to the API>

     

  20. 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