Preventing method duplication

Viewed 124

I created a class for SOAP requests. I saw then that I'm doing same thing so I was writing the same methods with different logic and values. A similar example as below:

class Client(object):

    def get_identity(self, identity):
        context = {'identity': identity}
        return self.post_request('get_identity', context)

    def get_degree(self, degree, date):
        context = {
            'degree': degree,
            'date': date
        }
        return self.post_request('get_degree', context)

    def send_response(self, status, degree):
        context = {
            'degree': degree,
            'status': status,
            'reason': 'Lorem ipsum dolor sit amet.'
        }
        return self.post_request('send_response', context)

    def post_request(self, method, args):
        headers = "Creating headers"
        data = "Mapping hear with method and args arguments"

        response = requests.post(self.wsdl, data=data.encode('UTF-8'), headers=headers)

        root = objectify.fromstring(response.content)
        objectify.deannotate(root, cleanup_namespaces=True)
        return root

Then whichever I'm invoking a method:

client = Client()
self.client.get_identity(identity)

When I want to implement new SOAP method for example I'll write this:

def get_status(self, id):
    context = {'id': id}
    return self.post_request('get_status', context)

As I mentioned above my aim is to prevent this duplication and I have no idea how to do that.

3 Answers
Related