Skip to content

Commit

Permalink
Add ability to override the soap endpoint via Client.set_address()
Browse files Browse the repository at this point in the history
  • Loading branch information
mvantellingen committed Jun 1, 2016
1 parent 8645446 commit 08a757c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGES
@@ -1,3 +1,8 @@
0.8.0 (unreleased)
------------------
- Add ability to override the soap endpoint via `Client.set_address()`


0.7.1 (2016-06-01)
------------------
- Fix regression with handling wsdl:import statements for messages (#47)
Expand Down
18 changes: 18 additions & 0 deletions docs/in-depth.rst
Expand Up @@ -29,6 +29,24 @@ binding is availble via ``client.service``. To use a specific binding you can
use ``binding = client.bind('MyService', 'MyPort')``.


Overriding the default endpoint address
---------------------------------------
There are situations where you need to change the soap address from the one
which is defined within the WSDL. This can be done by using the
``Client.set_address()`` method.

.. code-block:: python
from zeep import Client
from zeep import xsd
client = Client('http://my-endpoint.com/production.svc?wsdl')
client.set_address(
'EnterpriseService', 'EnterpriseSoap11',
'http://my-endpoint.com/acceptance/')
client.service.submit('something')

This comment has been minimized.

Copy link
@shaukat72

shaukat72 Nov 14, 2016

Hi
I have the WSDL and Directory Lookup Web services urls and targetNamespace. The ObtenirUrlsServices method is part of the Directory Lookup Web services. This enables you to obtain the URLs of our various Web services.

Please any one may help me in coding. So what should be my code for this?
I want to do the following steps.

  1. How to verify the availability of the web services and authentication of user credentials.
  2. How to create SOAP Header and Body
Any objects
-----------

Expand Down
22 changes: 22 additions & 0 deletions src/zeep/client.py
Expand Up @@ -73,6 +73,28 @@ def bind(self, service_name=None, port_name=None):
port = list(service.ports.values())[0]
return ServiceProxy(self, port)

def set_address(self, service_name, port_name, address):
"""Override the default port address for the given `service_name`
`port_name` combination.
:param service_name: Name of the service
:type address: string
:param port_name: Name of the port within the service
:type address: string
:param address: URL of the server
:type address: string
"""
service = self.wsdl.services.get(service_name)
if not service:
raise ValueError("Service not found")

port = service.ports.get(port_name)
if not port:
raise ValueError("Port not found")

port.binding_options['address'] = address

def get_type(self, name):
return self.wsdl.schema.get_type(name)

Expand Down
25 changes: 25 additions & 0 deletions tests/test_client.py
Expand Up @@ -21,6 +21,31 @@ def test_bind_service_port():
service = client_obj.bind('StockQuoteService', 'StockQuotePort')
assert service

@pytest.mark.requests
def test_set_address():
client_obj = client.Client('tests/wsdl_files/soap.wsdl')
client_obj.set_address(
'StockQuoteService', 'StockQuotePort', 'http://test.python-zeep.org/x')

response = """
<?xml version="1.0"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:stoc="http://example.com/stockquote.xsd">
<soapenv:Header/>
<soapenv:Body>
<stoc:TradePrice>
<price>120.123</price>
</stoc:TradePrice>
</soapenv:Body>
</soapenv:Envelope>
""".strip()

with requests_mock.mock() as m:
m.post('http://test.python-zeep.org/x', text=response)
result = client_obj.service.GetLastTradePrice('foobar')
assert result == 120.123


@pytest.mark.requests
def test_service_proxy():
Expand Down

0 comments on commit 08a757c

Please sign in to comment.