MCPcopy Create free account
hub / github.com/encode/starlette / Route

Class Route

starlette/routing.py:199–296  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

197
198
199class Route(BaseRoute):
200 def __init__(
201 self,
202 path: str,
203 endpoint: Callable[..., Any],
204 *,
205 methods: Collection[str] | None = None,
206 name: str | None = None,
207 include_in_schema: bool = True,
208 middleware: Sequence[Middleware] | None = None,
209 max_body_size: int | None = None,
210 ) -> None:
211 assert path.startswith("/"), "Routed paths must start with '/'"
212 self.path = path
213 self.endpoint = endpoint
214 self.name = get_name(endpoint) if name is None else name
215 self.include_in_schema = include_in_schema
216
217 endpoint_handler = endpoint
218 while isinstance(endpoint_handler, functools.partial):
219 endpoint_handler = endpoint_handler.func
220 if inspect.isfunction(endpoint_handler) or inspect.ismethod(endpoint_handler):
221 # Endpoint is function or method. Treat it as `func(request) -> response`.
222 self.app = request_response(endpoint)
223 if methods is None:
224 methods = ["GET"]
225 else:
226 # Endpoint is a class. Treat it as ASGI.
227 self.app = endpoint
228
229 if middleware is not None:
230 for cls, args, kwargs in reversed(middleware):
231 self.app = cls(self.app, *args, **kwargs)
232 if max_body_size is not None:
233 self.app = RequestBodyLimitMiddleware(self.app, max_body_size=max_body_size)
234
235 if methods is None:
236 self.methods = None
237 else:
238 self.methods = {method.upper() for method in methods}
239 if "GET" in self.methods:
240 self.methods.add("HEAD")
241
242 self.path_regex, self.path_format, self.param_convertors = compile_path(path)
243
244 def matches(self, scope: Scope) -> tuple[Match, Scope]:
245 path_params: dict[str, Any]
246 if scope["type"] == "http":
247 route_path = get_route_path(scope)
248 match = self.path_regex.match(route_path)
249 if match:
250 matched_params = match.groupdict()
251 for key, value in matched_params.items():
252 matched_params[key] = self.param_convertors[key].convert(value)
253 path_params = dict(scope.get("path_params", {}))
254 path_params.update(matched_params)
255 child_scope = {"endpoint": self.endpoint, "path_params": path_params}
256 if self.methods and scope["method"] not in self.methods:

Callers 15

test_schemas.pyFile · 0.90
test_templatesFunction · 0.90
test_routesFunction · 0.90
test_app_debugFunction · 0.90
test_app_add_routeFunction · 0.90
test_endpoints.pyFile · 0.90

Calls

no outgoing calls