diff --git a/examples/plugins.py b/examples/plugins.py new file mode 100644 index 00000000..f6486a64 --- /dev/null +++ b/examples/plugins.py @@ -0,0 +1,13 @@ +from pubnub import pnconfiguration +from pubnub.models.consumer.plugin import HookPoint, Plugin +from pubnub.pubnub import PubNub +from pubnub.pnconfiguration import PNConfiguration +from pubnub_beta.entities import entity_plugins + + +config = PNConfiguration() +config.uuid = 'test' +config.publish_key = 'test' +config.subscribe_key = 'test' +pn = PubNub(config, plugins=entity_plugins) +pn.spaces_metadata() \ No newline at end of file diff --git a/pubnub/pubnub.py b/pubnub/pubnub.py index 11477753..1b0f90ca 100644 --- a/pubnub/pubnub.py +++ b/pubnub/pubnub.py @@ -5,6 +5,8 @@ from threading import Event from queue import Queue, Empty +from pubnub.models.consumer.plugin import HookPoint, PluginList + from . import utils from .request_handlers.base import BaseRequestHandler from .request_handlers.requests_handler import RequestsRequestHandler @@ -26,10 +28,10 @@ class PubNub(PubNubCore): """PubNub Python API""" - def __init__(self, config): + def __init__(self, config, plugins=[]): assert isinstance(config, PNConfiguration) - - PubNubCore.__init__(self, config) + core_plugins = filter(lambda plugin: plugin.hook_point == HookPoint.PubNubCore, plugins) + PubNubCore.__init__(self, config, plugins=core_plugins) self._request_handler = RequestsRequestHandler(self) if self.config.enable_subscribe: diff --git a/pubnub/pubnub_core.py b/pubnub/pubnub_core.py index 42f9792a..191f45d7 100644 --- a/pubnub/pubnub_core.py +++ b/pubnub/pubnub_core.py @@ -72,8 +72,9 @@ class PubNubCore: MAX_SEQUENCE = 65535 __metaclass__ = ABCMeta + _plugins = [] - def __init__(self, config): + def __init__(self, config, plugins=[]): self.config = config self.config.validate() self.headers = { @@ -85,6 +86,8 @@ def __init__(self, config): self._telemetry_manager = TelemetryManager() self._base_path_manager = BasePathManager(config) self._token_manager = TokenManager() + for plugin in plugins: + self.__dict__[plugin.name] = plugin.method @property def base_origin(self): diff --git a/pubnub_beta/entities.py b/pubnub_beta/entities.py new file mode 100644 index 00000000..6657b7d7 --- /dev/null +++ b/pubnub_beta/entities.py @@ -0,0 +1,13 @@ +from pubnub.models.consumer.plugin import Plugin, HookPoint + +def spaces_metadata(): + raise ("This will get implemented") + +spaces_metadata_plugin = Plugin( + HookPoint.PubNubCore, + 'spaces_metadata', + spaces_metadata +) + + +entity_plugins = [spaces_metadata_plugin] \ No newline at end of file