#-----------------------------------------------------------------------------# Copyright (c) Anaconda, Inc., and Bokeh Contributors.# All rights reserved.## The full license is in the file LICENSE.txt, distributed with this software.#-----------------------------------------------------------------------------''' Internal utility functions used by ``bokeh.client``'''#-----------------------------------------------------------------------------# Boilerplate#-----------------------------------------------------------------------------from__future__importannotationsimportlogging# isort:skiplog=logging.getLogger(__name__)#-----------------------------------------------------------------------------# Imports#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Globals and constants#-----------------------------------------------------------------------------__all__=('server_url_for_websocket_url','websocket_url_for_server_url',)#-----------------------------------------------------------------------------# General API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Dev API#-----------------------------------------------------------------------------
[docs]defserver_url_for_websocket_url(url:str)->str:''' Convert an ``ws(s)`` URL for a Bokeh server into the appropriate ``http(s)`` URL for the websocket endpoint. Args: url (str): An ``ws(s)`` URL ending in ``/ws`` Returns: str: The corresponding ``http(s)`` URL. Raises: ValueError: If the input URL is not of the proper form. '''ifurl.startswith("ws:"):reprotocoled="http"+url[2:]elifurl.startswith("wss:"):reprotocoled="https"+url[3:]else:raiseValueError("URL has non-websocket protocol "+url)ifnotreprotocoled.endswith("/ws"):raiseValueError("websocket URL does not end in /ws")returnreprotocoled[:-2]
[docs]defwebsocket_url_for_server_url(url:str)->str:''' Convert an ``http(s)`` URL for a Bokeh server websocket endpoint into the appropriate ``ws(s)`` URL Args: url (str): An ``http(s)`` URL Returns: str: The corresponding ``ws(s)`` URL ending in ``/ws`` Raises: ValueError: If the input URL is not of the proper form. '''ifurl.startswith("http:"):reprotocoled="ws"+url[4:]elifurl.startswith("https:"):reprotocoled="wss"+url[5:]else:raiseValueError("URL has unknown protocol "+url)ifreprotocoled.endswith("/"):returnreprotocoled+"ws"else:returnreprotocoled+"/ws"