Skip to content

Commit

Permalink
Pubnub 4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-konshin committed Feb 7, 2017
1 parent 2d721dd commit cace0e1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 32 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
observable>=0.3.1
pubnub==3.*
pubnub==4.*
pycryptodome>=3.4.4
requests>=2.12.4
63 changes: 33 additions & 30 deletions ringcentral/subscription/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,34 +148,37 @@ def _subscribe_at_pubnub(self):
if not self.alive():
raise Exception('Subscription is not alive')

from pubnub import Pubnub

s_key = self._subscription['deliveryMode']['subscriberKey']
self._pubnub = Pubnub(subscribe_key=s_key, ssl_on=False, publish_key='')

def callback(message, channel=''):
self._notify(message)

def error(message):
self.trigger(Events.connectionError, message)

def connect(message):
pass

def reconnect(message):
pass

def disconnect(message):
pass

self._pubnub.subscribe(
self._subscription['deliveryMode']['address'],
callback=callback,
error=error,
connect=connect,
reconnect=reconnect,
disconnect=disconnect
)
from pubnub.pubnub import PubNub
from pubnub.pnconfiguration import PNConfiguration
from pubnub.callbacks import SubscribeCallback
from pubnub.enums import PNStatusCategory

pnconf = PNConfiguration()
pnconf.subscribe_key = self._subscription['deliveryMode']['subscriberKey']
self._pubnub = PubNub(pnconf)

subscription = self

class SubscribeCallbackImpl(SubscribeCallback):
def presence(self, pubnub, presence):
pass # handle incoming presence data

def status(self, pubnub, status):
if status.category == PNStatusCategory.PNUnexpectedDisconnectCategory:
subscription.trigger(Events.connectionError, 'Connectivity loas')
pass
elif status.category == PNStatusCategory.PNConnectedCategory:
pass
elif status.category == PNStatusCategory.PNReconnectedCategory:
pass
elif status.category == PNStatusCategory.PNDecryptionErrorCategory:
pass

def message(self, pubnub, pnmessage): # instance of PNMessageResult
subscription._notify(pnmessage.message)

self._pubnub.add_listener(SubscribeCallbackImpl())
self._pubnub.subscribe().channels(self._subscription['deliveryMode']['address']).execute()

def _notify(self, message):
message = self._decrypt(message)
Expand All @@ -185,7 +188,7 @@ def _decrypt(self, message):
if not self.alive():
raise Exception('Subscription is not alive')

from pubnub import AES
from Crypto.Cipher import AES

delivery_mode = self._subscription['deliveryMode']
is_encrypted = ('encryption' in delivery_mode) and ('encryptionKey' in delivery_mode)
Expand All @@ -201,7 +204,7 @@ def _decrypt(self, message):

def _unsubscribe_at_pubnub(self):
if self._pubnub and self.alive():
self._pubnub.unsubscribe(self._subscription['deliveryMode']['address'])
self._pubnub.unsubscribe().channels(self._subscription['deliveryMode']['address']).execute()

def _get_full_events_filter(self):
return [self._platform.create_url(e) for e in self._event_filters]
Expand Down
8 changes: 8 additions & 0 deletions ringcentral/test/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def get_sdk(self, mock):
text=''
)

matcher = re.compile('ps\.pndsn\.com')

mock.register_uri(
method=requests_mock.ANY,
url=matcher,
text=''
)

return sdk

def add(self, mock, method, url, body, status=200):
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
keywords=['sdk', 'ringcentral', 'connect', 'platform', 'api', 'python'],
install_requires=[
'observable>=0.3.1',
'pubnub==3.*',
'pubnub==4.*',
'pycryptodome>=3.4.4',
'requests>=2.7.0'
],
classifiers=[]
Expand Down

0 comments on commit cace0e1

Please sign in to comment.