How to use the emmett.ctx.current function in emmett

To help you get started, we’ve selected a few emmett 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 emmett-framework / emmett / emmett / routing / router.py View on Github external
async def dispatch(self):
        request, response = current.request, current.response
        route, reqargs = self.match(request)
        if not route:
            raise HTTP(404, body="Resource not found\n")
        request.name = route.name
        http_cls, output = await route.dispatch(request, reqargs)
        return http_cls(
            response.status, output, response.headers, response.cookies)
github emmett-framework / emmett / emmett / asgi / handlers.py View on Github external
async def dynamic_handler(self, scope, receive, send):
        await self.app._router_ws.dispatch()
        if current.websocket._accepted:
            await send({'type': 'websocket.close', 'code': 1000})
github emmett-framework / emmett / emmett / routing / urls.py View on Github external
except KeyError:
                raise RuntimeError(f'invalid url("{path}",...)')
        # handle classic urls
        else:
            builder = UrlBuilder([path])
        # add language
        lang = None
        if current.app.language_force_on_url:
            if language:
                #: use the given language if is enabled in application
                if language in current.app.languages:
                    lang = language
            else:
                #: try to use the request language if context exists
                if hasattr(current, 'request'):
                    lang = current.request.language
            if lang == current.app.language_default:
                lang = None
        # scheme=True means to use current scheme
        if scheme is True:
            if not hasattr(current, 'request'):
                raise RuntimeError(
                    f'cannot build url("{path}",...) without current request'
                )
            scheme = self.http_to_ws_schemes[current.request.scheme]
        # add scheme and host
        if scheme:
            if host is None:
                if not hasattr(current, 'request'):
                    raise RuntimeError(
                        f'cannot build url("{path}",...) '
                        'without current request'
github emmett-framework / emmett / emmett / http.py View on Github external
def redirect(location, status_code=303):
    current.response.status = status_code
    raise HTTPRedirect(status_code, location)