• solon
  • NEWBIE
  • 0 Points
  • Member since 2004

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hello,

I spent an interesting day trying to find out why I could not use the wsdl file with SOAPpy (or with ZSI) then tried to
use SOAPpy calling using home-grown class definitions, but could not get that to work either - because SF.com
rejected the nesting of tags thrown up by SOAPpy - which I could not figure out how to turn-off.

In the end I wrote the following example code for doing it all by hand. If you are really stuck and need to develop in Python this may help you out:

Note I am not offering to support it, if someone would like to make it "safer", "tidier" and extend the data types
please post it back.

Best of Luck
Gareth

PS if you got SOAPpy or ZSI to work let me know how, as that would be neater.



########################################################
# Upside Outcomes Ltd, July 22nd 2004
#
# Example Python WebServices Code for Salesforce.com
#
# I tried using SOAPpy and ZSI but could not
#
# a> get the SF.com WSDL file to load
# b> get SF.com to take use the code generated by SOAPpy
# which put tags around the data objects, which through up complaints.
#
# This code allows you to create a login session. To go further you will need
# create objects for request and response messages for different operations
# Use this as a template and you should go OK.
#
# Copyright (c) Upside Outcomes Ltd www.upside-outcomes.com 2004
# You are free to use, copy, modify and distribute this code without charge provided that
# you do not
# 1. claim it to be your own or
# 2. claim that you own the copyright or
# 3. prevent others from doing the same
#
# And provided you include the following message:
#
# "Some of this code contains elements written by Upside Outcomes ltd. www.upside-outcomes.com "
#
#########################################################


import sys, httplib
from xml.sax import saxutils
from xml.sax import make_parser
from xml.sax.handler import feature_namespaces
from xml.sax import ContentHandler

SFNS='"urn:enterprise.soap.sforce.com"'
SFSERVER="https://www.salesforce.com/services/Soap/c/4.0"
USER="YOURNAME"
PASS="YOURPASSWORD"



def NormaliseWhiteSpace(text):
"Remove redundant whitespace from a string"
return ' '.join(text.split())



############################# Message Receiver #######


class LoginResponse (ContentHandler):
def __init__ (self):

#Initialise State Flags
self.inEnvelope=0
self.inBody=0
self.inLoginResponse=0
self.inResult=0
self.inServerUrl=0
self.inSessionId=0
self.inUserId=0
######### Data Space"
self.namespace=""
self.serverUrl=""
self.sessionId=""
self.userId=""

def startElement (self, name, attr):
if name == 'soapenv:Envelope': self.inEnvelope=1
if name == 'soapenv:Body': self.inBody=1
if name == 'logingResponse': self.inLoginResponse=1
if name == 'result': self.inResult=1
if name == 'serverUrl': self.inServerUrl=1
if name == 'sessionId': self.inSessionId=1
if name == 'userId': self.inUserId=1

def characters (self,ch):
if self.inServerUrl: self.serverUrl +=ch
if self.inSessionId: self.sessionId +=ch
if self.inUserId: self.userId += ch

def endElement (self, name ):
if name == 'soapenv:Envelope': self.inEnvelope=0
if name == 'soapenv:Body': self.inBody=0
if name == 'logingResponse': self.inLoginResponse=0
if name == 'result': self.inResult=0
if name == 'serverUrl': self.inServerUrl=0
if name == 'sessionId': self.inSessionId=0
if name == 'userId': self.inUserId=0


################### Message sender ####################

class Request:
def __init__ (self):
self.namespace=SFNS
self.objectType="null"
self.dataTypes=[]
self.data={}

def StartWS (self):
return '''


'''
def StopWS (self):
return """


"""
def SOAPMessage (self):
outstr=self.StartWS() + "\n"
outstr+="\n"
for element in self.dataTypes:
outstr+=""+self.data[element]+"\n"
outstr += ""
outstr += self.StopWS()
return (outstr)
class LogonRequest (Request):
def __init__ (self,u="",p=""):

Request.__init__(self)

self.ObjectType="login"
self.dataTypes=["username","password"]
self.data["username"]=u
self.data["password"]=p




####################


def Logon():

########### a=LogonRequest(USER,PASS)
Soapmessage = a.SOAPMessage()
blen = len(Soapmessage)
requestor = httplib.HTTP("www.salesforce.com", 80)
requestor.putrequest("POST", SFSERVER)
requestor.putheader("Host", "www.salesforce.com")
requestor.putheader('Content-Type", "text/plain; charset=utf-8')
requestor.putheader("Content-Length", str(blen))
requestor.putheader("SOAPAction", "")
requestor.endheaders()
requestor.send(Soapmessage)

#############


(status_code, message, reply_headers) = requestor.getreply()
reply_body = requestor.getfile().read()

print "status code:", status_code
print "status message:", message

############
# Set up the XML parser
p=make_parser()
message=LoginResponse()
p.setFeature(feature_namespaces, 0)
p.setContentHandler(message)
p.feed(reply_body)
print "SessionID: " + str(message.sessionId)
print "ServerURL: " + str(message.serverUrl)
print "UserId: " + str(message.userId)



if __name__ == "__main__":
Logon()