How to use the blacksheep.utils.ensure_bytes function in blacksheep

To help you get started, we’ve selected a few blacksheep examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github RobertoPrevato / BlackSheep / tests / test_utils.py View on Github external
def test_ensure_bytes(value, expected_result):
    assert ensure_bytes(value) == expected_result
github RobertoPrevato / BlackSheep / tests / test_utils.py View on Github external
def test_ensure_bytes_throws_for_invalid_value():
    with pytest.raises(ValueError):
        ensure_bytes(True)
github RobertoPrevato / BlackSheep / blacksheep / server / routing.py View on Github external
def get_match(self, method: BytesOrStr, value: BytesOrStr) -> RouteMatch:
        method = ensure_bytes(method)
        value = ensure_bytes(value)

        for route in self.routes[method]:
            match = route.match(value)
            if match:
                return match
        return RouteMatch(self._fallback, None) if self.fallback else None
github RobertoPrevato / BlackSheep / blacksheep / server / routing.py View on Github external
def add_route(self, method: BytesOrStr, route: Route):
        self.routes[ensure_bytes(method)].append(route)
github RobertoPrevato / BlackSheep / blacksheep / server / application.py View on Github external
def get_controller_handler_pattern(self, controller_type: Type, route: RegisteredRoute) -> bytes:
        """Returns the full pattern to be used for a route handler, defined as controller method."""
        base_route = getattr(controller_type, 'route', None)

        if base_route:
            if callable(base_route):
                value = base_route()
            elif isinstance(base_route, (str, bytes)):
                value = base_route
            else:
                raise RuntimeError(f'Invalid controller `route` attribute. Controller `{controller_type.__name__}` '
                                   f'has an invalid route attribute: it should be callable, or str, or bytes.')

            if value:
                return ensure_bytes(join_fragments(value, route.pattern))
        return route.pattern
github RobertoPrevato / BlackSheep / blacksheep / server / routing.py View on Github external
def add(self, method: str, pattern: BytesOrStr, handler: Any):
        self.mark_handler(handler)
        method = ensure_bytes(method)
        pattern = ensure_bytes(pattern)
        new_route = Route(pattern, handler)
        self._check_duplicate(method, new_route)
        self.add_route(method, new_route)