Skip to content

Commit

Permalink
Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
seba-aln committed Aug 8, 2024
1 parent 1eb2306 commit 986d341
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
21 changes: 21 additions & 0 deletions examples/encrypted_publish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from os import getenv
from pubnub.pubnub import PubNub
from pubnub.pnconfiguration import PNConfiguration
from pubnub.crypto import AesCbcCryptoModule

config = PNConfiguration()
config.publish_key = getenv('PUBLISH_KEY', 'demo')
config.subscribe_key = getenv('SUBSCRIBE_KEY', 'demo')
config.cipher_key = getenv('CIPHER_KEY', 'my_cipher_key')
config.uuid = 'example-python'
config.crypto_module = AesCbcCryptoModule(config)

pubnub = PubNub(config)

message = 'Plaintext_message'
if config.cipher_key and not config.crypto_module:
message = f'cryptodome({type(config.crypto)})'
if config.crypto_module:
message = f'crypto_module({type(config.crypto_module)})'

pubnub.publish().channel('example').message(message).sync()
16 changes: 16 additions & 0 deletions examples/fetch_messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

from os import getenv
from pubnub.pubnub import PubNub
from pubnub.pnconfiguration import PNConfiguration

config = PNConfiguration()
config.publish_key = getenv('PUBLISH_KEY', 'demo')
config.subscribe_key = getenv('SUBSCRIBE_KEY', 'demo')
config.cipher_key = getenv('CIPHER_KEY', 'my_cipher_key')
config.uuid = 'example'
config.cipher_key = "my_cipher_key"
pubnub = PubNub(config)

messages = pubnub.fetch_messages().channels('example').count(30).decrypt_messages().sync()
for msg in messages.result.channels['example']:
print(msg.message, f' !! Error during decryption: {msg.error}' if msg.error else '')
36 changes: 36 additions & 0 deletions examples/pubnub_asyncio/subscribe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import asyncio

from os import getenv
from pubnub.callbacks import SubscribeCallback
from pubnub.crypto import AesCbcCryptoModule
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub_asyncio import PubNubAsyncio

config = PNConfiguration()
config.publish_key = getenv('PUBLISH_KEY', 'demo')
config.subscribe_key = getenv('SUBSCRIBE_KEY', 'demo')
config.cipher_key = getenv('CIPHER_KEY', 'my_cipher_key')
config.crypto_module = AesCbcCryptoModule(config)
config.uuid = 'example-python'
config.enable_subscribe = True

pubnub = PubNubAsyncio(config)


class PrinterCallback(SubscribeCallback):
def status(self, pubnub, status):
print(status.category.name)

def message(self, pubnub, message):
print(message.message)


async def main():
pubnub.add_listener(PrinterCallback())
pubnub.subscribe().channels("example").execute()

await asyncio.sleep(500)


loop = asyncio.get_event_loop()
loop.run_until_complete(main())
8 changes: 4 additions & 4 deletions pubnub/pubnub_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ def uuid(self):

@property
def crypto(self) -> PubNubCryptoModule:

return self.__crypto if self.__crypto else (
self.config.crypto_module or self.config.DEFAULT_CRYPTO_MODULE(self.config) if self.config.cipher_key else None
)
crypto_module = self.__crypto or self.config.crypto_module
if not crypto_module and self.config.cipher_key:
crypto_module = self.config.DEFAULT_CRYPTO_MODULE(self.config)
return crypto_module

@crypto.setter
def crypto(self, crypto: PubNubCryptoModule):
Expand Down

0 comments on commit 986d341

Please sign in to comment.