22import logging
33import threading
44
5+ from typing import Type
56from threading import Event
67from 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
2223logger = logging .getLogger ("pubnub" )
2324
2425
2526class 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
0 commit comments