apply jespern patch to support cookies; rename some of the private Client methods (no reason for them to be private); change access of urlparse result.scheme with [0] to be compatible with < 2.5; rename the release 0.2 :-D

This commit is contained in:
jortel 2008-04-29 14:50:10 +00:00
parent f04e6757fa
commit 704d13b9cd
3 changed files with 55 additions and 18 deletions

2
README
View File

@ -373,7 +373,7 @@ version-0.1.7 (04-08-08):
for lots of collaboration on #suds.
version-0.1.8 (04-28-08):
version-0.2 (04-28-08):
* Contains the first cut at the rpc/encoded soap style.

View File

@ -15,7 +15,7 @@
import logging
VERSION = "0.1.8"
VERSION = "0.2"
#
# Exceptions

View File

@ -13,6 +13,7 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# written by: Jeff Ortel ( jortel@redhat.com )
from cookielib import CookieJar
from urllib2 import Request, urlopen, urlparse, HTTPError
from suds import *
from suds.sudsobject import Object
@ -171,6 +172,8 @@ class Client:
@type schema: L{suds.schema.Schema}
@ivar builder: A builder object used to build schema types.
@type builder: L{Builder}
@ivar cookiejar: A cookie jar.
@type cookiejar: libcookie.CookieJar
"""
def __init__(self, url, **kwargs):
@ -193,6 +196,7 @@ class Client:
self.wsdl = WSDL(url)
self.schema = self.wsdl.get_schema()
self.builder = Builder(self.schema)
self.cookiejar = CookieJar()
self.log = logger('serviceproxy')
def send(self, method, *args):
@ -205,38 +209,61 @@ class Client:
"""
result = None
binding = self.wsdl.get_binding(method.name, **self.kwargs)
headers = self.__headers(method.name)
headers = self.headers(method.name)
location = self.wsdl.get_location().encode('utf-8')
msg = binding.get_message(method.name, *args)
self.log.debug('sending to (%s)\nmessage:\n%s', location, msg)
try:
request = Request(location, msg, headers)
self.__set_proxies(location, request)
self.cookiejar.add_cookie_header(request)
self.set_proxies(location, request)
fp = urlopen(request)
self.cookiejar.extract_cookies(fp, request)
reply = fp.read()
result = self.__succeeded(binding, method, reply)
result = self.succeeded(binding, method, reply)
except HTTPError, e:
result = self.__failed(binding, method, e)
result = self.failed(binding, method, e)
return result
def __set_proxies(self, location, request):
"""Set the proxies for the request. """
def set_proxies(self, location, request):
"""
Set the proxies for the request.
@param location: A URL location of the service method
@type location: str
@param request: A soap request object to be sent.
@type request: urllib2.Request
"""
proxies = self.kwargs.get('proxy', {})
protocol = urlparse.urlparse(location).scheme
protocol = urlparse.urlparse(location)[0]
proxy = proxies.get(protocol, None)
if proxy is not None:
self.log.info('proxy %s used for %s', proxy, location)
request.set_proxy(proxy, protocol)
def __headers(self, method):
""" get http headers """
def headers(self, method):
"""
Get http headers or the http/https request.
@param method: The B{name} of method being invoked.
@type method: str
@return: A dictionary of header/values.
@rtype: dict
"""
action = self.wsdl.get_soap_action(method)
return \
{ 'SOAPAction': action,
'Content-Type' : 'text/xml' }
def __succeeded(self, binding, method, reply):
""" request succeeded, process reply """
def succeeded(self, binding, method, reply):
"""
Request succeeded, process the reply
@param binding: The binding to be used to process the reply.
@type binding: L{Binding}
@param method: The service method that was invoked.
@type method: L{Method}
@return: The method result.
@rtype: I{builtin}, L{Object}
@raise WebFault: On server.
"""
self.log.debug('http succeeded:\n%s', reply)
if len(reply) > 0:
p = binding.get_reply(method.name, reply)
@ -247,8 +274,16 @@ class Client:
else:
return (200, None)
def __failed(self, binding, method, error):
""" request failed, process reply based on reason """
def failed(self, binding, method, error):
"""
Request failed, process reply based on reason
@param binding: The binding to be used to process the reply.
@type binding: L{Binding}
@param method: The service method that was invoked.
@type method: L{Method}
@param error: The http error message
@type error: urllib2.HTTPException
"""
status, reason = (error.code, error.msg)
reply = error.fp.read()
self.log.debug('http failed:\n%s', reply)
@ -262,8 +297,10 @@ class Client:
else:
return (status, None)
def __get_methods(self):
"""get a list of methods provided by this service"""
def get_methods(self):
"""
Get a list of methods provided by this service
"""
list = []
for op in self.wsdl.get_operations():
method = op.attribute('name')
@ -286,7 +323,7 @@ class Client:
for p in prefixes:
s += '\t\t%s = "%s"\n' % p
s += '\n\tmethods:\n'
for m in self.__get_methods():
for m in self.get_methods():
s += '\t\t%s\n' % m
return unicode(s)
except Exception, e: