|
| 1 | +"""NATS Queue configuration.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import Any, Mapping, Optional |
| 5 | + |
| 6 | +from pydantic import BaseModel |
| 7 | + |
| 8 | +LOGGER = logging.getLogger(__name__) |
| 9 | + |
| 10 | +EVENT_TOPIC_MAP = { |
| 11 | + "^acapy::webhook::(.*)$": "acapy.webhook.$wallet_id", |
| 12 | + "^acapy::record::([^:]*)::([^:]*)$": "acapy.record-with-state.$wallet_id", |
| 13 | + "^acapy::record::([^:])?": "acapy.record.$wallet_id", |
| 14 | + "acapy::basicmessage::received": "acapy.basicmessage.received", |
| 15 | + "acapy::problem_report": "acapy.problem_report", |
| 16 | + "acapy::ping::received": "acapy.ping.received", |
| 17 | + "acapy::ping::response_received": "acapy.ping.response_received", |
| 18 | + "acapy::actionmenu::received": "acapy.actionmenu.received", |
| 19 | + "acapy::actionmenu::get-active-menu": "acapy.actionmenu.get-active-menu", |
| 20 | + "acapy::actionmenu::perform-menu-action": "acapy.actionmenu.perform-menu-action", |
| 21 | + "acapy::keylist::updated": "acapy.keylist.updated", |
| 22 | + "acapy::revocation-notification::received": "acapy.revocation-notification.received", |
| 23 | + "acapy::revocation-notification-v2::received": "acapy.revocation-notification-v2.received", # noqa: E501 |
| 24 | + "acapy::forward::received": "acapy.forward.received", |
| 25 | + "acapy::outbound-message::queued_for_delivery": "acapy.outbound-message.queued-for-delivery", # noqa: E501 |
| 26 | +} |
| 27 | + |
| 28 | +EVENT_WEBHOOK_TOPIC_MAP = { |
| 29 | + "acapy::basicmessage::received": "basicmessages", |
| 30 | + "acapy::problem_report": "problem_report", |
| 31 | + "acapy::ping::received": "ping", |
| 32 | + "acapy::ping::response_received": "ping", |
| 33 | + "acapy::actionmenu::received": "actionmenu", |
| 34 | + "acapy::actionmenu::get-active-menu": "get-active-menu", |
| 35 | + "acapy::actionmenu::perform-menu-action": "perform-menu-action", |
| 36 | + "acapy::keylist::updated": "keylist", |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +def _alias_generator(key: str) -> str: |
| 41 | + return key.replace("_", "-") |
| 42 | + |
| 43 | + |
| 44 | +class ConnectionConfig(BaseModel): |
| 45 | + """Connection configuration model.""" |
| 46 | + |
| 47 | + connection_url: str |
| 48 | + |
| 49 | + class Config: |
| 50 | + """Pydantic config.""" |
| 51 | + |
| 52 | + alias_generator = _alias_generator |
| 53 | + populate_by_name = True |
| 54 | + |
| 55 | + @classmethod |
| 56 | + def default(cls): |
| 57 | + """Default connection configuration.""" |
| 58 | + return cls(connection_url="nats://default:test1234@localhost:4222") |
| 59 | + |
| 60 | + |
| 61 | +class EventConfig(BaseModel): |
| 62 | + """Event configuration model.""" |
| 63 | + |
| 64 | + event_topic_maps: Mapping[str, str] = EVENT_TOPIC_MAP |
| 65 | + event_webhook_topic_maps: Mapping[str, str] = EVENT_WEBHOOK_TOPIC_MAP |
| 66 | + deliver_webhook: bool = True |
| 67 | + |
| 68 | + class Config: |
| 69 | + """Pydantic config.""" |
| 70 | + |
| 71 | + alias_generator = _alias_generator |
| 72 | + populate_by_name = True |
| 73 | + |
| 74 | + @classmethod |
| 75 | + def default(cls): |
| 76 | + """Default event configuration.""" |
| 77 | + return cls( |
| 78 | + event_topic_maps=EVENT_TOPIC_MAP, |
| 79 | + event_webhook_topic_maps=EVENT_WEBHOOK_TOPIC_MAP, |
| 80 | + deliver_webhook=True, |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +class InboundConfig(BaseModel): |
| 85 | + """Inbound configuration model.""" |
| 86 | + |
| 87 | + acapy_inbound_topic: str = "acapy_inbound" |
| 88 | + acapy_direct_resp_topic: str = "acapy_inbound_direct_resp" |
| 89 | + |
| 90 | + class Config: |
| 91 | + """Pydantic config.""" |
| 92 | + |
| 93 | + alias_generator = _alias_generator |
| 94 | + populate_by_name = True |
| 95 | + |
| 96 | + @classmethod |
| 97 | + def default(cls): |
| 98 | + """Default inbound configuration.""" |
| 99 | + return cls( |
| 100 | + acapy_inbound_topic="acapy_inbound", |
| 101 | + acapy_direct_resp_topic="acapy_inbound_direct_resp", |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +class OutboundConfig(BaseModel): |
| 106 | + """Outbound configuration model.""" |
| 107 | + |
| 108 | + acapy_outbound_topic: str = "acapy_outbound" |
| 109 | + mediator_mode: bool = False |
| 110 | + |
| 111 | + @classmethod |
| 112 | + def default(cls): |
| 113 | + """Default outbound configuration.""" |
| 114 | + return cls( |
| 115 | + acapy_outbound_topic="acapy_outbound", |
| 116 | + mediator_mode=False, |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +class NATSConfig(BaseModel): |
| 121 | + """NATS configuration model.""" |
| 122 | + |
| 123 | + event: Optional[EventConfig] = EventConfig.default() |
| 124 | + inbound: Optional[InboundConfig] = InboundConfig.default() |
| 125 | + outbound: Optional[OutboundConfig] = OutboundConfig.default() |
| 126 | + connection: ConnectionConfig |
| 127 | + |
| 128 | + @classmethod |
| 129 | + def default(cls): |
| 130 | + """Default NATS configuration.""" |
| 131 | + return cls( |
| 132 | + event=EventConfig.default(), |
| 133 | + inbound=InboundConfig.default(), |
| 134 | + outbound=OutboundConfig.default(), |
| 135 | + connection=ConnectionConfig.default(), |
| 136 | + ) |
| 137 | + |
| 138 | + |
| 139 | +def process_config_dict(config_dict: dict) -> dict: |
| 140 | + """Add connection to inbound, outbound, event and return updated config.""" |
| 141 | + filter = ["inbound", "event", "outbound", "connection"] |
| 142 | + for key, value in config_dict.items(): |
| 143 | + if key in filter: |
| 144 | + config_dict[key] = value |
| 145 | + return config_dict |
| 146 | + |
| 147 | + |
| 148 | +def get_config(settings: Mapping[str, Any]) -> NATSConfig: |
| 149 | + """Retrieve producer configuration from settings.""" |
| 150 | + try: |
| 151 | + LOGGER.debug("Constructing config from: %s", settings.get("plugin_config")) |
| 152 | + config_dict = settings["plugin_config"].get("nats_queue", {}) |
| 153 | + LOGGER.debug("Retrieved: %s", config_dict) |
| 154 | + config_dict = process_config_dict(config_dict) |
| 155 | + config = NATSConfig(**config_dict) |
| 156 | + except KeyError: |
| 157 | + LOGGER.warning("Using default configuration") |
| 158 | + config = NATSConfig.default() |
| 159 | + |
| 160 | + LOGGER.debug("Returning config: %s", config.model_dump_json(indent=2)) |
| 161 | + LOGGER.debug( |
| 162 | + "Returning config(aliases): %s", config.model_dump_json(by_alias=True, indent=2) |
| 163 | + ) |
| 164 | + return config |
0 commit comments