|
56 | 56 | ) |
57 | 57 |
|
58 | 58 |
|
59 | | -def create_html_response( |
60 | | - request: Request, data: str, template_name: str |
61 | | -) -> _TemplateResponse: |
62 | | - """Create Template response.""" |
63 | | - urlpath = request.url.path |
64 | | - crumbs = [] |
65 | | - baseurl = str(request.base_url).rstrip("/") |
66 | | - crumbpath = str(baseurl) |
67 | | - for crumb in urlpath.split("/"): |
68 | | - crumbpath = crumbpath.rstrip("/") |
69 | | - part = crumb |
70 | | - if part is None or part == "": |
71 | | - part = "Home" |
72 | | - crumbpath += f"/{crumb}" |
73 | | - crumbs.append({"url": crumbpath.rstrip("/"), "part": part.capitalize()}) |
74 | | - |
75 | | - return templates.TemplateResponse( |
76 | | - f"{template_name}.html", |
77 | | - { |
78 | | - "request": request, |
79 | | - "response": json.loads(data), |
80 | | - "template": { |
81 | | - "api_root": baseurl, |
82 | | - "params": request.query_params, |
83 | | - "title": "", |
84 | | - }, |
85 | | - "crumbs": crumbs, |
86 | | - "url": str(request.url), |
87 | | - }, |
88 | | - ) |
89 | | - |
90 | | - |
91 | 59 | def create_csv_rows(data: Iterable[Dict]) -> Generator[str, None, None]: |
92 | 60 | """Creates an iterator that returns lines of csv from an iterable of dicts.""" |
93 | 61 |
|
@@ -144,6 +112,44 @@ def url_for(self, request: Request, name: str, **path_params: Any) -> str: |
144 | 112 |
|
145 | 113 | return url_path.make_absolute_url(base_url=base_url) |
146 | 114 |
|
| 115 | + def _create_html_response( |
| 116 | + self, |
| 117 | + request: Request, |
| 118 | + data: str, |
| 119 | + template_name: str, |
| 120 | + ) -> _TemplateResponse: |
| 121 | + """Create Template response.""" |
| 122 | + urlpath = request.url.path |
| 123 | + crumbs = [] |
| 124 | + baseurl = str(request.base_url).rstrip("/") |
| 125 | + |
| 126 | + crumbpath = str(baseurl) |
| 127 | + for crumb in urlpath.split("/"): |
| 128 | + crumbpath = crumbpath.rstrip("/") |
| 129 | + part = crumb |
| 130 | + if part is None or part == "": |
| 131 | + part = "Home" |
| 132 | + crumbpath += f"/{crumb}" |
| 133 | + crumbs.append({"url": crumbpath.rstrip("/"), "part": part.capitalize()}) |
| 134 | + |
| 135 | + if self.router_prefix: |
| 136 | + baseurl += self.router_prefix |
| 137 | + |
| 138 | + return templates.TemplateResponse( |
| 139 | + f"{template_name}.html", |
| 140 | + { |
| 141 | + "request": request, |
| 142 | + "response": json.loads(data), |
| 143 | + "template": { |
| 144 | + "api_root": baseurl, |
| 145 | + "params": request.query_params, |
| 146 | + "title": "", |
| 147 | + }, |
| 148 | + "crumbs": crumbs, |
| 149 | + "url": str(request.url), |
| 150 | + }, |
| 151 | + ) |
| 152 | + |
147 | 153 | def register_landing(self) -> None: |
148 | 154 | """Register landing endpoint.""" |
149 | 155 |
|
@@ -242,7 +248,7 @@ def landing( |
242 | 248 | ) |
243 | 249 |
|
244 | 250 | if output_type == MediaType.html: |
245 | | - return create_html_response( |
| 251 | + return self._create_html_response( |
246 | 252 | request, |
247 | 253 | data.json(exclude_none=True), |
248 | 254 | template_name="landing", |
@@ -291,7 +297,7 @@ def conformance( |
291 | 297 | ) |
292 | 298 |
|
293 | 299 | if output_type == MediaType.html: |
294 | | - return create_html_response( |
| 300 | + return self._create_html_response( |
295 | 301 | request, |
296 | 302 | data.json(exclude_none=True), |
297 | 303 | template_name="conformance", |
@@ -387,7 +393,7 @@ def collections( |
387 | 393 | ) |
388 | 394 |
|
389 | 395 | if output_type == MediaType.html: |
390 | | - return create_html_response( |
| 396 | + return self._create_html_response( |
391 | 397 | request, |
392 | 398 | data.json(exclude_none=True), |
393 | 399 | template_name="collections", |
@@ -469,7 +475,7 @@ def collection( |
469 | 475 | ) |
470 | 476 |
|
471 | 477 | if output_type == MediaType.html: |
472 | | - return create_html_response( |
| 478 | + return self._create_html_response( |
473 | 479 | request, |
474 | 480 | data.json(exclude_none=True), |
475 | 481 | template_name="collection", |
@@ -515,7 +521,7 @@ def queryables( |
515 | 521 | ) |
516 | 522 |
|
517 | 523 | if output_type == MediaType.html: |
518 | | - return create_html_response( |
| 524 | + return self._create_html_response( |
519 | 525 | request, |
520 | 526 | data.json(exclude_none=True), |
521 | 527 | template_name="queryables", |
@@ -772,7 +778,7 @@ async def items( |
772 | 778 |
|
773 | 779 | # HTML Response |
774 | 780 | if output_type == MediaType.html: |
775 | | - return create_html_response( |
| 781 | + return self._create_html_response( |
776 | 782 | request, |
777 | 783 | data.json(exclude_none=True), |
778 | 784 | template_name="items", |
@@ -847,7 +853,7 @@ async def item( |
847 | 853 |
|
848 | 854 | # HTML Response |
849 | 855 | if output_type == MediaType.html: |
850 | | - return create_html_response( |
| 856 | + return self._create_html_response( |
851 | 857 | request, |
852 | 858 | data.json(exclude_none=True), |
853 | 859 | template_name="item", |
|
0 commit comments