Converting Soap UI To REST Using Flask
For the past few months I have been working on a project which required me to convert Soap UI which was using XML format to REST which was to use JSON as it had to be consumed on a mobile application.
What is SOAP
I know a lot of people are familiar with REST, me being one of them. It was not until I undertook this project that I came to hear of SOAP for the first time.
SOAP stands for Simple Object Access Protocol. It is a protocol follows a strict standard to allow communication between the client and the server.It also uses XML for exchanging information.
Many may wonder why we were using SOAP in the first place ? It was because the data which was required was coming from Microsoft Dynamics ERP called Navision and its standard was SOAP.
You can read the difference between SOAP and REST here
Why I Chose Flask
I chose flask and not any other framework such as django because first I relatively had a better understanding of It and also Flask is light weight and what I wanted was just a third party or an interface between the Navison ERP system and the mobile application and flask was up to the task.
Setting Up Flask
Flask is one of the easiest framework to setup in python
Create a virtual environment
python -m venv <myproject>
Activate the virtual environment
source myproject/bin/activate
Installing flask
pip3 install Flask
Those on python 2.7 can use pip
We also have to install ZEEP a python package which is a SOAP client and which will be doing the conversion from XML to Json
pip3 install zeep
The system was using NTLM for authentication and I know alot of “tech bros” have a lot to say about how NTLM is insecure but “it is what it is”
In order to use NTLM i installed requests_ntlm a python package which was to help me with NTLM authentication
pip3 install requests-ntlm
I also installed requests which was to help me make https requests
pip3 install requests
And we are done those are all the packages that we will require for this project.
NTLM Authentication
Import all the module in the main.py file.
from flask import Flask, request, jsonify
from zeep import Client
from zeep.transports import Transport
import requests
from requests_ntlm import HttpNtlmAuth
For authentication we are going to use requests and requests_ntlm
session = requests.Session()
session.auth = (HttpNtlmAuth(
'Username', 'Password'))
The session is to ensure that we stay logged in and don’t have to login each and every time
Setting Up The Client
We need a client which will be able to recieve the XML data for conversion
url = <url>
client = Client(url,
transport=Transport(session=session))
We provide the url to our navision system and our transport will be our session.
Making SOAP to JSON Conversion
We first need to create a flask variable
app = Flask(_name_)
We make a flask get request to the system
@app.route('/api/booklist', methods=["GET"])
def booklist():
return client.service.bookList()
The client has a service attribute. In the service attribute we will tap into the soap function or navison function bookList() which will list all books in the system as json
In some cases the navision function may have a set of parameters that should be passed into it.
@app.route('/api/book/<bookid>')
def bookdetail(bookid):
data = {
"bookId": bookid
}
return client.service.FnBookDetails(**data).return_value
In this case the bookId is a required parameter for the FnBookDetails() function.
You can also make post requests
@app.route('/api/addBook', methods=["POST"])
def returnRecieve():
req = request.args
req_dict = req.to_dict()
data = {
"bookNo": req_dict['bookNo'],
"bookName": req_dict['bookName'],
"ISBN": req_dict['ISBN'],
"publishedDate": req_dict['publishedDate'],
}
return client.service.FnReturnReceiveParts(**data).return_value
And it is that simple.