コード例 #1
0
 def sanitize_for_serialization(cls, obj):
     """Prepares data for transmission before it is sent with the rest client
     If obj is None, return None.
     If obj is str, int, long, float, bool, return directly.
     If obj is datetime.datetime, datetime.date
         convert to string in iso8601 format.
     If obj is list, sanitize each element in the list.
     If obj is dict, return the dict.
     If obj is OpenAPI model, return the properties dict.
     If obj is io.IOBase, return the bytes
     :param obj: The data to serialize.
     :return: The serialized form of data.
     """
     if isinstance(obj, (ModelNormal, ModelComposed)):
         return {
             key: cls.sanitize_for_serialization(val) for key, val in model_to_dict(obj, serialize=True).items()
         }
     elif isinstance(obj, io.IOBase):
         return cls.get_file_data_and_close_file(obj)
     elif isinstance(obj, (str, int, float, none_type, bool)):
         return obj
     elif isinstance(obj, (datetime, date)):
         return obj.isoformat()
     elif isinstance(obj, ModelSimple):
         return cls.sanitize_for_serialization(obj.value)
     elif isinstance(obj, (list, tuple)):
         return [cls.sanitize_for_serialization(item) for item in obj]
     if isinstance(obj, dict):
         return {key: cls.sanitize_for_serialization(val) for key, val in obj.items()}
     raise ApiValueError('Unable to prepare type {} for serialization'.format(obj.__class__.__name__))
コード例 #2
0
    def sanitize_for_serialization(self, obj):
        """Builds a JSON POST object.

        If obj is None, return None.
        If obj is str, int, long, float, bool, return directly.
        If obj is datetime.datetime, datetime.date
            convert to string in iso8601 format.
        If obj is list, sanitize each element in the list.
        If obj is dict, return the dict.
        If obj is OpenAPI model, return the properties dict.

        :param obj: The data to serialize.
        :return: The serialized form of data.
        """
        if obj is None:
            return None
        elif isinstance(obj, self.PRIMITIVE_TYPES):
            return obj
        elif isinstance(obj, list):
            return [
                self.sanitize_for_serialization(sub_obj) for sub_obj in obj
            ]
        elif isinstance(obj, tuple):
            return tuple(
                self.sanitize_for_serialization(sub_obj) for sub_obj in obj)
        elif isinstance(obj, (datetime, date)):
            return obj.isoformat()

        if isinstance(obj, dict):
            obj_dict = obj
        elif isinstance(obj, ModelNormal) or isinstance(obj, ModelComposed):
            # Convert model obj to dict
            # Convert attribute name to json key in
            # model definition for request
            obj_dict = model_to_dict(obj, serialize=True)
        elif isinstance(obj, ModelSimple):
            return self.sanitize_for_serialization(obj.value)

        return {
            key: self.sanitize_for_serialization(val)
            for key, val in obj_dict.items()
        }