Skip to content

Commit

Permalink
Add snippet responses (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
gi0baro committed Oct 7, 2024
1 parent 56f0a52 commit a8703cf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
35 changes: 32 additions & 3 deletions emmett/routing/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

from __future__ import annotations

from typing import Any, Dict, Union
from typing import Any, Dict, Union, Tuple

from emmett_core.http.response import HTTPStringResponse
from emmett_core.http.response import HTTPResponse, HTTPStringResponse
from emmett_core.routing.response import ResponseProcessor
from renoir.errors import TemplateMissingError

Expand Down Expand Up @@ -51,9 +51,31 @@ def process(
)


class SnippetResponseBuilder(ResponseProcessor):
def process(
self,
output: Tuple[str, Union[Dict[str, Any], None]],
response
) -> str:
response.headers._data['content-type'] = _html_content_type
template, output = output
base_ctx = {
'current': current,
'url': url,
'asis': asis,
'load_component': load_component
}
output = base_ctx if output is None else {**base_ctx, **output}
return self.route.app.templater._render(
template, current.request.name, output
)


class AutoResponseBuilder(ResponseProcessor):
def process(self, output: Any, response) -> str:
is_template = False
is_template, snippet = False, None
if isinstance(output, tuple):
snippet, output = output
if isinstance(output, dict):
is_template = True
output = {
Expand All @@ -66,6 +88,7 @@ def process(self, output: Any, response) -> str:
**output
}
elif output is None:
is_template = True
output = {
'current': current,
'url': url,
Expand All @@ -74,6 +97,10 @@ def process(self, output: Any, response) -> str:
}
if is_template:
response.headers._data['content-type'] = _html_content_type
if snippet:
return self.route.app.templater._render(
snippet, current.request.name, output
)
try:
return self.route.app.templater.render(
self.route.template, output
Expand All @@ -86,4 +113,6 @@ def process(self, output: Any, response) -> str:
)
elif isinstance(output, str):
return output
elif isinstance(output, HTTPResponse):
return output
return str(output)
5 changes: 3 additions & 2 deletions emmett/routing/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from emmett_core.routing.router import HTTPRouter as _HTTPRouter, WebsocketRouter as WebsocketRouter, RoutingCtx as RoutingCtx, RoutingCtxGroup as RoutingCtxGroup

from .response import AutoResponseBuilder, TemplateResponseBuilder
from .response import AutoResponseBuilder, TemplateResponseBuilder, SnippetResponseBuilder
from .rules import HTTPRoutingRule


Expand All @@ -25,7 +25,8 @@ class HTTPRouter(_HTTPRouter):
**_HTTPRouter._outputs,
**{
'auto': AutoResponseBuilder,
'template': TemplateResponseBuilder
'template': TemplateResponseBuilder,
'snippet': SnippetResponseBuilder,
}
}

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ emmett = "emmett.cli:main"
python = "^3.8"

click = ">=6.0"
emmett-core = { git = "https://github.com/emmett-framework/core", rev = "8fddde2", extras = ["granian", "rapidjson"] }
emmett-core = { git = "https://github.com/emmett-framework/core", rev = "b711bcf", extras = ["granian", "rapidjson"] }
pendulum = "~3.0.0"
pyDAL = "17.3"
pyyaml = "^6.0"
Expand Down

0 comments on commit a8703cf

Please sign in to comment.