Flow or Subflow as a function

 

Another very important aspects which we have observed is, developers duplicate their code whenever they need to repeat the same execution but with different parameters.

e.g. If you have a third party API and you are trying to build system API on to that. Developer would duplicate the same code used for one of the resource/Method in another.

But if you create your flows/subflows as a function (like any other functions in java or any language), you will be able to reap lot of benefits. The code would become simple, clean, readable , without any duplicate.

In the example below we will demonstrate using “http requestor” for our flow. In the picture below with there are three different sub flows to create, update and delete a blog.

We have just utilized only one “mulesy-api-flow” to cater to all the features for blog.

 

 

Generalize the “flow/component” as below by using variable only.

 

 

 

 

In the parent flow define a variablehttp” as below

Transformation for create flow

 

 

Transformation for update flow

 

 

This way we can create our flow or subflow as a function. We just need to change the parameter while calling the sub flow or flow. We can generalize our most of the flows/connectors using variables and the above approach.

Code Snippet of the flow :

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" 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/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
	<sub-flow name="create-blog-SubFlow" doc:id="c282e4e3-9a56-4968-9f1c-22aecf3aad34">
		<ee:transform doc:name="create-http-variable" doc:id="e0db6c3e-68d4-4fe6-88ae-cd7964ab6a34">
			<ee:message/>
			<ee:variables>
				<ee:set-variable variableName="http"><![CDATA[%dw 2.0 output application/json --- { headers: { }, uriparameters: { }, queryparameters: { }, path: "/api/v1/mulesy/blog", method: "POST", request: { "blog": { "title": payload.title, "body_html": payload.body_html, "conent": "test" } } } ]]></ee:set-variable>
			</ee:variables>
		</ee:transform>
		<logger level="INFO" doc:name="Logger" doc:id="a1d0e998-0788-4909-a5ef-19a97e9d99db" message="payload #[vars.http]"/>
		<flow-ref doc:name="call mulesy system api" doc:id="fbb9af54-2912-44c4-b839-123ac262599b" name="mulesy-api-SubFlow" target="create_blog_response"/>
	</sub-flow>
	<sub-flow name="update-blog-SubFlow" doc:id="1ede5110-f0e8-483c-80dd-a1852df09a16">
		<ee:transform doc:name="create-http-variable" doc:id="056686ae-1e0d-4e3a-97f9-bb6cfef89002">
			<ee:message/>
			<ee:variables>
				<ee:set-variable variableName="http"><![CDATA[%dw 2.0 output application/json --- { headers: { }, uriparameters: { blog_id: 112323 }, queryparameters: { }, path: "/api/v1/mulesy/blog/{blog_id}", method: "PUT", request: { "blog": { "title": payload.title, "body_html": payload.body_html, "conent": "test" } } } ]]></ee:set-variable>
			</ee:variables>
		</ee:transform>
		<logger level="INFO" doc:name="Logger" doc:id="89791e3e-d532-4036-8dd9-1258b4ea7245" message="payload #[vars.http] "/>
		<flow-ref doc:name="call mulesy system api" doc:id="b697e30c-c2d6-4ba6-bca2-c38107885587" name="mulesy-api-SubFlow" target="update_blog_response"/>
	</sub-flow>
	<sub-flow name="delete-blog-SubFlow" doc:id="5a4dfdf1-2b12-47e1-9c24-2c9be8004f12">
		<ee:transform doc:name="create-http-variable" doc:id="6664f270-a9e1-4d39-93e7-e633bfdf7f7d">
			<ee:message/>
			<ee:variables>
				<ee:set-variable variableName="http"><![CDATA[%dw 2.0 output application/json --- { headers: { }, uriparameters: { blog_id: 112323 }, queryparameters: { }, path: "/api/v1/mulesy/blog/{blog_id}", method: "DELETE", request: { } } ]]></ee:set-variable>
			</ee:variables>
		</ee:transform>
		<logger level="INFO" doc:name="Logger" doc:id="f1a68deb-39a9-49ee-87bc-37c8ea832086" message="payload #[vars.http] "/>
		<flow-ref doc:name="call mulesy system api" doc:id="3edc1b71-798c-4d38-9b57-7a8afbf7cbe2" name="mulesy-api-SubFlow" target="update_blog_response"/>
	</sub-flow>
	<sub-flow name="mulesy-api-SubFlow" doc:id="90a2dd38-ced9-4c5f-8e6c-16f224070382">
		<http:request method="#[vars.http.method]" doc:name="call mulesy api endpoint" doc:id="c3469383-6b85-402e-b5d7-5004874a0833" config-ref="HTTP_Request_configuration" path="#[vars.http.path]" sendCorrelationId="ALWAYS" responseTimeout="${http.response.timeout}">
			<http:body><![CDATA[#[vars.http.request]]]></http:body>
			<http:headers><![CDATA[#[output application/java --- vars.http.headers]]]></http:headers>
			<http:uri-params><![CDATA[#[output application/java --- vars.http.uriparameters]]]></http:uri-params>
			<http:query-params><![CDATA[#[output application/java --- vars.http.queryparameters]]]></http:query-params>
		</http:request>
	</sub-flow>
</mule>

 

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Murthy
Murthy
2 years ago

We want more examples on mule4