braintree / braintree_python

Braintree Python library
https://developer.paypal.com/braintree/docs/start/overview
MIT License
242 stars 115 forks source link

Extract dictionnary from object #56

Closed PhilipGarnero closed 9 years ago

PhilipGarnero commented 9 years ago

It would be great if we could get the dictionnary representing a braintree object. We could extract data or send some objects directly to the clients like plans for example.

I managed to do this by adding this small function to your AttributeGetter but it's just monkey patching :

def serialize(self):
    data = {}
    for key in self._setattrs:
        if hasattr(getattr(self, key), "serialize"):
            data[key] = getattr(self, key).serialize()
        else:
            data[key] = getattr(self, key)
    return data

If I want to get the dictionnary from an object, I'll be able to do my_object.serialize()

agfor commented 9 years ago

@PhilipGarnero You can do this kind of thing without monkey patching (untested):

def braintree_object_tree_to_dict(braintree_object):
    data = braintree_object.__dict__.copy()
    for key, value in data.items():
        if isinstance(value, braintree.AttributeGetter):
            data[key] = braintree_object_tree_to_dict(value)
    return data

Since it's pretty straightforward and we haven't had requests for this before, I'm going to close the issue.

TimotheeJeannin commented 7 years ago

@agfor It would be great to have that feature added directly in the braintree python client. There is several situations where this could be useful. Passing customer information to another micro-service, storing customer information in a nosql database and update it via webhooks, ...