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

Method __init__

starlette/routing.py:200–242  ·  view source on GitHub ↗
(
        self,
        path: str,
        endpoint: Callable[..., Any],
        *,
        methods: Collection[str] | None = None,
        name: str | None = None,
        include_in_schema: bool = True,
        middleware: Sequence[Middleware] | None = None,
        max_body_size: int | None = None,
    )

Source from the content-addressed store, hash-verified

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]

Callers

nothing calls this directly

Calls 4

get_nameFunction · 0.85
request_responseFunction · 0.85
compile_pathFunction · 0.85

Tested by

no test coverage detected