async def call(url, method, **kwargs): scheme, host, port, path = util.parse_url(url) if path is None: path = "/" req = HTTPRequest.build(method, path) for var in REQUEST_ARGS: if var in kwargs: setattr(req, var, kwargs.pop(var)) if "Host" not in req.headers: req.headers["Host"] = util.make_url(None, host, port, None) url = util.make_url(scheme, host, port, None) return await request(url, req, **kwargs)
async def connect(url, context=None, *, protocols=None): logger.debug("Connecting WS client to %s", url) scheme, host, port, path = util.parse_url(url) if scheme == "ws": context = None elif scheme == "wss": if context is None: context = tls.TLSContext() elif scheme is not None: raise ValueError("Invalid WS url scheme: %s" % scheme) if path is None: path = "/" server = util.make_url(None, host, port, None) async with http.connect(server, context) as client: async with util.create_task_group() as group: async with WebSocketClient(client, group) as client: await client.start_client(host, path, protocols) yield client await client.close() logger.debug("WS client is closed")
def test_make_url(): assert util.make_url(None, "example.com", None, None) == "example.com" assert util.make_url("kdp", "example.com", 8080, "/test") == "kdp://example.com:8080/test"