def __init__(self, params=None, channel_name=None, client_id=None, store=None, kubemq_address=None): """ Initializes a new instance of the Events.Channel class using params OR "Manual" Parameters. :param params: ChannelParameters to use instead of the other params :param channel_name: Represents The channel name to send to using the KubeMQ. :param client_id: Represents the sender ID that the messages will be send under. :param store: Represents if the events should be send to persistence. :param kubemq_address: The address the of the KubeMQ including the GRPC Port ,Example: "LocalHost:50000". """ if params: self.channel_name = params.channel_name self.client_id = params.client_id self.store = params.store self.kubemq_address = params.kubemq_address else: self.channel_name = channel_name self.client_id = client_id self.store = store self.kubemq_address = kubemq_address if not self.channel_name: raise ValueError("channel_name parameter is mandatory") self.sender = Sender(kubemq_address)
def create_check_connection(): sender = Sender() try: result = sender.ping() except Exception as identifier: print('error {}'.format(identifier)) exit() print(result)
def send_event_to_store(channel_name, client_id, kube_add): sender = Sender(kube_add) event = Event(metadata="EventMetaData", body=("Event Created on time %s" % datetime.datetime.utcnow()).encode('UTF-8'), store=True, channel=channel_name, client_id="EventSenderStore") event.tags = [ ('key', 'value'), ('key2', 'value2'), ] sender.send_event(event)
def send_single_event(queue_name, client_id, kube_add): sender = Sender(kube_add) event = Event(metadata="EventMetaData", body=("Event Created on time %s" % datetime.datetime.utcnow()).encode('UTF-8'), store=False, channel="MyTestChannelName", client_id="EventSender") event.tags = [ ('key', 'value'), ('key2', 'value2'), ] sender.send_event(event)
def stream_to_event_store(channel_name, client_id, kube_add): sender = Sender(kube_add) def async_streamer(): for counter in range(3): yield Event( metadata="EventMetaData", body=("Event %s Created on time %s" % (counter, datetime.datetime.utcnow())).encode('UTF-8'), store=True, channel=channel_name, client_id="EventSenderStore", ) def result_handler(result): print(result) sender.stream_event(async_streamer(), result_handler)
import datetime import json from kubemq.events.lowlevel.event import Event from kubemq.events.lowlevel.sender import Sender if __name__ == "__main__": try: publisher = Sender("localhost:50000") f = open('bookmark_events.json') data = json.load(f) for event in data: event = Event(metadata="Bookmark event metadata", body=json.dumps(event).encode('utf-8'), store=False, channel="bookmark_event_channel", client_id="bookmark_events_subscriber") print(event) res = publisher.send_event(event) print(res) f.close() except Exception as err: print("'Error when sending event:'%s'" % (err))
import datetime from kubemq.events.lowlevel.event import Event from kubemq.events.lowlevel.sender import Sender if __name__ == "__main__": print("Sending event using sender example") sender = Sender("localhost:50000") def async_streamer(): for counter in range(3): yield Event( metadata="EventMetaData", body=("Event %s Created on time %s" % (counter, datetime.datetime.utcnow())).encode('UTF-8'), store=False, channel="MyTestChannelName", client_id="EventSender", ) def result_handler(result): print(result) try: sender.stream_event(async_streamer(), result_handler) except Exception as err: print('error, error:%s' % (err))
import datetime import sys sys.path.append(".") from kubemq.events.lowlevel.event import Event from kubemq.events.lowlevel.sender import Sender if __name__ == "__main__": print("Sending event using sender example") sender = Sender("localhost:50000") event = Event(metadata="EventMetaData", body=("Event Created on time %s" % datetime.datetime.utcnow()).encode('UTF-8'), store=False, channel="MyTestChannelName", client_id="EventSender") event.tags = [ ('key', 'value'), ('key2', 'value2'), ] try: sender.send_event(event) except Exception as err: print('error, error:%s' % (err))
class Channel: """Sender with a set of predefined parameters""" sender = None def __init__(self, params=None, channel_name=None, client_id=None, store=None, kubemq_address=None): """ Initializes a new instance of the Events.Channel class using params OR "Manual" Parameters. :param params: ChannelParameters to use instead of the other params :param channel_name: Represents The channel name to send to using the KubeMQ. :param client_id: Represents the sender ID that the messages will be send under. :param store: Represents if the events should be send to persistence. :param kubemq_address: The address the of the KubeMQ including the GRPC Port ,Example: "LocalHost:50000". """ if params: self.channel_name = params.channel_name self.client_id = params.client_id self.store = params.store self.kubemq_address = params.kubemq_address else: self.channel_name = channel_name self.client_id = client_id self.store = store self.kubemq_address = kubemq_address if not self.channel_name: raise ValueError("channel_name parameter is mandatory") self.sender = Sender(kubemq_address) def __repr__(self): return "<Channel channel_name:%s client_id:%s store:%s kubemq_address:%s>" % ( self.channel_name, self.client_id, self.store, self.kubemq_address) def send_event(self, event): """Sending a new Event using KubeMQ.""" low_level_event = self.create_low_level_event(event) return self.sender.send_event(low_level_event) def stream_event(self, stream, handler=None): """Sending a new Event using KubeMQ.""" def stream_handler(): for event in stream: yield self.create_low_level_event(event) return self.sender.stream_event(stream_handler(), handler) def create_low_level_event(self, notification): return Event( channel=self.channel_name, client_id=self.client_id, store=self.store, event_id=notification.event_id, body=notification.body, metadata=notification.metadata, )
import datetime from kubemq.events.lowlevel.event import Event from kubemq.events.lowlevel.sender import Sender if __name__ == "__main__": print("Sending event using sender example") sender = Sender() event = Event(metadata="EventMetaData", body=("Event Created on time %s" % datetime.datetime.utcnow()).encode('UTF-8'), store=False, channel="MyTestChannelName", client_id="EventSender") event.tags = [ ('key', 'value'), ('key2', 'value2'), ] sender.send_event(event)
# The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import jwt from kubemq.events.lowlevel.event import Event from kubemq.events.lowlevel.sender import Sender if __name__ == "__main__": publisher = Sender("localhost:50000", encryptionHeader=jwt.encode({}, algorithm="HS256", key="some-key")) event = Event(metadata="EventMetaData", body="hello kubemq - sending single event".encode('UTF-8'), store=False, channel="testing_event_channel", client_id="hello-world-subscriber") try: res = publisher.send_event(event) print(res) except Exception as err: print("'error sending:'%s'" % (err))
import datetime from kubemq.events.lowlevel.event import Event from kubemq.events.lowlevel.sender import Sender if __name__ == "__main__": print("Sending event using sender example") sender = Sender() def async_streamer(): for counter in range(3): yield Event( metadata="EventMetaData", body=("Event %s Created on time %s" % (counter, datetime.datetime.utcnow())).encode('UTF-8'), store=False, channel="MyTestChannelName", client_id="EventSender", ) def result_handler(result): print(result) sender.stream_event(async_streamer(), result_handler)