Commits

Anonymous committed aa7a768

Fix issue when converting a product into dict.

Comments (0)

Files changed (1)

src/main/python/magazineluiza/api/managers/base.py

                 else:
                     logger.debug('Erro ao fazer request para o servidor %s. Tentando outro da lista. Tipo do erro = %s' % (server_to_try, type(e)))
 
-
     def _get(self, rest_path, headers={}, legacy=False, legacy_auth=False, cache=True, ssl=False):
         url, headers = self.__prepare_request(rest_path, headers, legacy, legacy_auth)
         newrelic.agent.add_custom_parameter('service_url', url)
 
 
 class ManagedObject(object):
+    MANAGED = 'managed'
+    LIST = 'list'
+    DICT = 'dict'
+    DEFAULT = 'default'
 
     def from_dict(self, data):
         self.__dict__.update(data)
 
-    def to_dict(self, camel_case=True):
-        return_dict = {}
-        for k, v in self.__dict__.iteritems():
-            if isinstance(v, ManagedObject):
-                if camel_case:
-                    return_dict[CamelCase.parse(k)] = v.to_dict(camel_case)
-                else:
-                    return_dict[k] = v.to_dict(camel_case)
-
-            elif isinstance(v, list):
-                if camel_case:
-                    return_dict[CamelCase.parse(k)] = [item.to_dict(camel_case) for item in v]
-                else:
-                    return_dict[k] = [item.to_dict(camel_case) for item in v]
+    def _value_converter(self, _type):
+        converters = {
+            self.MANAGED: lambda x, c: x.to_dict(c),
+            self.LIST: lambda x, c: [i.to_dict(c) if hasattr(i, 'to_dict') else i for i in x],
+            self.DICT: self._dictify,
+            self.DEFAULT: lambda x, c: x
+        }
+        return converters[_type]
+
+    def _value_type(self, value):
+        if isinstance(value, ManagedObject):
+            return self.MANAGED
+        elif isinstance(value, (list, tuple)):
+            return self.LIST
+        elif isinstance(value, dict):
+            return self.DICT
+        return self.DEFAULT
+
+    def _dictify(self, _dict, camel_case):
+        resp = {}
+        for k, v in _dict.iteritems():
+            if k is None or v is None:
+                continue
+            key = CamelCase.parse(k) if camel_case else k
+            converter = self._value_converter(self._value_type(v))
+            resp[key] = converter(v, camel_case)
+        return resp
 
-            elif isinstance(v, dict):
-                for k2, v2 in v.iteritems():
-                    return_dict[k] = {}
-                    if camel_case:
-                        return_dict[CamelCase.parse(k)][CamelCase.parse(str(k2))] = v2.to_dict(camel_case)
-                    else:
-                        return_dict[k][k2] = v2.to_dict(camel_case)
-            else:
-                if k != None and v != None:
-                    if camel_case:
-                        return_dict[CamelCase.parse(k)] = v
-                    else:
-                        return_dict[k] = v
-        return return_dict
+    def to_dict(self, camel_case=True):
+        resp = self._dictify(self.__dict__, camel_case)
+        return resp
 
 
 class ListParameters (object):