예제 #1
0
 def url_path_for(self, name: str, **path_params: str) -> URLPath:
     if self.name is not None and name == self.name and "path" in path_params:
         # 'name' matches "<mount_name>".
         path_params["path"] = path_params["path"].lstrip("/")
         path, remaining_params = replace_params(self.path_format,
                                                 self.param_convertors,
                                                 path_params)
         if not remaining_params:
             return URLPath(path=path, protocol="http")
     elif self.name is None or name.startswith(self.name + ":"):
         if self.name is None:
             # No mount name.
             remaining_name = name
         else:
             # 'name' matches "<mount_name>:<child_name>".
             remaining_name = name[len(self.name) + 1:]
         path_params["path"] = ""
         path, remaining_params = replace_params(self.path_format,
                                                 self.param_convertors,
                                                 path_params)
         for route in self.routes or []:
             try:
                 url = route.url_path_for(remaining_name,
                                          **remaining_params)
                 return URLPath(path=path.rstrip("/") + str(url),
                                protocol=url.protocol)
             except NoMatchFound as exc:
                 pass
     raise NoMatchFound()
예제 #2
0
 def url_path_for(self, *args: str, **kwargs: str) -> URLPath:
     assert len(
         args) == 1, "Sole positional argument must be the route name."
     name = args[0]
     if self.name is not None and name == self.name and "path" in kwargs:
         # 'name' matches "<mount_name>".
         path = kwargs.pop("path")
         host, remaining_params = replace_params(self.host_format,
                                                 self.param_convertors,
                                                 kwargs)
         if not remaining_params:
             return URLPath(path=path, host=host)
     elif self.name is None or name.startswith(self.name + ":"):
         if self.name is None:
             # No mount name.
             remaining_name = name
         else:
             # 'name' matches "<mount_name>:<child_name>".
             remaining_name = name[len(self.name) + 1:]
         host, remaining_params = replace_params(self.host_format,
                                                 self.param_convertors,
                                                 kwargs)
         for route in self.routes or []:
             try:
                 url = route.url_path_for(remaining_name,
                                          **remaining_params)
                 return URLPath(path=str(url),
                                protocol=url.protocol,
                                host=host)
             except NoMatchFound:
                 pass
     raise NoMatchFound()
예제 #3
0
파일: routing.py 프로젝트: encode/starlette
 def url_path_for(self, name: str, **path_params: typing.Any) -> URLPath:
     if self.name is not None and name == self.name and "path" in path_params:
         # 'name' matches "<mount_name>".
         path = path_params.pop("path")
         host, remaining_params = replace_params(
             self.host_format, self.param_convertors, path_params
         )
         if not remaining_params:
             return URLPath(path=path, host=host)
     elif self.name is None or name.startswith(self.name + ":"):
         if self.name is None:
             # No mount name.
             remaining_name = name
         else:
             # 'name' matches "<mount_name>:<child_name>".
             remaining_name = name[len(self.name) + 1 :]
         host, remaining_params = replace_params(
             self.host_format, self.param_convertors, path_params
         )
         for route in self.routes or []:
             try:
                 url = route.url_path_for(remaining_name, **remaining_params)
                 return URLPath(path=str(url), protocol=url.protocol, host=host)
             except NoMatchFound:
                 pass
     raise NoMatchFound(name, path_params)
예제 #4
0
def generate_url(request: Request, *args: str, protocol: str = "http") -> str:
    assert protocol in ["http", "ws"]
    path = "/".join(args)
    if path and path[0] != "/":
        path = "/" + path
    if not protocol:
        protocol = request.url.scheme
    return URLPath(path, protocol).make_absolute_url(get_base_url(request))
예제 #5
0
def get_datum_download_url(request: Request, id: str = Path()):
    output = Output.from_cache(id)
    if not output:
        return JSONResponse({"error": "Output not found in cache"}, 404)
    url = URLPath(
        output.download_url).make_absolute_url(base_url=request.base_url)
    return {
        "url": url,
    }
예제 #6
0
    def url_path_for(self, name: str, **path_params: str) -> URLPath:
        seen_params = set(path_params.keys())
        expected_params = set(self.param_convertors.keys())

        if name != self.name or seen_params != expected_params:
            raise NoMatchFound()

        path, remaining_params = replace_params(self.path_format,
                                                self.param_convertors,
                                                path_params)
        assert not remaining_params
        return URLPath(path=path, protocol="websocket")
예제 #7
0
    def url_path_for(self, *args: str, **kwargs: str) -> URLPath:
        assert len(
            args) == 1, "Sole positional argument must be the route name."
        seen_params = set(kwargs.keys())
        expected_params = set(self.param_convertors.keys())

        if args[0] != self.name or seen_params != expected_params:
            raise NoMatchFound()

        path, remaining_params = replace_params(self.path_format,
                                                self.param_convertors, kwargs)
        assert not remaining_params
        return URLPath(path=path, protocol="websocket")
예제 #8
0
 def url_path_for(self, name: str, **path_params: str) -> URLPath:
     if name != self.name or self.param_names != set(path_params.keys()):
         raise NoMatchFound()
     path, remaining_params = replace_params(self.path, path_params)
     assert not remaining_params
     return URLPath(path=path, protocol="websocket")