25
25
from io import BytesIO
26
26
27
27
from ibm_cloud_sdk_core .authenticators import Authenticator
28
- from requests import Response
28
+ from requests import Response , Session
29
29
from requests .cookies import RequestsCookieJar
30
30
31
31
from .common import get_sdk_headers
@@ -72,12 +72,10 @@ def __hash__(self):
72
72
# Since Py3.6 dict is ordered so use a key only dict for our set
73
73
rules_by_operation .setdefault (operation_id , dict ()).setdefault (rule )
74
74
75
-
76
- old_init = CloudantV1 .__init__
77
-
75
+ _old_init = CloudantV1 .__init__
78
76
79
77
def new_init (self , authenticator : Authenticator = None ):
80
- old_init (self , authenticator )
78
+ _old_init (self , authenticator )
81
79
# Overwrite default read timeout to 2.5 minutes
82
80
if not ('timeout' in self .http_config ):
83
81
new_http_config = self .http_config .copy ()
@@ -88,27 +86,22 @@ def new_init(self, authenticator: Authenticator = None):
88
86
# Replacing BaseService's http.cookiejar.CookieJar as RequestsCookieJar supports update(CookieJar)
89
87
self .jar = RequestsCookieJar (self .jar )
90
88
self .authenticator .set_jar (self .jar ) # Authenticators don't have access to cookie jars by default
91
- response_hooks = self .get_http_client ().hooks ['response' ]
92
- if _error_response_hook not in response_hooks :
93
- response_hooks .append (_error_response_hook )
94
-
95
- old_set_service_url = CloudantV1 .set_service_url
89
+ add_hooks (self )
96
90
91
+ _old_set_service_url = CloudantV1 .set_service_url
97
92
98
93
def new_set_service_url (self , service_url : str ):
99
- old_set_service_url (self , service_url )
94
+ _old_set_service_url (self , service_url )
100
95
try :
101
96
if isinstance (self .authenticator , CouchDbSessionAuthenticator ):
102
97
self .authenticator .token_manager .set_service_url (service_url )
103
98
except AttributeError :
104
99
pass # in case no authenticator is configured yet, pass
105
100
106
-
107
- old_set_default_headers = CloudantV1 .set_default_headers
108
-
101
+ _old_set_default_headers = CloudantV1 .set_default_headers
109
102
110
103
def new_set_default_headers (self , headers : Dict [str , str ]):
111
- old_set_default_headers (self , headers )
104
+ _old_set_default_headers (self , headers )
112
105
if isinstance (self .authenticator , CouchDbSessionAuthenticator ):
113
106
combined_headers = {}
114
107
combined_headers .update (headers )
@@ -119,12 +112,13 @@ def new_set_default_headers(self, headers: Dict[str, str]):
119
112
)
120
113
self .authenticator .token_manager .set_default_headers (combined_headers )
121
114
115
+ _old_set_disable_ssl_verification = CloudantV1 .set_disable_ssl_verification
122
116
123
- old_set_disable_ssl_verification = CloudantV1 . set_disable_ssl_verification
124
-
125
-
117
+ # Note this is currently unused, but probably should be enabled.
118
+ # To enable it we need to resolve whether CouchDbSessionAuthenticator
119
+ # should ever be allowed to have a different value from the service client.
126
120
def new_set_disable_ssl_verification (self , status : bool = False ) -> None :
127
- old_set_disable_ssl_verification (self , status )
121
+ _old_set_disable_ssl_verification (self , status )
128
122
if isinstance (self .authenticator , CouchDbSessionAuthenticator ):
129
123
self .authenticator .token_manager .set_disable_ssl_verification (status )
130
124
@@ -193,7 +187,7 @@ def _error_response_hook(response:Response, *args, **kwargs) -> Optional[Respons
193
187
pass
194
188
return response
195
189
196
- old_prepare_request = CloudantV1 .prepare_request
190
+ _old_prepare_request = CloudantV1 .prepare_request
197
191
198
192
def new_prepare_request (self ,
199
193
method : str ,
@@ -229,4 +223,15 @@ def new_prepare_request(self,
229
223
if segment_to_validate .startswith ('_' ):
230
224
raise ValueError ('{0} {1} starts with the invalid _ character.' .format (rule .error_parameter_name ,
231
225
unquote (segment_to_validate )))
232
- return old_prepare_request (self , method , url , * args , headers = headers , params = params , data = data , files = files , ** kwargs )
226
+ return _old_prepare_request (self , method , url , * args , headers = headers , params = params , data = data , files = files , ** kwargs )
227
+
228
+ def add_hooks (self ):
229
+ response_hooks = self .get_http_client ().hooks ['response' ]
230
+ if _error_response_hook not in response_hooks :
231
+ response_hooks .append (_error_response_hook )
232
+
233
+ _old_set_http_client = CloudantV1 .set_http_client
234
+
235
+ def new_set_http_client (self , http_client : Session ) -> None :
236
+ _old_set_http_client (self , http_client )
237
+ add_hooks (self )
0 commit comments