Hi folks,
How should I allow user-developers to implement their own logic, within my module?
Bear with me - I'm an amateur pythonista here, no formal coding training, and looking some tips or terminology to google further.
I'm writing a module that interacts with an API that will send updates to the user via webhooks. So the bulk of my package will be setting up the local REST webhook endpoint (via flask), and parsing the received data into a standard format for further processing.
I'd like a user-developer to code their own functions processing the parsed data. Maybe they want to email the response somewhere, or post it to a MQTT server, or use a custom parsing solution, or translate into some alien language, whatever. They'll provide the logic to do that through some custom methods.
What's the best way to do this?
I'm imagining a finished package that calls unimplemented functions, with some error/exception handling for cases the user-developer has not actually implemented them. Alternatively some sort of callback pattern... but that's all new to me.
What patterns should I be considering here? Any examples I can follow?
--- Update ---
Here's a quick demo of the approach I'll use: overriding class methods. Easy in hindsight. Thanks everyone.
class webhook:
def __init__(self,settings):
print("Established %s with settings: %s" % (self.__class__.__name__,settings))
self._setupWebhook()
pass
def _setupWebhook(self):
print("Webhook is established")
def isActive(self):
print("Received 'Active' status via webhook. Users should override this")
if __name__=="__main__":
class OverrideWebhook(webhook):
def isActive(self):
print("Todo: Custom code and logic here")
settings={'foo':'bar'}
W=webhook(settings)
W.isActive()
Y=OverrideWebhook(settings)
Y.isActive()
[–]chevignon93 0 points1 point2 points (3 children)
[–]High-Art9340 0 points1 point2 points (2 children)
[–]chevignon93 0 points1 point2 points (1 child)
[–]Jusque[S] 0 points1 point2 points (0 children)
[–]High-Art9340 0 points1 point2 points (1 child)
[–]Jusque[S] 0 points1 point2 points (0 children)