Ejemplo n.º 1
0
    def object_from_json(self, object_type, object_json):
        """
        Given a blob of JSON representing a Zenpy object, recursively deserialize it and
         any nested objects it contains. This method also adds the deserialized object
         to the relevant cache if applicable.
        """
        if not isinstance(object_json, dict):
            return object_json
        ZenpyClass = self.class_for_type(object_type)
        obj = ZenpyClass(api=self.api)
        for key, value in object_json.items():
            if isinstance(value, dict):
                key = self.format_key(key, parent=obj)
                if key in self.class_mapping:
                    value = self.object_from_json(key, value)
                elif as_singular(key) in self.class_mapping:
                    value = self.object_from_json(as_singular(key), value)
            elif isinstance(value, list) and self.format_key(as_singular(key), parent=obj) in self.class_mapping:

                zenpy_objects = list()
                for item in value:
                    zenpy_objects.append(self.object_from_json(self.format_key(as_singular(key), parent=obj), item))
                value = zenpy_objects
            setattr(obj, key, value)
        add_to_cache(obj)
        return obj
Ejemplo n.º 2
0
    def object_from_json(self, object_type, object_json):
        """ 
        Given a blob of JSON representing a Zenpy object, recursively deserialize it and 
         any nested objects it contains. This method also adds the deserialized object
         to the relevant cache if applicable. 
        """
        if not isinstance(object_json, dict):
            return object_json
        ZenpyClass = self.class_for_type(object_type)
        obj = ZenpyClass(api=self.api)
        for key, value in object_json.items():
            if isinstance(value, dict):
                key = self.format_key(key, parent=obj)
                if key in self.class_mapping:
                    value = self.object_from_json(key, value)
                elif as_singular(key) in self.class_mapping:
                    value = self.object_from_json(as_singular(key), value)
            elif isinstance(value, list) and self.format_key(as_singular(key), parent=obj) in self.class_mapping:

                zenpy_objects = list()
                for item in value:
                    zenpy_objects.append(self.object_from_json(self.format_key(as_singular(key), parent=obj), item))
                value = zenpy_objects
            setattr(obj, key, value)
        add_to_cache(obj)
        return obj
Ejemplo n.º 3
0
 def object_from_json(self, object_type, object_json, parent=None):
     """
     Given a blob of JSON representing a Zenpy object, recursively deserialize it and
      any nested objects it contains. This method also adds the deserialized object
      to the relevant cache if applicable.
     """
     if not isinstance(object_json, dict):
         return object_json
     obj = self.instantiate_object(object_type, parent)
     for key, value in object_json.items():
         if key not in self.skip_attrs:
             key, value = self._deserialize(key, obj, value)
         if isinstance(value, dict):
             value = ProxyDict(value,
                               dirty_callback=getattr(
                                   obj, '_dirty_callback', None))
         elif isinstance(value, list):
             value = ProxyList(value,
                               dirty_callback=getattr(
                                   obj, '_dirty_callback', None))
         setattr(obj, key, value)
     if hasattr(obj, '_clean_dirty'):
         obj._clean_dirty()
     add_to_cache(obj)
     return obj
Ejemplo n.º 4
0
 def cache_item(self, zenpy_class=Ticket, **kwargs):
     zenpy_object = zenpy_class(**kwargs)
     add_to_cache(zenpy_object)
     return zenpy_object
Ejemplo n.º 5
0
 def cache_item(self, zenpy_class=Ticket, **kwargs):
     zenpy_object = zenpy_class(**kwargs)
     add_to_cache(zenpy_object)
     return zenpy_object