Can we use multiple events handler and get event type?

class Service:
 
    name = "service"
    @event_handler("main_dispatcher", "vendor1.event1")
    @event_handler("main_dispatcher", "vendro2.event1")
    @event_handler("main_dispatcher", "vendro2.event2")
    def parse(self, body):
        if event_type == 'vendro2.event1":
            params = vendro2.parse(body)

I need to process a few events in one Service, can anyone show right way for this ?

The event_handler API is slightly annoying in not supporting this.

Your options are either to declare a common method body, and a wrapper method for each handler you want, or use partials.

i.e.

class Service:
    name = "service"

    @event_handler("main_dispatcher", "vendor1.event1")
    def vendor1_event1(self, body):
        self.base(body, type="vendor1.event1")
        
    @event_handler("main_dispatcher", "vendor1.event1")
    def vendor2_event1(self, body):
        self.base(body, type="vendor2.event1")

    def base(self, body, type):
        ...

or.

handle_vendor1_event1 = partial(event_handler, "vendor1.event1")
handle_vendor2_event1 = partial(event_handler, "vendor2.event1")

class Service:
    name = "service"

    @handle_vendor1_event1
    @handle_vendor2_event1
    def handle(self, type, body):
        ...

I haven’t actually tested the latter but I think it would work.

I would probably favour the first approach

2 Likes

thanks, will try, BTW second looks more pretty