Python

Use Python to invoke web services using various techniques such as urllib2 to create a HTTP request and Suds client.

urllib2 Library

The following code uses urllib2 library to create a HTTP request to:
  1. Construct an xml payload to invoke the service. This example code passes a hard coded string.

  2. Construct a Base64-encoded string for the credentials of the service call.

  3. Create and register an opener. If the call is to a server behind a firewall, handle it through proxy.

  4. Create a request to call the service.

  5. Configure the request content type to be xml.

  6. Configure the request header with the authentication information.

  7. Set the SOAPAction to be invoked. Though the service call works without this value, it is a recommended standard.

  8. Write the xml payload to the request and execute the request.

  9. Get the response and process it. This example just prints the response.

import urllib2, base64
username='username'
password='password'

# Construct xml payload to invoke the service. In the example, it is a hard coded string.
envelope = """<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                             
                             xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                <soap:Body>
                 <findRule
                             xmlns="http://xmlns.oracle.com/apps/incentiveCompensation/cn/creditSetup/creditRule/creditRuleService/types/">
                    <findCriteria>
                        <fetchStart xmlns="http://xmlns.oracle.com/adf/svc/types/">0</fetchStart>
                        <fetchSize xmlns="http://xmlns.oracle.com/adf/svc/types/">-1</fetchSize>
                        <filter xmlns="http://xmlns.oracle.com/adf/svc/types/">
                            <group>
                               <upperCaseCompare>false</upperCaseCompare>
                               <item>
                                  <upperCaseCompare>false</upperCaseCompare>
                                  <attribute>RuleId</attribute>
                                  <operator>=</operator>
                                  <value>300000000851162</value>
                               </item>
                            </group>
                        </filter>
                        <excludeAttribute
                            xmlns="http://xmlns.oracle.com/adf/svc/types/">false</excludeAttribute>
                    </findCriteria>
                    <findControl>
                       <retrieveAllTranslations
                            xmlns="http://xmlns.oracle.com/adf/svc/types/">false</retrieveAllTranslations>
                    </findControl>
                 </findRule>
                </soap:Body>
            </soap:Envelope>"""
			
# Construct the base 64 encoded string used as credentials for the service call
creadentials = base64.encodestring('%s:%s' % (username, password))[:-1]
authorization = "Basic %s" % creadentials

# Create and register opener. Requires proxy when behind a firewall
opener = urllib2.build_opener(urllib2.HTTPHandler(), urllib2.HTTPSHandler(), urllib2.ProxyHandler({'https':'proxyhost:proxyport'}))
urllib2.install_opener(opener)

# Create request for the service call
request = urllib2.Request("https://host:port/icCnSetupCreditRulesPublicService/CreditRuleService")

# Configure the request content type to be xml
request.add_header("Content-Type", 'text/xml;charset=UTF-8')

# Configure the request authentication in the header with base64-encoded user name and password
request.add_header("Authorization", authorization)

# Set the SOAP action to be invoked; while the call works without this, the value is expected to be set based on standards
request.add_header("SOAPAction",
'http://xmlns.oracle.com/apps/incentiveCompensation/cn/creditSetup/creditRule/creditRuleService/findRule')

# Write the xml payload to the request
request.add_data(envelope)

# Execute the request
handle = urllib2.urlopen(request)

# Get the response and process it. This example just prints the response.
content = handle.read()
print content

Suds Client

Use Suds, which is based on WSDL, to build SOAP envelope and client for consuming web services. For information about installing suds, see setup tools.

The following is a sample code using the suds framework to call a service:
  1. Construct a Base64-encoded string for the credentials of the service call.

  2. Configure the request header with the content type, SOAP action, and authentication information.

  3. Create a new client based on the WSDL.

  4. Populate the objects to use as parameters.

  5. Call the service.

  6. Process the response. This example just prints the response.

import base64
from suds.client import Client

username='username'
password='password'

# Construct the base64-encoded string used as the credentials for the service call
credentials = base64.encodestring('%s:%s' % (username, password))[:-1]
authorization = "Basic %s" % credentials

url = 'https://host:port/icCnSetupCreditRulesPublicService/CreditRuleService?wsdl'

# Configure the request content type, SOAP action, and authentication in header
authenticationHeader = {
   "SOAPAction" :"http://xmlns.oracle.com/apps/incentiveCompensation/cn/creditSetup/creditRule/creditRuleService/findRule",
   "Content-Type" : "text/xml;charset=UTF-8",
   "Authorization" : authorization }
   
# Create new client
client = Client(url=url, headers=authenticationHeader)

# Populate the objects to be used as parameter
findCriteria = client.factory.create('ns4:FindCriteria')
findControl = client.factory.create('ns4:FindControl')
viewCriteria = client.factory.create('ns4:ViewCriteria')
viewCriteriaRow = client.factory.create('ns4:ViewCriteriaRow')
viewCriteriaItem = client.factory.create('ns4:ViewCriteriaItem')

viewCriteriaItem.upperCaseCompare = "false";
viewCriteriaItem.attribute = "RuleId";
viewCriteriaItem.operator = "=";
viewCriteriaItem.value = "300000000851162"

viewCriteriaRow.upperCaseCompare = "false"
viewCriteriaRow.item.append([viewCriteriaItem])
viewCriteria.group.append([viewCriteriaRow])

findCriteria.fetchStart = "0"
findCriteria.fetchSize = "-1"
findCriteria.excludeAttribute = "false"
findCriteria.filter = viewCriteria

findControl.retrieveAllTranslations = "false"

# Call the service
rule = client.service.findRule(findCriteria, findControl)

# Process the response. This example just prints the response.
print rule
Related Topics
  • Java Client
  • Oracle Database
  • .NET Framework
  • PHP
  • Perl
  • Ruby
  • Invoking SOAP Web Services