2020-08-17 02:05:02 +02:00
|
|
|
import pika
|
2020-08-17 02:38:06 +02:00
|
|
|
|
2020-08-17 02:05:02 +02:00
|
|
|
from radicale import hook
|
2020-08-17 02:23:49 +02:00
|
|
|
from radicale.hook import HookNotificationItem
|
2020-08-17 02:05:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Hook(hook.BaseHook):
|
|
|
|
|
|
|
|
def __init__(self, configuration):
|
|
|
|
super().__init__(configuration)
|
|
|
|
endpoint = configuration.get("hook", "rabbitmq_endpoint")
|
2020-08-17 03:19:27 +02:00
|
|
|
self._topic = configuration.get("hook", "rabbitmq_topic")
|
|
|
|
self._encoding = configuration.get("encoding", "stock")
|
2020-08-17 02:05:02 +02:00
|
|
|
|
|
|
|
self._make_connection_synced(endpoint)
|
2020-08-17 03:19:27 +02:00
|
|
|
self._make_declare_queue_synced(self._topic)
|
2020-08-17 02:05:02 +02:00
|
|
|
|
|
|
|
def _make_connection_synced(self, endpoint):
|
|
|
|
parameters = pika.URLParameters(endpoint)
|
2020-08-17 03:19:27 +02:00
|
|
|
connection = pika.BlockingConnection(parameters)
|
|
|
|
self._channel = connection.channel()
|
2020-08-17 02:05:02 +02:00
|
|
|
|
|
|
|
def _make_declare_queue_synced(self, topic):
|
2020-08-17 14:44:16 +02:00
|
|
|
self._channel.queue_declare(queue=topic, durable=True)
|
2020-08-17 02:05:02 +02:00
|
|
|
|
2020-08-17 02:14:04 +02:00
|
|
|
def notify(self, notification_item):
|
|
|
|
if isinstance(notification_item, HookNotificationItem):
|
2020-08-17 03:19:27 +02:00
|
|
|
self._channel.basic_publish(
|
2020-08-17 02:14:04 +02:00
|
|
|
exchange='',
|
2020-08-17 03:19:27 +02:00
|
|
|
routing_key=self._topic,
|
2020-08-17 03:32:13 +02:00
|
|
|
body=notification_item.to_json().encode(
|
|
|
|
encoding=self._encoding
|
|
|
|
)
|
2020-08-17 03:01:21 +02:00
|
|
|
)
|