text: import logging
  
  
  def middle_logic(functions: list, **args):
      """
      Recursive function that calls each node in the list.
      Each node has a "try" function that is executed first. If the try
      function returns True, the next node in the list is returned.  If the
      try function returns False, the node's "fail" list is executed in the
      same way.
  
      example = dict({
              "rules": [
                  {
                      "pass": success1,
                      "fail": [
                          {
                              "pass": failure1,
                              "fail": []
                          }
                      ],
                  },
              ]
          })
  
      The middleware is called like this: middle_logic(example['rules'])
      """
      if functions:
          try_fail_node = functions.pop(0)
          flag, args = try_fail_node['try'](**args)
          logging.debug("result from {} is {}".format(try_fail_node['try'].__name__, flag))
          if flag:
              args = middle_logic(functions, **args)
          else:
              args = middle_logic(try_fail_node['fail'], **args)
      return args
  
  
  