Example #1
0
    async def run(self, payload: dict):

        dot = DotAccessor(self.profile, self.session, payload, self.event,
                          self.flow)
        for destination, value in self.mapping.items():
            if destination in dot:
                if not isinstance(dot[destination], list):
                    dot[destination] = [dot[destination]]

                if value not in dot[destination]:
                    dot[destination].append(value)
            else:
                dot[destination] = value

        if not isinstance(dot.profile['traits']['private'], dict):
            raise ValueError(
                "Error when appending [email protected] to value `{}`. Private must have key:value pair. "
                "E.g. `name`: `{}`".format(dot.profile['traits']['private'],
                                           dot.profile['traits']['private']))

        if not isinstance(dot.profile['traits']['public'], dict):
            raise ValueError(
                "Error when appending [email protected] to value `{}`. Public must have key:value pair. "
                "E.g. `name`: `{}`".format(dot.profile['traits']['public'],
                                           dot.profile['traits']['public']))

        profile = Profile(**dot.profile)
        event = Event(**dot.event)
        session = Session(**dot.session)

        self.profile.replace(profile)
        self.session.replace(session)
        self.event.replace(event)

        return Result(port="payload", value=payload)
Example #2
0
def test_value_read():
    profile = Profile(id="1")
    session = Session(id="2")
    payload = {"a": 3}
    source = Source(id="3", type="event")
    context = Context()
    event = Event(id="event-id", type="type", source=source, context=context, profile=profile, session=session)
    flow = Flow(id="flow-id", name="flow")
    dot = DotAccessor(profile, session, payload, event, flow)

    assert dot['profile@id'] == "1"
    assert dot['session@id'] == "2"
    assert dot['payload@a'] == 3
    assert dot['flow@id'] == "flow-id"
    assert dot['event@id'] == "event-id"

    try:
        _ = dot['profile@none']
        assert False
    except KeyError:
        assert True

    dot['payload@b'] = 2
    assert dot['payload@b'] == 2

    dot['[email protected]'] = 2
    assert dot['[email protected]'] == 2

    assert dot.profile['other']['a'] == 2
Example #3
0
    async def run(self, payload):

        dot = DotAccessor(self.profile, self.session, payload, self.event,
                          self.flow)

        try:

            value = dot[self.field]

            if value is None:
                value = 0

        except KeyError:
            value = 0

        if type(value) != int:
            raise ValueError("Filed `{}` value is not numeric.".format(
                self.field))

        value += self.increment

        dot[self.field] = value

        self.profile.replace(Profile(**dot.profile))

        return Result(port="payload", value=payload)
Example #4
0
def test_value_exists():
    profile = Profile(id="1")
    session = Session(id="2")
    payload = {"a": 3}
    source = Source(id="3", type="event")
    context = Context()
    event = Event(id="event-id", type="type", source=source, context=context, profile=profile, session=session)
    flow = Flow(id="flow-id", name="flow")
    dot = DotAccessor(profile, session, payload, event, flow)

    assert 'profile@id' in dot
    assert 'profile@missing' not in dot
Example #5
0
    async def run(self, payload: dict):

        dot = DotAccessor(self.profile, self.session, payload, self.event,
                          self.flow)

        for value in self.delete:
            del dot[value]

        profile = Profile(**dot.profile)

        self.profile.replace(profile)

        return Result(port="payload", value=payload)
Example #6
0
    async def run(self, payload):

        dot = DotAccessor(self.profile, self.session, payload, self.event, self.flow)
        try:
            event_properties = dot[self.event_properties]
        except ValueError:
            # If there is error that means properties mus be as defined not a path.
            if isinstance(self.event_properties, dict):
                event_properties = self.event_properties
            else:
                raise ValueError("Properties must be either dict or dot notation path to event, profile or payload. Please correct action configuration.")

        session_copy = self.session.copy(deep=True)
        profile_copy = self.profile.copy(deep=True)

        event_result = await RulesEngine.raise_event(
            self.event_type,
            event_properties,
            session_copy,
            profile_copy,
            source_id=None)

        return Result(port="result", value=event_result)
Example #7
0
 async def run(self, payload: dict):
     dot = DotAccessor(self.profile, self.session, payload, self.event,
                       self.flow)
     return Result(port="trait", value=dot[self.property])