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.

    module4 lab2 press add after customer
  2. Scroll down 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.
  3. Click inside the 'Expression (required)' data entry field.

    module4 lab2 select expression
  4. Enter expression payload != null. This is a DataWeave expression. Press the Ok button to save.

    module4 lab2 select enter expression
  5. Enter the Display Name If Not Empty

    module4 lab2 choice complete

    You are creating an if then else statement.

    Now let’s continue creating the statement by adding a message to 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

    module4 lab2 add transform
  3. Click on the script tab

    module4 lab2 default transform script
  4. Replace the value null with "No Customers Found", ensuring you enter the double quotes to indicate this is a string literal.

    module4 lab2 no cusotmer found
  5. Close the card by pressing the X in the top right corner.

    module4 lab2 close transform

Step 4: Create an Audience

  1. Click on the module4_lab1_add_button next to the If statement that has a red dot.

    module4 lab2 choice select then

    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 and select Social Network API for Facebook.

    module4 lab2 social network filter
  3. 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

      As we did in the Customer API card, we need to configure a Connector Name and Type.

  7. Press the Add Connection button.

  8. Complete with a name and choose Connection in the Connection Type. We are not going to share the connection, so uncheck the option.

    module4 lab2 save social connector
  9. Press the Test button and ensure a module4_lab1_test_successful message is returned.

  10. Press the Save button after a successful test.

  11. Close the card by pressing the X in the top right corner.

    module4 lab2 close audience api
    According to the API RAML Specification we need to adapt the customer information to what the API needs.
  12. If you hover 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
  13. Select the Transform component.

    module4 lab2 select transform

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

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

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

    • The third panel shows you how the mapping ends.

  14. Click on the script tab

    You can drag and drop the fields from the input to the output and the mapping will be generated for you
  15. 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: []
      })
    }
    module4 lab2 transform script paste
    We are going to add sample data to this transform so that we can see if our transform is correct in realtime
  16. Go to the Sample data tab.

    module4 lab2 transform sample data paste
  17. 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 will work and will respond in real time as you modify the data or the transformation.

    module4 lab2 preview transform
  18. Close the card by pressing the X in the top right corner.

    module4 lab2 close transform sample data

    Great! We are now consuming the Social Network API for Facebook and have created an 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.

    module4 lab2 add logger
  2. Select the logger component.

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

    • Message: payload. This sets the entire payload to be logged.

    • Level: Info (which is the default)

      module4 lab2 logger fx
  4. Close module4_lab1_close_button logger card.

    We will now add another call to the Social Network API for Facebook, but in this case select method Create Campaign
  5. Press the module4_lab1_add_button, that is after the Logger component you just added.

    module4 lab2 add campaign
  6. Search for Social Network on the select component panel and select Social Network API for Facebook.

    module4 lab2 social network filter

    Select Create Campaign from the list.

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

    • Client Id : 11def1b704e24d87a5ea5769772c90a7

    • Client Secret: 88845E529f1F42E4a5aC96Fd504c3e01

    • Content_Type : application/json

    • Accept: application/json

      module4 lab2 configure campaign
  8. Press the Edit link.

    module4 lab2 create campaign api edit
  9. Verify 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
  10. Close module4_lab1_close_button the card.

    Once again we need to transform the payload before sending the post message. To accomplish this, we will add a transform between the Logger and the last Social Network API for Facebook
  11. Press module4_lab1_add_button between the Logger Card and the Social Network Create Campaign card

    module4 lab2 add campaign transform
  12. Select the Transform component.

    module4 lab2 select transform
  13. You will see the following. Note the input and output panes.

    module4 lab4 transform campaign panel
  14. Open the Audience structure withing the Output payload by clicking on the arrow.

    module4 lab4 transform campaign panel audience
  15. Drag and Drop payload id to the audience id See the below image

    module4 lab4 transform campaign mapped
    Since we have not entered sample data, the preview shows 'null' for the id
  16. Close module4_lab1_close_button the card.

  17. Verify your final flow looks like this:

    module4 lab2 final flow

    Great! You have finished the call to Create Campaign.

Congratulations! You have completed the Facebook Campaign Implementation.

In the next lab we are going to test it!

Summary

In this lab we,

  • Designed a Flow

  • Added a choice

  • Created an Audience

  • Created a Campaign

  • Sent an error message when no customer is found.

This lab demonstrates how easy Design Center can be to build MuleSoft APIs.

For further reading:

Congratulations! You have completed Lab 2.

Please proceed to Lab 3