Session 5: Integration and Building SOAP APIs
What's SOAP?
- Slides
- Clone Source Code:
git clone git@github.com:babaheer/soap.git
- SOAP is a platform-agnostic messaging protocol specification
- SOAP Works with XML by design
- SOAP is used when an enterprise requires tight security and clearly defined rules to support more complex data exchanges and the ability to call procedures.
- SOAP works well with processes (actions)
- Check out the Public SOAP APIs collection for sample requests you can try in Postman.
SOAP Server:
- Required Packages:
- Spyne:
python3 -m pip install spyne
- lxml:
python3 -m pip install --upgrade lxml
from spyne import Application, rpc, ServiceBase, Iterable, Integer, Unicode
from spyne.protocol.soap import Soap11
from spyne.server.wsgi import WsgiApplication
class HelloWorldService(ServiceBase):
@rpc(Unicode, Integer, _returns=Iterable(Unicode))
def say_hello(ctx, name, times):
"""Docstrings for service methods appear as documentation in the wsdl.
<b>What fun!</b>
@param name the name to say hello to
@param times the number of times to say hello
@return the completed array
"""
for i in range(times):
yield u'Tere, %s' % name
application = Application([HelloWorldService], 'spyne.examples.hello.soap',
in_protocol=Soap11(validator='lxml'),
out_protocol=Soap11())
wsgi_application = WsgiApplication(application)
if __name__ == '__main__':
import logging
from wsgiref.simple_server import make_server
logging.basicConfig(level=logging.DEBUG)
logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)
logging.info("listening to http://127.0.0.1:8000")
logging.info("wsdl is at: http://localhost:8000/?wsdl")
server = make_server('127.0.0.1', 8000, wsgi_application)
server.serve_forever()
SOAP Client (Python):
- Required Packages:
- SUDS:
python3 -m pip install suds-jurko
from suds.client import Client
client = Client('http://localhost:8000/?wsdl', cache=None)
print(client.service.say_hello(u'Maarika', 5))
wsdl = Client('http://www.learnwebservices.com/services/hello?wsdl', cache=None)
request = {'Name': 'Maarika'}
print(wsdl.service.SayHello(request))
SOAP Client (PHP):
- Download XAMPP:
https://www.apachefriends.org/download.html
- Navigate to Download Folder:
cd Downloads
- Install XAMPP:
- Change the permissions to the installer:
chmod 755 xampp-linux-*-installer.run
- Run the installer:
sudo ./xampp-linux-*-installer.run
- That's all. XAMPP is now installed below the
/opt/lampp
directory.
- Copy the below code and add it to a
.php
file and move this file to /opt/lampp/htdocs/
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
header('Content-Type: application/json');
$client = new SoapClient('http://localhost:8000/?WSDL');
$response = $client->say_hello(array('name' => 'Peeter', 'times' => '3'));
echo json_encode($response, JSON_PRETTY_PRINT);
?>