|
| 1 | +# ================================================================= |
| 2 | +# |
| 3 | +# Authors: Tom Kralidis <[email protected]> |
| 4 | +# Angelos Tzotsos <[email protected]> |
| 5 | +# |
| 6 | +# Copyright (c) 2025 Tom Kralidis |
| 7 | +# Copyright (c) 2025 Angelos Tzotsos |
| 8 | +# |
| 9 | +# Permission is hereby granted, free of charge, to any person |
| 10 | +# obtaining a copy of this software and associated documentation |
| 11 | +# files (the "Software"), to deal in the Software without |
| 12 | +# restriction, including without limitation the rights to use, |
| 13 | +# copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | +# copies of the Software, and to permit persons to whom the |
| 15 | +# Software is furnished to do so, subject to the following |
| 16 | +# conditions: |
| 17 | +# |
| 18 | +# The above copyright notice and this permission notice shall be |
| 19 | +# included in all copies or substantial portions of the Software. |
| 20 | +# |
| 21 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 22 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 23 | +# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 24 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 25 | +# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 26 | +# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 27 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 28 | +# OTHER DEALINGS IN THE SOFTWARE. |
| 29 | +# |
| 30 | +# ================================================================= |
| 31 | + |
| 32 | +import logging |
| 33 | + |
| 34 | +import requests |
| 35 | + |
| 36 | +from pycsw.broker.base import BasePubSubClient |
| 37 | + |
| 38 | +LOGGER = logging.getLogger(__name__) |
| 39 | + |
| 40 | + |
| 41 | +class HTTPPubSubClient(BasePubSubClient): |
| 42 | + """HTTP client""" |
| 43 | + |
| 44 | + def __init__(self, broker_url): |
| 45 | + """ |
| 46 | + Initialize object |
| 47 | +
|
| 48 | + :param publisher_def: provider definition |
| 49 | +
|
| 50 | + :returns: pycsw.pubsub.http.HTTPPubSubClient |
| 51 | + """ |
| 52 | + |
| 53 | + super().__init__(broker_url) |
| 54 | + self.type = 'http' |
| 55 | + self.auth = None |
| 56 | + |
| 57 | + msg = f'Initializing to broker {self.broker_safe_url} with id {self.client_id}' # noqa |
| 58 | + LOGGER.debug(msg) |
| 59 | + |
| 60 | + if None not in [self.broker_url.username, self.broker_url.password]: |
| 61 | + LOGGER.debug('Setting credentials') |
| 62 | + self.auth = ( |
| 63 | + self.broker_url.username, |
| 64 | + self.broker_url.password |
| 65 | + ) |
| 66 | + |
| 67 | + def connect(self) -> None: |
| 68 | + """ |
| 69 | + Connect to an HTTP broker |
| 70 | +
|
| 71 | + :returns: None |
| 72 | + """ |
| 73 | + |
| 74 | + LOGGER.debug('No connection to HTTP') |
| 75 | + pass |
| 76 | + |
| 77 | + def pub(self, channel: str, message: str, qos: int = 1) -> bool: |
| 78 | + """ |
| 79 | + Publish a message to a broker/channel |
| 80 | +
|
| 81 | + :param channel: `str` of topic |
| 82 | + :param message: `str` of message |
| 83 | +
|
| 84 | + :returns: `bool` of publish result |
| 85 | + """ |
| 86 | + |
| 87 | + LOGGER.debug(f'Publishing to broker {self.broker_safe_url}') |
| 88 | + LOGGER.debug(f'Channel: {channel}') |
| 89 | + LOGGER.debug(f'Message: {message}') |
| 90 | + |
| 91 | + url = f'{self.broker}/{channel}' |
| 92 | + |
| 93 | + try: |
| 94 | + response = requests.post(url, auth=self.auth, json=message) |
| 95 | + response.raise_for_status() |
| 96 | + except Exception as err: |
| 97 | + LOGGER.debug(f'Message publishing failed: {err}') |
| 98 | + |
| 99 | + def __repr__(self): |
| 100 | + return f'<HTTPPubSubClient> {self.broker_safe_url}' |
0 commit comments