Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redirect response #18

Merged
merged 5 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion playground/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def html_example(_request):
@router.get('/hello')
def get_example(request):
logger.info(f'got {request=}')
return web.responses.JsonResponse({'message': 'hello, world!'})
return web.responses.RedirectResponse('/main')


@router.post('/hello', middlewares=[ExampleMiddleware])
Expand Down
20 changes: 20 additions & 0 deletions web/responses.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import Enum
from http import HTTPStatus

from webob import Response as _Response
from jinja2 import Environment, FileSystemLoader
Expand Down Expand Up @@ -26,3 +27,22 @@ def __init__(self, template_path: str, context: dict, *args, templates_dir: str
template = env.get_template(template_path)
rendered_html = template.render(context)
super().__init__(*args, body=rendered_html, content_type=ContentType.HTML.value, **kwargs)


class RedirectResponse(Response):
def __init__(
self,
location: str,
*args,
body: dict | None = None,
status: HTTPStatus = HTTPStatus.MOVED_PERMANENTLY,
**kwargs,
):
super().__init__(*args, **kwargs)
self.status = status
self.headers['Location'] = location

if body:
self.json_body = body
else:
self.body = b""
Loading