Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit a00ed7f

Browse files
committed
Put extracting the "configuration" back into the stream.py module, and use
functools.partial to orchestrate calling the websocket request hanlder.
1 parent fd62214 commit a00ed7f

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

stream/stream.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,26 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import types
15+
import functools
1616

1717
from . import ws_client
1818

1919

20-
def stream(func, *args, **kwargs):
21-
"""Stream given API call using websocket.
22-
Extra kwarg: capture-all=True - captures all stdout+stderr for use with WSClient.read_all()"""
23-
24-
api_client = func.__self__.api_client
20+
def _websocket_reqeust(websocket_request, api_method, *args, **kwargs):
21+
"""Override the ApiClient.request method with an alternative websocket based
22+
method and call the supplied Kubernetes API method with that in place."""
23+
api_client = api_method.__self__.api_client
24+
# old generated code's api client has config. new ones has configuration
25+
try:
26+
configuration = api_client.configuration
27+
except AttributeError:
28+
configuration = api_client.config
2529
prev_request = api_client.request
2630
try:
27-
api_client.request = types.MethodType(ws_client.websocket_call, api_client)
28-
return func(*args, **kwargs)
31+
api_client.request = functools.partial(websocket_request, configuration)
32+
return api_method(*args, **kwargs)
2933
finally:
3034
api_client.request = prev_request
35+
36+
37+
stream = functools.partial(_websocket_reqeust, ws_client.websocket_call)

stream/ws_client.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -283,18 +283,9 @@ def create_websocket(configuration, url, headers=None):
283283
return websocket
284284

285285

286-
def _configuration(api_client):
287-
# old generated code's api client has config. new ones has
288-
# configuration
289-
try:
290-
return api_client.configuration
291-
except AttributeError:
292-
return api_client.config
293-
294-
295-
def websocket_call(api_client, _method, url, **kwargs):
286+
def websocket_call(configuration, _method, url, **kwargs):
296287
"""An internal function to be called in api-client when a websocket
297-
connection is required. args and kwargs are the parameters of
288+
connection is required. method, url, and kwargs are the parameters of
298289
apiClient.request method."""
299290

300291
url = get_websocket_url(url, kwargs.get("query_params"))
@@ -304,7 +295,7 @@ def websocket_call(api_client, _method, url, **kwargs):
304295
capture_all = kwargs.get("capture_all", True)
305296

306297
try:
307-
client = WSClient(_configuration(api_client), url, headers, capture_all)
298+
client = WSClient(configuration, url, headers, capture_all)
308299
if not _preload_content:
309300
return client
310301
client.run_forever(timeout=_request_timeout)

0 commit comments

Comments
 (0)