You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

83 lines
3.0 KiB

  1. # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
  2. #
  3. # This software is subject to the provisions of the Zope Public License,
  4. # Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
  5. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  6. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  7. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  8. # FOR A PARTICULAR PURPOSE.
  9. from ZSI import *
  10. from ZSI.client import *
  11. import WSDLTools
  12. from urlparse import urlparse
  13. import weakref
  14. class ServiceProxy:
  15. """A ServiceProxy provides a convenient way to call a remote web
  16. service that is described with WSDL. The proxy exposes methods
  17. that reflect the methods of the remote web service."""
  18. def __init__(self, wsdl, service=None, port=None, tracefile=None,
  19. typesmodule=None, nsdict=None):
  20. if not hasattr(wsdl, 'targetNamespace'):
  21. wsdl = WSDLTools.WSDLReader().loadFromURL(wsdl)
  22. # for item in wsdl.types.items():
  23. # self._serializer.loadSchema(item)
  24. self._service = wsdl.services[service or 0]
  25. self.__doc__ = self._service.documentation
  26. self._port = self._service.ports[port or 0]
  27. self._name = self._service.name
  28. self._wsdl = wsdl
  29. self._tracefile = tracefile
  30. self._typesmodule = typesmodule
  31. self._nsdict = nsdict
  32. binding = self._port.getBinding()
  33. portType = binding.getPortType()
  34. for item in portType.operations:
  35. callinfo = callInfoFromWSDL(self._port, item.name)
  36. method = MethodProxy(self, callinfo)
  37. setattr(self, item.name, method)
  38. def _call(self, name, *args, **kwargs):
  39. """Call the named remote web service method."""
  40. if len(args) and len(kwargs):
  41. raise TypeError(
  42. 'Use positional or keyword argument only.'
  43. )
  44. callinfo = getattr(self, name).callinfo
  45. url = callinfo.location
  46. (protocol, host, uri, query, fragment, identifier) = urlparse(url)
  47. port = 80
  48. if host.find(':') >= 0:
  49. host, port = host.split(':')
  50. params = callinfo.getInParameters()
  51. host = str(host)
  52. port = str(port)
  53. binding = Binding(host=host, tracefile=self._tracefile,
  54. ssl=(protocol == 'https'),
  55. port=port, url=uri, typesmodule=self._typesmodule,
  56. nsdict=self._nsdict)
  57. apply(getattr(binding, callinfo.methodName), args)
  58. #print binding.ReceiveRaw()
  59. return binding.Receive()
  60. class MethodProxy:
  61. """ """
  62. def __init__(self, parent, callinfo):
  63. self.__name__ = callinfo.methodName
  64. self.__doc__ = callinfo.documentation
  65. self.callinfo = callinfo
  66. self.parent = weakref.ref(parent)
  67. def __call__(self, *args, **kwargs):
  68. return self.parent()._call(self.__name__, *args, **kwargs)