by Vijayalaxmi Cigala



- “Mule” is a runtime engine
- Anypoint Studio is the IDE in which to create applications
- Design Center is an application where APIs can be designed
#%RAML 1.0 title: WWE RAW Apis version: v1.0 securitySchemes: basic: description: | This API supports Basic Authentication. type: Basic Authentication securedBy: [basic] traits: hasHeaders: headers: Authorization: displayName: Authorization description: Basic authentication base 64 encoded string type: string required: true Content-type: displayName: Content-type description: json type: string required: true types: wrestler: properties: first_name : string last_name : string weight_lbs : integer current_champion : boolean /raw/wrestlers/champion: is: ["hasHeaders"] description: api for fetching current champion get: responses: 200: body: application/json: type: wrestler 404: body: application/json: properties: message: string example: | "message" : "No don't have a champ yet!"2. Generate an API using RAML: We create a Mule project in Anypoint Studio and copy the RAML we designed above into the project. We then generate a “flow” from the RAML. This “flow” represents our API with whatever information our RAML provided such as path, payload, body, query parameters, etc. if any. It will be an empty flow that needs to be completed as shown below by drag and drop of the components from Mule Palette into the IDE.

<?xml version="1.0" encoding="UTF-8"?> <mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:apikit="http://www.mulesoft.org/schema/mule/mule-apikit" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:mongo="http://www.mulesoft.org/schema/mule/mongo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/mule-apikit http://www.mulesoft.org/schema/mule/mule-apikit/current/mule-apikit.xsd http://www.mulesoft.org/schema/mule/mongo http://www.mulesoft.org/schema/mule/mongo/current/mule-mongo.xsd http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd"> <configuration-properties file="app.properties"/> <http:listener-config name="wwe_raw_apis-httpListenerConfig"> <http:listener-connection host="${app.hostname}" port="${app.port}" /> </http:listener-config> <apikit:config name="wwe_raw_apis-config" raml="wwe_raw_apis.raml" outboundHeadersMapName="outboundHeaders" httpStatusVarName="httpStatus" /> <mongo:config name="MongoDB_Config" doc:name="MongoDB Config" doc:id="fa250676-40f4-499d-8c78-c7d1bc609f3b"> <mongo:connection username="${db.username}" password="${db.password}" database="${db.name}" /> </mongo:config> <apikit:config outboundHeadersMapName="outboundHeadersMapName" httpStatusVarName="httpStatus" doc:name="Router" doc:id="b268a874-716a-4b9f-ab60-a6e4e0bb4c6a" name="Router" raml="wwe_raw_apis.raml" /> <flow name="wwe_raw_apis-main"> <http:listener config-ref="wwe_raw_apis-httpListenerConfig" path="/api/*"> <http:response statusCode="#[vars.httpStatus default 200]"> <http:headers>#[vars.outboundHeaders default {}]</http:headers> </http:response> <http:error-response statusCode="#[vars.httpStatus default 500]"> <http:body>#[payload]</http:body> <http:headers>#[vars.outboundHeaders default {}]</http:headers> </http:error-response> </http:listener> <apikit:router config-ref="Router" /> <error-handler> <on-error-propagate type="APIKIT:BAD_REQUEST"> <ee:transform xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd"> <ee:message> <ee:set-payload><![CDATA[%dw 2.0 output application/json --- {message: "Bad request"}]]></ee:set-payload> </ee:message> <ee:variables> <ee:set-variable variableName="httpStatus">400</ee:set-variable> </ee:variables> </ee:transform> </on-error-propagate> <on-error-propagate type="APIKIT:NOT_FOUND"> <ee:transform xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd" doc:id="4f5e1524-c26f-4885-b0c2-97871c0db96b"> <ee:message> <ee:set-payload><![CDATA[%dw 2.0 output application/json --- {message: "Resource not found"}]]></ee:set-payload> </ee:message> <ee:variables> <ee:set-variable variableName="httpStatus"><![CDATA[404]]></ee:set-variable> </ee:variables> </ee:transform> </on-error-propagate> <on-error-propagate type="APIKIT:METHOD_NOT_ALLOWED"> <ee:transform xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd" doc:id="2c47904e-ba10-4909-872c-6deee0f3b86b"> <ee:message> <ee:set-payload><![CDATA[%dw 2.0 output application/json --- {message: "Method not allowed"}]]></ee:set-payload> </ee:message> <ee:variables> <ee:set-variable variableName="httpStatus"><![CDATA[405]]></ee:set-variable> </ee:variables> </ee:transform> </on-error-propagate> <on-error-propagate type="APIKIT:NOT_ACCEPTABLE"> <ee:transform xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd" doc:id="531f1c92-02e4-415d-b09c-95c612d3b6f6"> <ee:message> <ee:set-payload><![CDATA[%dw 2.0 output application/json --- {message: "Not acceptable"}]]></ee:set-payload> </ee:message> <ee:variables> <ee:set-variable variableName="httpStatus"><![CDATA[406]]></ee:set-variable> </ee:variables> </ee:transform> </on-error-propagate> <on-error-propagate type="APIKIT:UNSUPPORTED_MEDIA_TYPE"> <ee:transform xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd"> <ee:message> <ee:set-payload><![CDATA[%dw 2.0 output application/json --- {message: "Unsupported media type"}]]></ee:set-payload> </ee:message> <ee:variables> <ee:set-variable variableName="httpStatus">415</ee:set-variable> </ee:variables> </ee:transform> </on-error-propagate> <on-error-propagate type="APIKIT:NOT_IMPLEMENTED"> <ee:transform xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd"> <ee:message> <ee:set-payload><![CDATA[%dw 2.0 output application/json --- {message: "Not Implemented"}]]></ee:set-payload> </ee:message> <ee:variables> <ee:set-variable variableName="httpStatus">501</ee:set-variable> </ee:variables> </ee:transform> </on-error-propagate> </error-handler> </flow> <flow name="wwe_raw_apis-console"> <http:listener config-ref="wwe_raw_apis-httpListenerConfig" path="/console/*"> <http:response statusCode="#[vars.httpStatus default 200]"> <http:headers>#[vars.outboundHeaders default {}]</http:headers> </http:response> <http:error-response statusCode="#[vars.httpStatus default 500]"> <http:body>#[payload]</http:body> <http:headers>#[vars.outboundHeaders default {}]</http:headers> </http:error-response> </http:listener> <apikit:console config-ref="wwe_raw_apis-config" /> <error-handler> <on-error-propagate type="APIKIT:NOT_FOUND"> <ee:transform xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd" doc:id="444222a1-ff70-48a8-b118-d939dac3afa0"> <ee:message> <ee:set-payload><![CDATA[%dw 2.0 output application/json --- {message: "Resource not found"}]]></ee:set-payload> </ee:message> <ee:variables> <ee:set-variable variableName="httpStatus"><![CDATA[404]]></ee:set-variable> </ee:variables> </ee:transform> </on-error-propagate> </error-handler> </flow> <flow name="get:rawwrestlerschampion:Router"> <logger level="INFO" message="get:rawwrestlerschampion:Router" /> <ee:transform doc:name="Create query" doc:id="24144e23-51a6-4c09-9d82-ebb7222d9862" > <ee:message /> <ee:variables > <ee:set-variable variableName="query" ><![CDATA[%dw 2.0 output application/json --- { "current_champion" : true }]]></ee:set-variable> </ee:variables> </ee:transform> <mongo:find-one-document collectionName="wrestlers" doc:name="Execute query" doc:id="b027e174-f9b9-4827-a66c-37b083f873cd" config-ref="MongoDB_Config" returnId="false" > <mongo:find-query ><![CDATA[#[vars.query]]]></mongo:find-query> </mongo:find-one-document> <set-payload value="#[payload]" doc:name="Set Payload" doc:id="2cf2cf6a-9b03-4c5b-b136-fe95ff1b98e8" /> </flow> </mule>We run the project, and this should start our application at a URL configured in the configuration XML with values such as ${app.hostname}, ${app.port}, context (/api/) and flow name - get:rawwrestlerschampion. Here “get” in the flow name serves as the HTTP verb. Time to taste our recipe. It better be sweet!

