Commits

Anonymous committed 695614c

Implement custom handlers and update the documentations

  • Participants
  • Parent commits 323f000

Comments (0)

Files changed (2)

File docs/wiki/config/index.mdown

 Over time, there will be more custom handlers with various options to control them.
 
 What if you do not want to use any of the default handlers, but instead, use your own proprietary system?
+----------------------------------------------------- ---------------------------------------------------
 
-There is support for drop-in replacements of handlers so that you might write your own custom handler. More documentation on this to follow.
+There is support for drop-in replacements of handlers so that you might write your own custom handler. It's actually quite simple. All that's needed is that you implement a class, that implements an `enqueue` method which takes two parameters, `payload`, and `timeout`. You also have to import the api that's needed to report.
+
+For example, consider the following class below:
+
+    from hoptoad.api import htv2
+
+    class SomeAwesomeReportingMethod(object):
+        def enqueue(self, payload, timeout):
+            """This enqueue method is your own implementation"""
+            htv2.report(payload, timeout)
+
+You would need to also to set two variables in `settings.py`:
+
+    HOPTOAD_HANDLER = "/path/to/the/custom/implementation.py"
+    HOPTOAD_HANDLER_CLASS = "SomeAwesomeReport"
+
+As you can see, `HOPTOAD_HANDLER` is the file location to the implementation of the custom handler and `HOPTOAD_HANDLER_CLASS` is the name of the actual class that implements the enqueue method.
 
 Hoptoad Notification URL
 ------------------------

File hoptoad/handlers/__init__.py

 various different protocols.
 """
 import logging
+import os
+import imp
+import pprint
 
 from hoptoad import get_hoptoad_settings
 from hoptoad.handlers.threaded import ThreadedNotifier
         return ThreadedNotifier(threads , *args, **kwargs)
     elif handler.lower() == 'blocking':
         return BlockingNotifier(*args, **kwargs)
+    else:
+        _class_module = hoptoad_settings.get('HOPTOAD_HANDLER_CLASS', None)
+        if not _class_module:
+            # not defined, abort setting up hoptoad, skip it.
+            raise MiddlewareNotUsed
+        # module name that we should import from
+        _module_name = os.path.splitext(os.path.basename(handler))[0]
+        # load the module!
+        m = imp.load_module(_module_name,
+                            *imp.find_module(_module_name,
+                                             [os.path.dirname(handler)]))
+
+        # instantiate the class
+        return getattr(m, _class_module)(*args, **kwargs)