-1

I am making a SOAP call via Zeep and utilizing the plugin based on the documentation located here. The class is defined as a variable plugin which is then sent in the Client. When I run resp = service.listChange() it will run the class ResponseHeader which prints the following:

Headers:
{'Set-Cookie': 'JSESSIONIDSSO=F8C6115A5588CD010C4A646A0751C2C3; Path=/; Secure; HttpOnly, JSESSIONID=B8B5AB8ACB982C021F1E94DC23F12A6A; Path=/axl; Secure; HttpOnly',
'X-Frame-Options': 'SAMEORIGIN', 'Strict-Transport-Security': 'max-age=31536000; includeSubdomains', 'Content-Security-Policy': "default-src *; script-src * 'unsafe-inline' 'unsafe-eval';style-src * 'unsafe-inline'; img-src * data: 'unsafe-inline';", 'X-Content-Type-Options': 'nosniff', 'X-XSS-Protection': '1; mode=block', 'Content-Type': 'text/xml;charset=UTF-8', 'Transfer-Encoding': 'chunked', 'Date': 'Thu, 22 Jun 2023 12:36:07 GMT', 'Server': ''}

Instead of printing I need to be able to access the http_headers variable that is returned from service.listChange()

    class ResponseHeader( Plugin ):
            def ingress( self, envelope, http_headers, operation ):
                print( f'\nResponse\n-------\nHeaders:\n{http_headers}\n' )
    

    session = Session()
    session.verify = False
    urllib3.disable_warnings( urllib3.exceptions.InsecureRequestWarning
    session.auth = HTTPBasicAuth(username, password)
    transport = Transport( session = session, timeout = 10 )
    settings = Settings(xml_huge_tree = True )
    plugin = [ ResponseHeader() ] if DEBUG else []
    client = Client( WSDL_FILE, settings = settings, transport = transport, plugins = plugin )
    service = client.create_service( '{http://www.cisco.com/AXLAPIService/}AXLAPIBinding',
                                    'https://server:8443/axl/' )

    try:

        resp = service.listChange()
        header = ResponseHeader()
        print(header)

    except Exception as err:
        logging.error( f'\nZeep error: initial listChange: { err }' )
        sys.exit( 1 )

I have also tried defining `http_headers` outside of the class which did not work either. 
Chad
  • 83
  • 1
  • 7
  • The values will be passed into the function by whatever is calling the function. They only exist at that time inside the function. You cannot get them by calling `ingress` yourself, because you'd then have to pass the values in as arguments, which is obviously pointless (and you're not _calling_ `ingress` to begin with). You'll need to describe in more detail what you're trying to do, so one may propose a sensible approach. – deceze Jun 22 '23 at 11:31
  • @deceze - edits made. Hopefully that provides clarity – Chad Jun 22 '23 at 14:58

1 Answers1

0

What you want is to pass an instance of your plugin, keep a handle on that instance, let the plugin save data to some attribute on itself, and later access that saved data from your plugin instance. Something along these lines:

class ResponseHeader(Plugin):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.headers = []

    def ingress(self, envelope, http_headers, operation):
        self.headers.append(http_headers)


...

response_header = ResponseHeader()
plugin = [response_header] if DEBUG else []
client = Client(WSDL_FILE, settings=settings, transport=transport, plugins=plugin)

...

print(response_header.headers)
deceze
  • 510,633
  • 85
  • 743
  • 889
  • Thanks @deceze. To clarify, this is needed due to the fact the information needed is nested in a function? Or does this have to do with how Zeep handles the returned information? – Chad Jun 23 '23 at 14:33
  • I have no idea about Zeep. There may be some completely different mechanism there by which you could get your desired data. But given the plugin approach you're using, this would be the seemingly correct way to go about it. – deceze Jun 23 '23 at 14:47