Tuesday, March 07, 2017

Calling SOAP and REST Webservice using Django 1.10, Python 35

We are using Eclipse as IDE and PyDev plugin for Django
Step 1:- Create Django project refer this site for creating Django Project
https://shdhumale.wordpress.com/2017/03/03/simple-django-hello-world-programe-using-eclipse-python-3-5-and-django-1-10/
Step 2:- Create view.py and add following code in it
import json
from django.forms import ModelForm
from django.shortcuts import render, redirect, get_object_or_404
import requests
from requests.auth import HTTPDigestAuth
import requests
import xml.etree.ElementTree as ET
import re
def SiddhuDefaultPage (request, template_name='siddhudjangowebservicecall/SiddhuCallWS_form.html'): 
return render(request, template_name)

def SiddhuSOAPCall(request, template_name='siddhudjangowebservicecall/SiddhuCallWS_form.html'):
url="http://IPADDRESS:8088/mockExposeWebServiceSoap11Binding?WSDL"
#headers = {'content-type': 'application/soap+xml'}
headers = {'content-type': 'text/xml'}
body = """YOUR SOAP REQUEST"""

response = requests.post(url,data=body,headers=headers)

print (response.content)
data = {}
data['object_list'] = parse(response.content) 
return render(request, template_name, data)
def SiddhuRestCall(request, template_name='siddhudjangowebservicecall/SiddhuCallWS_form.html'):
#if request.method == 'POST':
# Replace with the correct URL
url = "http://services.groupkt.com/country/get/iso2code/IN"
myResponse = requests.get(url)
# check api response is (OK = 200)
if(myResponse.ok):

print("----myResponse.content-------" + myResponse.text)
jData = json.loads(myResponse.content.decode('utf-8'))
print(jData)
locations = jData['RestResponse']['result']['alpha2_code']
print("locations" + locations)
data = {}
data['object_list'] = locations 

else:
myResponse.raise_for_status()
return render(request, template_name, data)

def parse(answer):
print("\nANSWER<<", answer)
try:
tree = ET.fromstring(answer)
result = {}
for item in tree.getiterator():
if item.tag in ['{http://dataobject.siddhu.com/xsd}codeReviewCoverage']:
result[item.tag] = item.text
#print ("get<<%s" % result.get('status', None))

resp1 = result.get('{http://dataobject.siddhu.com/xsd}codeReviewCoverage')
print("\resp1<<>>>>>>>>>>>>>>>>>>>>>>>", resp1)

except Exception:
print ("\nERROR <<", str("Err"))
return -1
return resp1
Step 3:- Modify url.py as below
from django.conf.urls import url
from django.contrib import admin
from siddhudjangowebservicecall import views

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.SiddhuDefaultPage),
url(r'^SOAPWS/', views.SiddhuSOAPCall, name='siddhu_soap_call'),
url(r'^RESTWS/', views.SiddhuRestCall, name='siddhu_rest_call'),
]
Step 4:- Create a template folder inside project and create another folder with name siddhudjangowebservicecall (prioject name) inside template.
Keep our html files in that folder
SiddhuCallWS_form.html:-


Folder Structure:-
Image1
UI Screen
Image2

Click on CAll REST W/S and check your url value is changed i.e. url is appended with RESTWS/and we will get ui as below
Image3
Note :- We are using REST service exposed in url = "http://services.groupkt.com/country/get/iso2code/IN"
Click on CAll SOAP W/S and check your url value is changed i.e. url is appended with SOAPWS/ and we will get ui as below
Image4

No comments: