MCPcopy Create free account
hub / github.com/StackStorm/st2 / _process_response

Method _process_response

st2common/st2common/router.py:770–845  ·  view source on GitHub ↗

Process controller response data such as removing attributes based on the values of exclude_attributes and include_attributes query param filters and similar. :param data: Response data. :type: data: ``list`` or ``dict``

(
        self,
        data,
        mandatory_include_fields=None,
        include_attributes=None,
        exclude_attributes=None,
    )

Source from the content-addressed store, hash-verified

768 return instance
769
770 def _process_response(
771 self,
772 data,
773 mandatory_include_fields=None,
774 include_attributes=None,
775 exclude_attributes=None,
776 ):
777 """
778 Process controller response data such as removing attributes based on the values of
779 exclude_attributes and include_attributes query param filters and similar.
780
781 :param data: Response data.
782 :type: data: ``list`` or ``dict``
783 """
784 mandatory_include_fields = mandatory_include_fields or []
785 include_attributes = include_attributes or []
786 exclude_attributes = exclude_attributes or []
787
788 # NOTE: include_attributes and exclude_attributes are mutually exclusive
789 if include_attributes and exclude_attributes:
790 msg = (
791 "exclude_attributes and include_attributes arguments are mutually exclusive. "
792 "You need to provide either one or another, but not both."
793 )
794 raise ValueError(msg)
795
796 # Common case - filters are not provided
797 if not include_attributes and not exclude_attributes:
798 return data
799
800 # Skip processing of error responses
801 if isinstance(data, dict) and data.get("faultstring", None):
802 return data
803
804 # We only care about the first part of the field name since deep filtering happens inside
805 # MongoDB. Deep filtering here would also be quite expensive and waste of CPU cycles.
806 cleaned_include_attributes = [
807 attribute.split(".")[0] for attribute in include_attributes
808 ]
809
810 # Add in mandatory fields which always need to be present in the response (primary keys)
811 cleaned_include_attributes += mandatory_include_fields
812 cleaned_exclude_attributes = [
813 attribute.split(".")[0] for attribute in exclude_attributes
814 ]
815
816 # NOTE: Since those parameters are mutually exclusive we could perform more efficient
817 # filtering when just exclude_attributes is provided. Instead of creating a new dict, we
818 # could just manipulate (delete) from the existing one.
819 def process_item(item):
820 result = {}
821 for name, value in six.iteritems(item):
822 if include_attributes and name not in cleaned_include_attributes:
823 continue
824
825 if exclude_attributes and name in cleaned_exclude_attributes:
826 continue
827

Callers 1

__call__Method · 0.95

Calls 1

getMethod · 0.45

Tested by

no test coverage detected