Skip to content

Commit d9dbc83

Browse files
committed
safeguard links
1 parent 17e2920 commit d9dbc83

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

pywis_pubsub/message.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def get_data(msg_dict: dict, verify_certs=True) -> bytes:
6464
:returns: `bytes` of data
6565
"""
6666

67-
link = get_link(msg_dict['links'])
67+
link = get_link(msg_dict.get('links', []))
6868

6969
if link:
7070
LOGGER.debug(f'Found link: {link}')

pywis_pubsub/mqtt.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
class MQTTPubSubClient:
3636
"""MQTT PubSub client"""
37-
def __init__(self, broker: str, options: dict = {}) -> None:
37+
def __init__(self, broker: str, options: dict = {}, conn=None) -> None:
3838
"""
3939
PubSub initializer
4040
@@ -62,6 +62,11 @@ def __init__(self, broker: str, options: dict = {}) -> None:
6262
if self.broker_url.scheme in ['ws', 'wss']:
6363
transport = 'websockets'
6464

65+
if conn is not None:
66+
LOGGER.debug(f'Using existing connection {conn}')
67+
self.conn = conn
68+
return
69+
6570
msg = f'Connecting to broker {self.broker_safe_url} with id {self.client_id}' # noqa
6671
LOGGER.debug(msg)
6772
self.conn = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION2,

pywis_pubsub/subscribe.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def on_message_handler(client, userdata, msg):
7070
LOGGER.debug('Message geometry not within bbox; skipping')
7171
return
7272

73-
clink = get_link(msg_dict['links'])
73+
clink = get_link(msg_dict.get('links', []))
7474
if not clink:
7575
LOGGER.warning('No valid data link found')
7676
return
@@ -106,7 +106,7 @@ def on_message_handler(client, userdata, msg):
106106
filepath = userdata['storage']['options'].get('filepath', 'data_id')
107107
LOGGER.debug(f'Using {filepath} for naming filepath')
108108

109-
link = get_link(msg_dict['links'])
109+
link = get_link(msg_dict.get('links', []))
110110

111111
if filepath == 'link':
112112
LOGGER.debug('Using link as filepath')

pywis_pubsub/verification.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def verify_data(instance: dict, verify_certs: bool = True) -> bool:
9797
if 'content' in instance['properties']:
9898
size = instance['properties']['content']['size']
9999
else:
100-
size = get_link(instance['links'])['length']
100+
size = get_link(instance.get('links', [])).get('length')
101101

102102
LOGGER.debug(f'size: {size}')
103103
return data_verified(data, size, method, value)

0 commit comments

Comments
 (0)