0

I have been trying to modify the Reply class of suds.transport.

I tried with the following approach:

import suds.transport
old_reply = suds.transport.Reply

class Reply2:
    """
    A transport reply
    @ivar code: The http code returned.
    @type code: int
    @ivar message: The message to be sent in a POST request.
    @type message: str
    @ivar headers: The http headers to be used for the request.
    @type headers: dict
    """

    def __init__(self, code, headers, message):
        """
        @param code: The http code returned.
        @type code: int
        @param headers: The http returned headers.
        @type headers: dict
        @param message: The (optional) reply message received.
        @type message: str
        """
        print 'hello, i patched the class'
        self.code = code
        self.headers = headers
        self.message = message

    def __str__(self):
        s = []
        s.append('CODE: %s' % self.code)
        s.append('HEADERS: %s' % self.headers)
        s.append('MESSAGE:')
        s.append(self.message)
        return '\n'.join(s)

suds.transport.Reply = Reply2

When executing a client request (as you would normally with Suds), the default Reply is used instead of the patched one.

What could be a reason that this approach is failing?

Note: it seems that patching the __init__ separately does give better results. But I need to modify more behaviour in the class. In the end, I'm trying to override Reply to get incoming attachments, like asked on SO and the solution here

Community
  • 1
  • 1
taper
  • 9,236
  • 5
  • 28
  • 29

1 Answers1

1

The suds.transport.http module imports Reply with the line:

from suds.transport import *

and does this before you can patch it. You need to update that reference too:

import suds.transport.http
suds.transport.http.Reply = Reply2
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Yeah, you're right! BTW: `suds.transport.https` also `import *`, so I thought about patching that too. But in my opinion that statement is unnecessary, right? None of the classes is used from `suds.transport.__init__.py`. – taper Jun 19 '13 at 07:39
  • @taper: `.https` doesn't use the `Reply` class, no; it just inherits from `.http.HttpTransport` which refers to the `Reply` it imported. – Martijn Pieters Jun 19 '13 at 08:58