Nav

Lab 2: Complete the Process API Layer for Order Fulfillment API

Overview

Let us now orchestrate our back-end systems to fulfill the order. We’ll post the order to the Order API.

module9 lab2 api overview

The implementation will consist of a few steps

  1. Extend the Order Fulfillment API stub generated from previous lab

  2. Use Flow Controls such as Scatter Gather to drive the orchestration of this Process API

  3. Call the Order API to create an Order.

  4. Call the Customer API to retrieve user data.

  5. Construct an aggregated Process API response message from System API requests using DataWeave transformations

  6. Run and test the Mule app containing the Process API from Anypoint Studio

Step 1: Setup the POST flow for the Process API

Let’s start with implementing the post:\order_fulfillment:application\json:order-fulfillment-api-config back-end flow.

  1. Back in the flow diagram in Anypoint Studio, click on the Transform Message from the flow named post:\order_fulfillment:application\json:order-fulfillment-api-config

    module9 lab2 studio update transform
  2. Remove the transform component and add a scatter gather.

    module9 lab2 studio scatter gather
    Scatter-Gather sends a request message to multiple targets concurrently. It collects the responses from all routes, and aggregates them into a single message.

Step 2: Create a configuration file

We want to allow easy configuration of the project by using configuration files. To do that we are going to create a yaml file with the configuration of the properties that are going to be used along the lab.

  1. On the Menu Go to File → New → File

    module9 lab2 new file
  2. Complete the File name property with configuration.yaml

    Make sure that you are creating in the api-order-fulfillment/src/main/resources folder

    module9 lab2 new file config
  3. Press Finish

  4. Copy and paste the following values

    system:
      api:
        host:
          customers: mythical-customer-api-v4-0.cloudhub.io
          orders: mythical-order-api-v4-0.cloudhub.io

    One of the benefits of using YAML is that the information in a single YAML file can be easily translated to multiple language types.

  5. In the api.raml file, go to the Global Elements Tab.

    module9 lab2 global elements tab
  6. Press Create button

    module9 lab2 global elements create
  7. Filter by Configuration and select Configuration properties

    module9 lab2 global elements config
  8. Press OK

  9. Complete the File property with the value configuration.yaml

    module9 lab2 configuration panel
  10. Press OK

Step 3: Order API - Consume Connector

We’ll start with calling the Order API, which is using the REST API Modeling Language (RAML) for its definition. Each time you create a RAML Spec and you publish into Exchange, a new connector is generated. We need to add this module to our project.

Preview the Order API by taking a look at its API definition in Exchange.

  1. Click on the Exchange Icon

    module9 lab2 exchange button

    Login with your credentials if necessary.

  2. On the Organization Name, search for Order API

    module9 lab2 exchange order api search
  3. Click on the Order API and explore the API Portal

    module9 lab2 api console order api
  4. Let’s look in details how the POST Method request and response body.

    module9 lab2 order request
    module9 lab2 order response
  5. Click Add to project

  6. A warning pop up window will appear press Proceed

    module9 lab2 proceed

    A window will appear telling that the import was successful.

    After Pressing OK, the Order API module will be ready to be used in the palette.

    module9 lab2 order api palette
    The connector version can change. Be sure that is the last version.
  7. Drag and Drop Create order from the Order API

    module9 lab2 studio create order
  8. Double-click on the connector to display its properties. You should see the component properties for you to configure in the Mule Properties View.

  9. Click on the module9_lab2_add_button icon to create a new configuration as shown:

    module9 lab2 studio api order config add
  10. Complete the Request Configuration

    • Host: mythical-order-api-v4-0.cloudhub.io

    • Port: 80

    • Base Path: /api

    • Protocol: HTTP

      Notice the Host value has the same value we configured in the configuration.yaml file for the Order API URL So we changed the value to ${system.api.host.orders}

      module9 lab2 order api config
      Check the protocol, by default it says HTTPS, but the service is HTTP
  11. Click OK

  12. In the General section, set the following parameters:

    Name Expression Value

    Create order request data

    #[payload]

    Client id

    11def1b704e24d87a5ea5769772c90a7

    Client secret

    88845E529f1F42E4a5aC96Fd504c3e01

    Content type

    application/json

    Accept

    application/json

    The configuration should look similar to this:

    module9 lab1 call order api completed

    You can click on the module9_lab2_transform_button and do a field by field mapping.

  13. Remove the payload word from the Create order request data: box and add {}.

  14. Press Refresh Metadata button.

    module9 lab2 create order request
  15. Click the module9_lab2_transform_button button. We are going to transform the messages going in and out of the RAML Consumers.

  16. Map the fields from the POST call of the Order Fulfillment API to the fields required for the POST call of the Order API. Paste the following DataWeave script into the DataWeave code editor:

    {
    	customerId: payload.customer_id,
    	orderLineItems: (payload.products default []) map ( product , indexOfProduct ) -> {
    		productId: product.id,
    		quantitySafetyStock: product.quantity,
    		quantitiyOnHand: product.quantity,
    		productName: product.productName
    	},
    	status: payload.status,
    	totalPrice: payload.price as String
    }
  17. The mapping should look like the following.

    module9 lab2 studio dw mapping view
  18. Press the module9_lab2_done_button button.

    We don’t want to change the original payload, so we are going to save the Order Id to a variable.

  19. If you are not in the Order API configuration. Focus on Order API connector.

  20. Go to the Advance Tab

    module9 lab2 order advance
  21. Let’s complete the table with the following data:

    Name Expression Value

    Target Variable

    order_id

    Target Value

    #[payload.id]

    We know the output because we checked the API Spec in Exchange. You can also go to the Output tab and check in the Payload tree.

    module9 lab2 output payload
    module9 lab2 studio dw mapping view java
  22. Search for "Business" in the component palette.

    module9 lab2 studio palette business
  23. Drag-and-drop a Customer Business Event connector onto the canvas after the Connector component.

    module9 lab2 studio flow business event
  24. Double-click on the Customer Business Event icon to display its properties. You should see the component properties for you to configure in the Mule Properties View.

  25. Add a meaningful name for the operation in the Display Name and the Event Name field like Order Created Event.

  26. Under the Key Performance Indicators section, click on module9_lab2_add_button to add the following KPIs:

    Name Expression Value

    Order ID

    #[vars.order_id]

    Order Price

    #[payload.price]

    Customer ID

    #[payload.customer_id]

    Order Status

    #[payload.status]

    module9 lab2 studio business event config

Step 4: Customer API - Consume Connector

Next we’ll call the Customer API to retrieve additional information about the customer. Again we are going to add a REST connector to our project. In this case we are going to consume the Get By ID resource.

  1. Preview the Customer API by taking a look at its portal in Exchange.

    module9 lab2 api console customer
  2. Let’s take a look at the response

    module9 lab2 customer response
  3. Click Add to project button.

  4. Follow the same steps you did to import the Order API Connector.

    At the end, the Customer API module will be ready to be used in the palette.

    module9 lab2 customer api palette
    The connector version can change. Be sure that is the last version.
  5. Drag and Drop Get customer by id from the Customer API connector

    module9 lab2 customer by id
  6. Double-click on the connector to display its properties. You should see the component properties for you to configure in the Mule Properties View.

  7. Click on module9_lab2_add_button icon to create a new configuration as shown:

    module9 lab2 customer api config
  8. Define the HTTP configuration you will use with the Customer API.

  9. Complete with the following values:

    • Host: mythical-customer-api-v4-0.cloudhub.io

    • Port: 80

    • Base Path: /api

    • Protocol: HTTP

      Notice the Host value has the same value we configured in the configuration.yaml file for the Order API URL So we changed the value to ${system.api.host.customers}
      module9 lab2 studio http params customer
  10. Click OK

  11. In the General section, set the following parameters:

    Name Expression Value

    Id

    #[payload.customer_id]

    Client id

    11def1b704e24d87a5ea5769772c90a7

    Client secret

    88845E529f1F42E4a5aC96Fd504c3e01

    Content Type

    application/json

    Accept

    application/json

    The configuration should look similar to this:

    module9 lab2 customer api completed
  12. Finally we add a Transform Component after the Scatter-Gather to Build the API Response.

  13. Double-click on the Transform Message Component

  14. Pate the following Dataweave script

    %dw 2.0
    output application/json
    ---
    {
    	order_id: vars.order_id as Number,
    	username: payload."1".payload.name default ""
    }

    The purpose of the lab is not to talk too much about DataWeave, but after the Scatter-Gather we are going to have a payload array. So payload."0".payload it means that you are getting values from the payload that comes from the first branch.

    At the end you should have a flow like this

    module9 lab2 final flow

Step 5: Run the API

Now it’s time to run the API in Anypoint Studio to see how it all works together.

  1. Start the application as done in the previous Lab. (Right-click the application, Run As > Mule Application

  2. Once the API Console View opens in Anypoint Studio, click on the link.

    module9 lab2 api console order fulfillment
  3. Under Try it, view the request body then click on the SEND button below.

    module9 lab2 api console post button fulfillment
  4. Once the request is served, you should see a 200 response code and the JSON response of our API.

    module9 lab2 api console post success response

    You could get 404 Response. This means that the User ID it doesn’t exist.

Summary

In this lab, we

  • Using Anypoint Studio, implemented the Order Fulfillment Stub to complete the Process API layer.

  • Configured the Scatter-Gather flow control and RAML Consumer objects to orchestrate Order and Customer System APIs.

  • Using DataWeave, managed System and API responses to produce desired result set.

  • Tested the Process API implementation that traverses the System API layer using live API calls from RAML Consumers.

Go Further:

Congratulations! You have completed Lab 2.

Please proceed to Lab 3