Integrate Python with MuleSoft

Python 🐍

Python is a programming language that is commonly used to create websites and applications, automate operations, and perform data analysis. Python is a general-purpose programming language, which means it can be used to develop a wide range of applications and isn’t specialized for any particular problem. Because of its versatility and beginner-friendliness, it has become one of the most widely used programming languages today.

Why is Python so popular?

Although, Python is known for being slow 🐢 yet it’s very popular. Here are some reasons:

  • Easy to learn and use
    Python is a High Level Programming Language which is close to writing pseuducode in English and hence people find it bit easy
  • Libraries
    “Don’t reinvent the wheel” and so there’s hunders of useful libraries which gets job done faster
  • Community Support
    It has rich community support hosting meetups and events to clear fellow developer’s doubts.

These are just a few of many other reasons why python is trending.

Here’s a StackOverflow survey showing popular ones :
Click here for the survey insights.



Integrating python with MuleSoft Scripting Module

  • Create a new app
  • Add Scripting module as below
  • Add Jython dependency
<dependency>
  <groupId>org.python</groupId>
  <artifactId>jython-standalone</artifactId>
  <version>2.7.2</version>
</dependency>
  • Also, include jython in shared libraries
<configuration>
	<sharedLibraries>
		<sharedLibrary>
			<groupId>org.python</groupId>
			<artifactId>jython-standalone</artifactId>
		</sharedLibrary>
	</sharedLibraries>
</configuration>
  • Now we should be able to add Scripting Execute in our flow to look something as below
  • Here’s the python code that uses reuqest library to call a website and return status code
import requests
def request_website(site):
	r = requests.get(site, verify=False)
	return r.status_code

result = request_website(site)
  • Alternaitvely, we can also add reference to the script file using ${file::app.py} considering app.py is your scritp file present in src/resources
  • Files should look like below:
  • Finally we add a Transform Message to display the content to calling client
  • Now let’s see it in action using Postman

There we have a working MuleSoft application integrated with python.

Hope you liked it. Cheers 🍻

 

Leave a Reply

Your email address will not be published. Required fields are marked *