Nav

Lab 2: Consume a reusable Social Network API

Overview

Now that Sheryl has designed a flow that accesses customer data from a reusable Customer API, she can proceed with consuming a Social Network API from the same flow to send the customer data to Facebook to fulfill her marketing campaign needs. Lets once again play the role of Sheryl in completing this flow design with the Social Network API for Facebook.

Step 1: Design the Flow

In the previous lab we consumed the Customer API by getting all the customers. Now, We would like to apply the following logic.

  1. Check if the customer call has any data

  2. If successful,

    1. Create an Audience

    2. Get the Audience ID

    3. Create the Facebook Campaign

  3. If failure

    1. Set the response code to 400 (400 is the status code for Bad Requests)

    2. Set an error message to communicate to the user

Step 2: Check If Collection is not Empty

The steps that needs to be executed are:

  1. After the Customer API, press module4_lab1_add_button and select the choice component.

    module4 lab2 select choice

    Most fields can be populated with either a static value or an expression. Switch between one modality or another by clicking the expression button

    When expressions are enabled on a field, you can write a DataWeave expression. This can consist of a simple reference to a part of the element’s input (such as 'payload.user'), or may also involve functions to manipulate incoming values into something else. Smart autocomplete options help you write valid expressions that reference existing elements.

    When using the expression editor, anything you type into the field is considered a DataWeave expression. Therefore, you shouldn’t wrap your expression in #[], as this is already implied.
  2. Complete the Card with the following information:

    • Expression: payload != null (This should be set as a DataWeave expression)

    • Display Name: If Not Empty

      module4 lab2 choice complete

      You can see here that you are creating an if then else statement.

      Now let’s continue with the default branch

Step 3: Create Not Found Message

Here we are going to send an error message when no customer is Found

  1. Click on the module4_lab1_add_button that is beside the default box.

    module4 lab2 add default
  2. Add a Transform component

  3. Go to the script tab

    module4 lab2 default transform script
  4. Modify the null value with No Customers Found

    module4 lab2 no cusotmer found
  5. Close the card.

Step 4: Create an Audience

  1. Click on the module4_lab1_add_button that has a red number 1.

    module4 lab2 choice select then

    Here we are going to consume another API, so we need to select another REST Connector

  2. Search for Social Network on the select component panel.

    module4 lab2 social network filter
  3. Click on it and select Create Audience

    module4 lab2 create audience
  4. Create an Audience based on the Customer data by adding a new card titled Social Network API for Facebook.

    1. Complete the card with the following information:

      • Client Id : 11def1b704e24d87a5ea5769772c90a7

      • Client Secret: 88845E529f1F42E4a5aC96Fd504c3e01

      • Content_Type : application/json

      • Accept: application/json

        module4 lab2 create audience api
  5. Press the Edit link

    module4 lab2 create audience api edit
  6. Complete with the following information:

    • Configuration Name: Social Network API for Facebook

    • Host: social-network-api.cloudhub.io

    • Port: 80

    • Base Path: /api

    • Protocol: HTTP

      module4 lab2 create audience host conf

      According to the API RAML Specification we need to adapt the customer information to what the API needs.

  7. If you go over the arrow that is between If Not Empty and Social Network API for Facebook it will convert into an plus button. Press the button

    module4 lab2 add transformation plus
  8. Select a Transform component

    module4 lab2 select transform

    After you select the transform option, you are going to see a panel like this one:

    module4 lab2 transform panel
    • The first panel is the input. In this case is the Customer API output.

    • The second panel is the output. In this case is what the Social Network API for Facebook is expecting

    • The third panel shows you how the mapping ends.

  9. You can drag and drop the fields from the input to the output and the mapping will be generated. In this case to make it faster, we are going to go to the script tab and copy the following text

    %dw 2.0
    
    output application/json
    
    ---
    {
      customers: (payload map (value0, index0) -> {
        country: value0.billingAddress.country,
        phone: value0.phone,
        city: value0.billingAddress.city,
        postalCode: value0.billingAddress.postalCode,
        email: value0.email,
        product: []
      })
    }
  10. To validate the transformation, we are going to add an example

  11. Go to the Sample data tab.

  12. Copy and paste the following example:

    [
      {
        "id": "CD451796",
        "email": "edge@communications.com",
        "name": "Edge Communications",
        "phone": "(512) 757-6000",
        "shippingAddress": {
          "address1": "312 Constitution Place\nAustin, TX 78767\nUSA",
          "city": "Austin",
          "country": "US",
          "postalCode": "948241",
          "state": "Tx"
        },
        "billingAddress": {
          "address1": "312 Constitution Place\nAustin, TX 78767\nUSA",
          "city": "Austin",
          "country": "US",
          "postalCode": "948241",
          "state": "TX"
        }
      },
      {
        "id": "CD656092",
        "email": "burlingto@textil.com",
        "name": "Burlington Textiles Corp of America",
        "phone": "(336) 222-7000",
        "shippingAddress": {
          "address1": "525 S. Lexington Ave",
          "city": "Burlington",
          "country": "US",
          "postalCode": "27215",
          "state": "Vermont"
        },
        "billingAddress": {
          "address1": "525 S. Lexington Ave",
          "city": "Burlington",
          "country": "US",
          "postalCode": "27215",
          "state": "Vermont"
        }
      }
    ]

    The third panel will shows you how the transformation is going to be

    module4 lab2 preview transform

    Great we are consuming the Social Network API for Facebook and created and Audience.

    The next step is to create a Campaign.

Step 5: Creating the Campaign

Once we get the Audience, we need to create the campaign. First we are going to add a log to see the Create Audience response

  1. Press the module4_lab1_add_button, that is after the Social Network component, and select the logger component

    module4 lab2 logger
  2. Complete with card with the following data:

    • Message: payload. This should be set as a Dataweave expression. To do that press the f(x) button.

    • Level: Info

      module4 lab2 logger fx
  3. Close module4_lab1_close_button logger card.

  4. Add another call to the Social Network API for Facebook, but in this case select method Create Campaign

    module4 lab2 create campaign option
  5. Complete the configuration with the following information:

    • Client Id : 11def1b704e24d87a5ea5769772c90a7

    • Client Secret: 88845E529f1F42E4a5aC96Fd504c3e01

    • Content_Type : application/json

    • Accept: application/json

  6. Press the Edit link

    module4 lab2 create audience api edit
  7. Check the following information:

    • Configuration Name: Social Network API for Facebook

    • Host: social-network-api.cloudhub.io

    • Port: 80

    • Base Path: /api

    • Protocol: HTTP

      module4 lab2 create audience host conf
  8. Close module4_lab1_close_button the card.

    Again we need to transform the payload to send the post message. We will add a transform between the Logger and the last Social Network API for Facebook

  9. Press module4_lab1_add_button between the logger and the social network card

    module4 lab2 add campaign transform
  10. Select a Transform component

    module4 lab4 transform campaign panel

    You will see the input and output.

  11. Drag and Drop payload id to the audience id See the below image

    module4 lab4 transform campaign mapped

    We finished to create the Compaign.

    At the end the final flow looks like this:

    module4 lab2 final flow

That’s it. We completed the Facebook Campaing Implementation.

In the next lab we are going to test it.

Summary

In this lab we,

  • Designed the Flow

  • Added a choice

  • Created an Audience

  • Created a Campaign

  • Send an error message when no customer is found.

This lab shows how easy Design Center can be to build mulesoft APIs.

For further reading:

Congratulations! You have completed Lab 2.

Please proceed to Lab 3