MCPcopy
hub / github.com/encode/starlette / WebSocketRoute

Class WebSocketRoute

starlette/routing.py:293–356  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

291
292
293class WebSocketRoute(BaseRoute):
294 def __init__(
295 self,
296 path: str,
297 endpoint: Callable[..., Any],
298 *,
299 name: str | None = None,
300 middleware: Sequence[Middleware] | None = None,
301 ) -> None:
302 assert path.startswith("/"), "Routed paths must start with '/'"
303 self.path = path
304 self.endpoint = endpoint
305 self.name = get_name(endpoint) if name is None else name
306
307 endpoint_handler = endpoint
308 while isinstance(endpoint_handler, functools.partial):
309 endpoint_handler = endpoint_handler.func
310 if inspect.isfunction(endpoint_handler) or inspect.ismethod(endpoint_handler):
311 # Endpoint is function or method. Treat it as `func(websocket)`.
312 self.app = websocket_session(endpoint)
313 else:
314 # Endpoint is a class. Treat it as ASGI.
315 self.app = endpoint
316
317 if middleware is not None:
318 for cls, args, kwargs in reversed(middleware):
319 self.app = cls(self.app, *args, **kwargs)
320
321 self.path_regex, self.path_format, self.param_convertors = compile_path(path)
322
323 def matches(self, scope: Scope) -> tuple[Match, Scope]:
324 path_params: dict[str, Any]
325 if scope["type"] == "websocket":
326 route_path = get_route_path(scope)
327 match = self.path_regex.match(route_path)
328 if match:
329 matched_params = match.groupdict()
330 for key, value in matched_params.items():
331 matched_params[key] = self.param_convertors[key].convert(value)
332 path_params = dict(scope.get("path_params", {}))
333 path_params.update(matched_params)
334 child_scope = {"endpoint": self.endpoint, "path_params": path_params}
335 return Match.FULL, child_scope
336 return Match.NONE, {}
337
338 def url_path_for(self, name: str, /, **path_params: Any) -> URLPath:
339 seen_params = set(path_params.keys())
340 expected_params = set(self.param_convertors.keys())
341
342 if name != self.name or seen_params != expected_params:
343 raise NoMatchFound(name, path_params)
344
345 path, remaining_params = replace_params(self.path_format, self.param_convertors, path_params)
346 assert not remaining_params
347 return URLPath(path=path, protocol="websocket")
348
349 async def handle(self, scope: Scope, receive: Receive, send: Send) -> None:
350 await self.app(scope, receive, send)

Calls

no outgoing calls