1

I am getting an object(not defined in my code) from a SOAP request. It prints out like this:

 Print(Person)

{
    'ID': '00000000-0000-0000-0000-000000000000',
    'FIRST_NAME': 'Qwe',
    'LAST_NAME': 'Qwe',
    'MIDDLE_NAME': 'Qwe',
    'TAB_NUM': '123456',
    'ORG_ID': '321'
}

I am trying to define it like this, but it doesn't do the job.

class PersonClass:
    def __init__(self):
        self.ID = ''
        self.FIRST_NAME = ''
        self.LAST_NAME = ''
        self.MIDDLE_NAME = ''
        self.TAB_NUM = ''
        self.ORG_ID = ''

    def returnPerson(self):
        return (self)

This is a SOAP request and response:

POST /IntegrationService/IntegrationService.asmx HTTP/1.1
Host: 192.168.56.3
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://parsec.ru/Parsec3IntergationService/FindPeople"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <FindPeople xmlns="http://parsec.ru/Parsec3IntergationService">
      <sessionID>guid</sessionID>
      <lastname>string</lastname>
      <firstname>string</firstname>
      <middlename>string</middlename>
    </FindPeople>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <FindPeopleResponse xmlns="http://parsec.ru/Parsec3IntergationService">
      <FindPeopleResult>
        <Person>
          <ORG_ID>guid</ORG_ID>
        </Person>
        <Person>
          <ORG_ID>guid</ORG_ID>
        </Person>
      </FindPeopleResult>
    </FindPeopleResponse>
  </soap:Body>
</soap:Envelope>
  1. How to define a class in my code same as this object?
  2. Is there a py module for this cases?
3
  • How are you making the SOAP request? Are you using some library for that? Can you show that code too? Commented Aug 26, 2021 at 9:17
  • Person is a dictionary a built in type of object in Python. Commented Aug 26, 2021 at 9:21
  • @AKX added to a question Commented Aug 26, 2021 at 9:49

1 Answer 1

4

In case the SOAP response isn't fixed and you want the class to be generic enough, here are some solutions:

Solution 1

Raw class

soap_raw = {
    'ID': '00000000-0000-0000-0000-000000000000',
    'FIRST_NAME': 'Qwe',
    'LAST_NAME': 'Qwe',
    'MIDDLE_NAME': 'Qwe',
    'TAB_NUM': '123456',
    'ORG_ID': '321'
}


class SoapObject:
    def __init__(self, kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)

soap_object = SoapObject(soap_raw)

print(f"{soap_object.ID=}")
print(f"{soap_object.FIRST_NAME=}")
print(f"{soap_object.LAST_NAME=}")
print(f"{soap_object.MIDDLE_NAME=}")
print(f"{soap_object.TAB_NUM=}")
print(f"{soap_object.ORG_ID=}")

print(f"Whole object: {soap_object.__dict__=}")

Solution 2

Using dataclasses.make_dataclass

from dataclasses import make_dataclass
...
SoapObject = make_dataclass('SoapObject', soap_raw.keys())
soap_object = SoapObject(**soap_raw)
...

Solution 3

The simplest and probably the best one. Thanks to @AKX for this idea! (See comments). Use types.SimpleNamespace.

from types import SimpleNamespace
...
soap_object = SimpleNamespace(**soap_raw)
...

Output (applies to all 3 solutions)

soap_object.ID='00000000-0000-0000-0000-000000000000'
soap_object.FIRST_NAME='Qwe'
soap_object.LAST_NAME='Qwe'
soap_object.MIDDLE_NAME='Qwe'
soap_object.TAB_NUM='123456'
soap_object.ORG_ID='321'
Whole object: soap_object.__dict__={'ID': '00000000-0000-0000-0000-000000000000', 'FIRST_NAME': 'Qwe', 'LAST_NAME': 'Qwe', 'MIDDLE_NAME': 'Qwe', 'TAB_NUM': '123456', 'ORG_ID': '321'}
Sign up to request clarification or add additional context in comments.

3 Comments

types.SimpleNamespace does the same thing.
Ohh okay so there's a builtin for this. I tried and it also worked, so it's better. Good to know, thank you for this info! :)
I updated the answer to show your idea as it seems the best option :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.