DEV Community

How to simplify a complex factory pattern?

Arunmozhi on April 05, 2020

I have a function which takes the incoming request, parses the data and performs an action and posts the results to a webhook. This is running as b...
Collapse
 
tecoholic profile image
Arunmozhi

I solved it by making two important changes:

  1. Normalised the processor's by making the common arguments positional and everything else keyword based. This allows me to pass the kwargs as I receive them without unpacking. It is the processor's job.
  2. For the extra files, make a copy of the kwargs and replace the remote file url with the local file location. This way, the extra files are a part of the kwargs dict itself.
def run_task(action, input_file, *args, **kwargs):

    params = kwargs.copy()

    # Get the input file from a URL
    log = create_logitem()
    try:
        file = get_input_file(input_file)
        if action == "action_2":
           params["extra_file"] = get_input_file(kwargs["extra_file"]  # update the files in params
    except:
        log.status = "Failure"

    # process the input file
    try:
        processor = processors[action](file)
        results = processor.execute(**params)   # Unpack and pass the params
    except:
        log.status = "Failure"

    # upload the results to another location
    try:
        upload_result_file(results.file)
    except:
        log.status = "Failure"

    # Post the log about the entire process to a webhoook
    post_results_to_webhook(log)

Now I have the same lean structure as I originally had. The only processor specific code is the file downloads which I think I can live with for now.

Collapse
 
tecoholic profile image
Arunmozhi

Very interesting solution. I didn't know there existed dispatcher wrappers. I will try it out and see if it make simplifies the code. Thank you.

Collapse
 
tecoholic profile image
Arunmozhi

I have asked the question in Stack Exchange as well and was offered a simple solution which involves minimal rewrite. Here softwareengineering.stackexchange....