abhishek-ram / django-pyas2

AS2 file transfer Server built on Python and Django.
https://django-pyas2.readthedocs.io
GNU General Public License v3.0
78 stars 31 forks source link

Callback when message has been received #51

Open ioFilippo opened 3 years ago

ioFilippo commented 3 years ago

Hi, There is a more elegant way to trigger a received message event without monkey patching 'run_post_receive' like I did below?

from pyas2 import utils
import requests

old_run_post_receive = utils.run_post_receive

def new_run_post_receive(message, full_filename):
    old_run_post_receive(message, full_filename)
    payload = dict(
        id='1',
        access_token='xyz'
    )
    r = requests.post('http://127.0.0.1/url/to/post', data=payload)

utils.run_post_receive = new_run_post_receive
abhishek-ram commented 3 years ago

Why not just define this logic in the post receive command? Create a post receive script post_as2_message_receive.py which would be something like

#!/usr/bin/env python3
import requests

payload = dict(
        id='1',
        access_token='xyz'
)
r = requests.post('http://127.0.0.1/url/to/post', data=payload)

# Put additional post receive logic here

And set Command on Message Receipt: on the partner to path_to_script/post_as2_message_receive.py

ioFilippo commented 3 years ago

Thanks for your answer. Good idea but I am not a fan on using external scripts. Anyway I guess a "Command on Message Receipt" can handle received data too (passing that as a parameter). Do you have other approaches I could dig into? What do you think implementing a feature to handle callbacks on such events? (I might work on that)

abhishek-ram commented 3 years ago

It would be part of your repo so I don't see it as an external script. I dont see a reason to implement callbacks as it can be handled in this command.

gainskills commented 3 years ago

I have the same question here. For my opinion: I prefer to use callback instead of external command.

ioFilippo commented 3 years ago

I guess with callbacks you can have some pros, for example a return code to refuse the message or the chance to handle the content of the message directly or change headers data too before saving.

gainskills commented 3 years ago

Hi @abhishek-ram , @bluebytech , I tried an implementation for this and did a pull request: https://github.com/abhishek-ram/django-pyas2/pull/53.

Could you please review it and share your suggestions?

with the PR, the integration can be done in another Django apps in this way instead of defining an external command

from pyas2.utils import pyas2Utils

def cutmize_run(*args, **kwargs):
    print(args, kwargs)

pyas2Utils.cust_run_post_receive = cutmize_run

Thanks,