lab 1: discover and reuse order fulfillment api ... -...

8
Lab 1 Discover and Reuse Order Fulfillment API from Exchange 2.0

Upload: others

Post on 26-May-2020

19 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lab 1: Discover and Reuse Order Fulfillment API ... - Mulemule4.workshop.tools.mulesoft.com/pdf/module10_lab1.pdfStep 1: Create a new Mule Project and import RAML Definition We are

Lab 1Discover and Reuse Order Fulfillment API from

Exchange 2.0

Page 2: Lab 1: Discover and Reuse Order Fulfillment API ... - Mulemule4.workshop.tools.mulesoft.com/pdf/module10_lab1.pdfStep 1: Create a new Mule Project and import RAML Definition We are

OverviewIn this lab we will create a new Mule application in Anypoint Studio from the Order Fulfillment APIRAML Definition stored in the Anypoint Exchange Design Center. This will be the implementationof our REST API.

Step 1: Create a new Mule Project andimport RAML DefinitionWe are going to use our API definition to create an implementation of our Order Fulfillment API. Todo this we need to import the RAML Specification from the Design Center

1. If it’s not already opened, start Anypoint Studio from the desktop icon and select the sameworkspace you’ve been working (example: C:\workspaces\myworkspace).

2. From Anypoint Studio’s menu, select File > New > Mule Project to create a New Mule project. AWindow will pop-up to define the details of this new application.

3. Give the project the name api-order-fulfillment

4. Select the Mule Server 4.1.4 EE.

5. Under API Implementation settings, check Specify API definition file location or URL andchoose Design Center… by clicking in the (…) button on the right of the Location textbox.

6. Once you clicked there, it should take you to the Browse Design Center for APIs window, if itasks you to login, use your Anypoint Platform account.

7. Select the correct Business Group.

8. This is the same you did in the last module. You will see a list with all the APIs definitions thatwere created. Select Order Fulfillment API and press OK

1

Page 3: Lab 1: Discover and Reuse Order Fulfillment API ... - Mulemule4.workshop.tools.mulesoft.com/pdf/module10_lab1.pdfStep 1: Create a new Mule Project and import RAML Definition We are

WARNINGIf you don’t see the Order Fulfillment API, press Load more projects.This link only appears when there are too many projects on the APIDesigner.

9. Press Finish. This will create a skeleton project that implements your API.

10. The import will create a new Anypoint Studio application with a preconfigured implementationbased on your API metadata like the following

Let’s explore what was automatically generated.

At the top of the diagram you will see a flow called order_fulfillment-api-main along with aplaceholder flow of the Process API namely: post:/order_fulfillment:application/json:order-fulfillment-api-config. This Process API is a reflection of the POST method defined in the RAMLsourced from Exchange 2.0. (There will also be an api-product-console flow, as well as a globalexception handling block.)

Typically, there will be flows that look like this get:/resource, post:/resource, put:/resource, etc.Note that the name of the flow is very important for the APIkit router to be able to route therequest to the appropriate flow - you don’t want to change these.

2

Page 4: Lab 1: Discover and Reuse Order Fulfillment API ... - Mulemule4.workshop.tools.mulesoft.com/pdf/module10_lab1.pdfStep 1: Create a new Mule Project and import RAML Definition We are

When APIkit detects example data in the response of a method in the RAML it inserts a SetPayload Transformer into the flow which returns the static response specified by the exampledata reference.

The static response returned by the auto-generated Set Payload Transformer allows you to totest the stub immediately after generation. These flows can be enhanced to provide moreadvanced mock services as well as evolve into the full API implementation.

api-product-main

This is the main flow. It exposes an HTTPservice and processes the requests using theAPIKit Router.

The HTTP request will be converted to a mulemessage, and redirected to the requested flowby the APIKit router.

If you take a look at the HTTP configuration,you will see that its listening for requests onhttp://localhost:8081/api

3

Page 5: Lab 1: Discover and Reuse Order Fulfillment API ... - Mulemule4.workshop.tools.mulesoft.com/pdf/module10_lab1.pdfStep 1: Create a new Mule Project and import RAML Definition We are

post:/order_fulfillment:application/json:order-fulfillment-api-config

This is the skeleton flow that we’ll implementin the next step. By quickly laying down thisskeleton flow or stub, developers andconsumers alike can quickly test this flowrunning as a Mule application. The staticresponse returned by the auto-generatedallows one to test the stub immediately aftergeneration. Obviously these flows can beenhanced to provide more advanced mockservices as well as evolve into the full APIimplementation. The evolution of API asset is anatural byproduct of typical Agiledevelopment practices adopted by manyMulesoft customers.

Step 2: Run the API1. To test the API, let’s run it within Studio first. Right click api-order-fulfillment in the Package

Explorer view.

2. Select Run As > Mule Application (Feel free to skip this if you’ve done this in the previous lab).

3. The application will start running, and the console will show the Mule Runtime logs. Wait untilthe console shows the application has completed deployment.

4. Once the application is deployed, Anypoint Studio will open a tab with a link to the API Console

4

Page 6: Lab 1: Discover and Reuse Order Fulfillment API ... - Mulemule4.workshop.tools.mulesoft.com/pdf/module10_lab1.pdfStep 1: Create a new Mule Project and import RAML Definition We are

where we can test the application (to maximize the console just double-click the APIKit Consoletab)

5. Click on it and a new page will open on your custom browser.

6. Click the POST tab on the left to test the order_fulfillment method.

5

Page 7: Lab 1: Discover and Reuse Order Fulfillment API ... - Mulemule4.workshop.tools.mulesoft.com/pdf/module10_lab1.pdfStep 1: Create a new Mule Project and import RAML Definition We are

7. Press Try it button. You will see a Console to test the API.

8. Go to the Body tab and check the request body.

9. Click the SEND button.

10. You should see the sample the response from the customer creation.

6

Page 8: Lab 1: Discover and Reuse Order Fulfillment API ... - Mulemule4.workshop.tools.mulesoft.com/pdf/module10_lab1.pdfStep 1: Create a new Mule Project and import RAML Definition We are

11. Go to the console tab and press the red button to stop the Mule runtime server.

In the next lab we will update the flows to connect to the database.

SummaryIn this lab, we

• Import the RAML Definition directly into Anypoint Studio

• Auto-generated a skeleton project to implement the API

• Run the skeleton to check that is correct.

Go Further:

• See the link APIkit doc for more information.

Congratulations! You have completed Lab 1.

Please proceed to Lab 2

Take me to the TOP

7