Skip to content

Commit 7da6aca

Browse files
committed
Added hub
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent 0594f34 commit 7da6aca

File tree

5 files changed

+58
-7
lines changed

5 files changed

+58
-7
lines changed

labstack/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
from .client import Client, APIError
1+
from .client import Client, APIError
2+
from .hub import Hub

labstack/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import requests
22
from .currency import Currency
33
from .geocode import Geocode
4-
from .post import Post
4+
from .email import Email
55
from .watermark import Watermark
66
from .webpage import Webpage
77

@@ -37,8 +37,8 @@ def currency(self):
3737
def geocode(self):
3838
return Geocode(self)
3939

40-
def post(self):
41-
return Post(self)
40+
def email(self):
41+
return Email(self)
4242

4343
def watermark(self):
4444
return Watermark(self)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import requests
22

3-
class Post():
3+
class Email():
44
def __init__(self, client):
55
self.client = client
66

77
def verify(self, email):
8-
return self.client._request('GET', '/post/verify', params={'email': email})
8+
return self.client._request('GET', '/email/verify', params={'email': email})

labstack/hub.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import signal
2+
import time
3+
import paho.mqtt.client as mqtt
4+
5+
class Hub():
6+
def __init__(self, account_id, api_key, client_id=None):
7+
self.account_id = account_id
8+
self.client = mqtt.Client(client_id=client_id, clean_session=True)
9+
self.client.username_pw_set(account_id, api_key)
10+
# self.client.tls_set(ca_certs='labstack.com/cert.pem')
11+
self.handlers = {}
12+
def handler(client, userdata, msg):
13+
self.handlers[msg.topic](msg.payload)
14+
self.client.on_message = handler
15+
self._run = True
16+
signal.signal(signal.SIGINT, self._stop)
17+
signal.signal(signal.SIGTERM, self._stop)
18+
19+
def connect(self, handler=None):
20+
self.client.connect("hub.labstack.com", 1883)
21+
self.client.loop_start()
22+
def on_connect(client, userdata, flags, rc):
23+
handler()
24+
self.client.on_connect = on_connect
25+
26+
def publish(self, topic, message):
27+
self.client.publish('{}/{}'.format(self.account_id, topic), message)
28+
29+
# def subscribe(self, topic, handler, shared=False):
30+
def subscribe(self, topic, handler):
31+
topic = '{}/{}'.format(self.account_id, topic)
32+
# if shared:
33+
# topic = '$queue/' + topic
34+
self.client.subscribe(topic)
35+
self.handlers[topic] = handler
36+
37+
def unsubscribe(self, topic):
38+
self.client.unsubscribe(topic)
39+
40+
def disconnect(self):
41+
self.client.loop_stop()
42+
self.client.disconnect()
43+
44+
def run(self):
45+
while self._run:
46+
time.sleep(1)
47+
48+
def _stop(self, signum, frame):
49+
self._run = False
50+
self.disconnect()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='labstack',
5-
version='0.31.4',
5+
version='0.31.5',
66
description='Official Python client library for the LabStack platform',
77
long_description='`<https://github.com/labstack/labstack-python>`_',
88
keywords='image compress, image resize, text summary, barcode generate, barcode scan',

0 commit comments

Comments
 (0)