Skip to content

Commit f284dd5

Browse files
author
Sebastian Molenda
committed
Just before cleanup
1 parent 4a4872c commit f284dd5

28 files changed

+1861
-171
lines changed

pubnub/endpoints/file_operations/download_file.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from pubnub.enums import HttpMethod, PNOperationType
33
from pubnub.crypto import PubNubFileCrypto
44
from pubnub.models.consumer.file import PNDownloadFileResult
5-
from pubnub.request_handlers.requests_handler import RequestsRequestHandler
65
from pubnub.endpoints.file_operations.get_file_url import GetFileDownloadUrl
76
from warnings import warn
87
from urllib.parse import urlparse, parse_qs
@@ -86,4 +85,4 @@ def sync(self):
8685
return super(DownloadFileNative, self).sync()
8786

8887
def pn_async(self, callback):
89-
return RequestsRequestHandler(self._pubnub).async_file_based_operation(self.sync, callback, "File Download")
88+
self._pubnub.get_request_handler().async_file_based_operation(self.sync, callback, "File Download")

pubnub/endpoints/file_operations/send_file.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from pubnub.models.consumer.file import PNSendFileResult
66
from pubnub.endpoints.file_operations.publish_file_message import PublishFileMessage
77
from pubnub.endpoints.file_operations.fetch_upload_details import FetchFileUploadS3Data
8-
from pubnub.request_handlers.requests_handler import RequestsRequestHandler
98
from pubnub.endpoints.mixins import TimeTokenOverrideMixin
109
from warnings import warn
1110

@@ -152,4 +151,4 @@ def sync(self):
152151
return response_envelope
153152

154153
def pn_async(self, callback):
155-
return RequestsRequestHandler(self._pubnub).async_file_based_operation(self.sync, callback, "File Download")
154+
self._pubnub.get_request_handler().async_file_based_operation(self.sync, callback, "File Download")

pubnub/pubnub.py

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,45 @@
22
import logging
33
import threading
44

5+
from typing import Type
56
from threading import Event
67
from queue import Queue, Empty
7-
from . import utils
8-
from .request_handlers.base import BaseRequestHandler
9-
from .request_handlers.requests_handler import RequestsRequestHandler
10-
from .callbacks import SubscribeCallback, ReconnectionCallback
11-
from .endpoints.presence.heartbeat import Heartbeat
12-
from .endpoints.presence.leave import Leave
13-
from .endpoints.pubsub.subscribe import Subscribe
14-
from .enums import PNStatusCategory, PNHeartbeatNotificationOptions, PNOperationType, PNReconnectionPolicy
15-
from .managers import SubscriptionManager, PublishSequenceManager, ReconnectionManager, TelemetryManager
16-
from .models.consumer.common import PNStatus
17-
from .pnconfiguration import PNConfiguration
18-
from .pubnub_core import PubNubCore
19-
from .structures import PlatformOptions
20-
from .workers import SubscribeMessageWorker
8+
from pubnub import utils
9+
from pubnub.request_handlers.base import BaseRequestHandler
10+
from pubnub.request_handlers.httpx import HttpxRequestHandler
11+
from pubnub.callbacks import SubscribeCallback, ReconnectionCallback
12+
from pubnub.endpoints.presence.heartbeat import Heartbeat
13+
from pubnub.endpoints.presence.leave import Leave
14+
from pubnub.endpoints.pubsub.subscribe import Subscribe
15+
from pubnub.enums import PNStatusCategory, PNHeartbeatNotificationOptions, PNOperationType, PNReconnectionPolicy
16+
from pubnub.managers import SubscriptionManager, PublishSequenceManager, ReconnectionManager, TelemetryManager
17+
from pubnub.models.consumer.common import PNStatus
18+
from pubnub.pnconfiguration import PNConfiguration
19+
from pubnub.pubnub_core import PubNubCore
20+
from pubnub.structures import PlatformOptions
21+
from pubnub.workers import SubscribeMessageWorker
2122

2223
logger = logging.getLogger("pubnub")
2324

2425

2526
class PubNub(PubNubCore):
2627
"""PubNub Python API"""
2728

28-
def __init__(self, config):
29+
def __init__(self, config: PNConfiguration, *, custom_request_handler: Type[BaseRequestHandler] = None):
30+
""" PubNub instance constructor
31+
32+
Parameters:
33+
config (PNConfiguration): PNConfiguration instance (required)
34+
custom_request_handler (BaseRequestHandler): Custom request handler class (optional)
35+
36+
"""
2937
assert isinstance(config, PNConfiguration)
3038
PubNubCore.__init__(self, config)
31-
self._request_handler = RequestsRequestHandler(self)
39+
40+
if isinstance(custom_request_handler, BaseRequestHandler):
41+
self._request_handler = custom_request_handler(self)
42+
else:
43+
self._request_handler = HttpxRequestHandler(self)
3244

3345
if self.config.enable_subscribe:
3446
self._subscription_manager = NativeSubscriptionManager(self)
@@ -40,10 +52,22 @@ def __init__(self, config):
4052
def sdk_platform(self):
4153
return ""
4254

43-
def set_request_handler(self, handler):
55+
def set_request_handler(self, handler: BaseRequestHandler):
56+
"""Set custom request handler
57+
58+
Parametrers:
59+
handler (BaseRequestHandler): Instance of custom request handler
60+
"""
4461
assert isinstance(handler, BaseRequestHandler)
4562
self._request_handler = handler
4663

64+
def get_request_handler(self) -> BaseRequestHandler:
65+
"""Get instance of request handler
66+
67+
Return: handler(BaseRequestHandler): Instance of request handler
68+
"""
69+
return self._request_handler
70+
4771
def request_sync(self, endpoint_call_options):
4872
platform_options = PlatformOptions(self.headers, self.config)
4973

pubnub/pubnub_asyncio.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
from pubnub.endpoints.presence.leave import Leave
1919
from pubnub.endpoints.pubsub.subscribe import Subscribe
2020
from pubnub.pubnub_core import PubNubCore
21+
from pubnub.request_handlers.base import BaseRequestHandler
22+
from pubnub.request_handlers.httpx import HttpxRequestHandler
23+
from pubnub.request_handlers.requests import RequestsRequestHandler
2124
from pubnub.workers import SubscribeMessageWorker
2225
from pubnub.managers import SubscriptionManager, PublishSequenceManager, ReconnectionManager, TelemetryManager
2326
from pubnub import utils
@@ -46,7 +49,7 @@ class PubNubAsyncio(PubNubCore):
4649
PubNub Python SDK for asyncio framework
4750
"""
4851

49-
def __init__(self, config, custom_event_loop=None, subscription_manager=None):
52+
def __init__(self, config, custom_event_loop=None, subscription_manager=None, *, custom_request_handler=None):
5053
super(PubNubAsyncio, self).__init__(config)
5154
self.event_loop = custom_event_loop or asyncio.get_event_loop()
5255

@@ -55,6 +58,11 @@ def __init__(self, config, custom_event_loop=None, subscription_manager=None):
5558

5659
self._connector = PubNubAsyncHTTPTransport()
5760

61+
if isinstance(custom_request_handler, BaseRequestHandler):
62+
self._request_handler = custom_request_handler(self)
63+
else:
64+
self._request_handler = RequestsRequestHandler(self)
65+
5866
if not subscription_manager:
5967
subscription_manager = EventEngineSubscriptionManager
6068

pubnub/request_handlers/requests_handler.py renamed to pubnub/request_handlers/httpx.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,12 @@
2323
logger = logging.getLogger("pubnub")
2424

2525

26-
class RequestsRequestHandler(BaseRequestHandler):
26+
class HttpxRequestHandler(BaseRequestHandler):
2727
""" PubNub Python SDK Native requests handler based on `requests` HTTP library. """
2828
ENDPOINT_THREAD_COUNTER: int = 0
2929

3030
def __init__(self, pubnub):
3131
self.session = httpx.Client()
32-
# self.session = Session()
33-
34-
# self.session.mount('http://%s' % pubnub.config.origin, HTTPAdapter(max_retries=1, pool_maxsize=500))
35-
# self.session.mount('https://%s' % pubnub.config.origin, HTTPAdapter(max_retries=1, pool_maxsize=500))
36-
# self.session.mount('http://%s/v2/subscribe' % pubnub.config.origin, HTTPAdapter(pool_maxsize=500))
37-
# self.session.mount('https://%s/v2/subscribe' % pubnub.config.origin, HTTPAdapter(pool_maxsize=500))
3832

3933
self.pubnub = pubnub
4034

@@ -89,11 +83,11 @@ def execute_callback_in_separate_thread(
8983
):
9084
client = AsyncHTTPClient(callback_to_invoke_in_another_thread)
9185

92-
RequestsRequestHandler.ENDPOINT_THREAD_COUNTER += 1
86+
HttpxRequestHandler.ENDPOINT_THREAD_COUNTER += 1
9387

9488
thread = threading.Thread(
9589
target=client.run,
96-
name=f"Thread-{operation_name}-{RequestsRequestHandler.ENDPOINT_THREAD_COUNTER}",
90+
name=f"Thread-{operation_name}-{HttpxRequestHandler.ENDPOINT_THREAD_COUNTER}",
9791
daemon=self.pubnub.config.daemon
9892
).start()
9993

0 commit comments

Comments
 (0)