@staticmethod
def bakery(pages: dict, page_name: str, headers: dict, events: dict, has_to_return_data: bool=False) -> tuple[bytes, HTTPDStatus]:
"""Process received events and bake the given page.
:param pages: (dict) A dictionary with key=page_name and value=ResponseRecipe
:param page_name: (str) The current page name
:param headers: (dict) The headers of the http request
:param events: (dict) The events extract from the request (only for POST request, send empty dict for GET)
:param has_to_return_data: (bool) False by default, Boolean to know if we have to return the html page or data
:return: (tuple[bytes, HTTPDStatus]) The content to answer to the client and the status of the response
"""
response = pages.get(page_name)
if response is None:
status = HTTPDStatus(404)
return (status.to_html(encode=True, msg_error=f'Page not found: {page_name}'), status)
content = bytes(response.bake(events, headers=headers), 'utf-8')
if has_to_return_data is True:
content = response.get_data()
if isinstance(content, (bytes, bytearray)) is False:
content = bytes(content, 'utf-8')
response.reset_data()
status = response.status
if isinstance(status, int):
status = HTTPDStatus(status)
elif hasattr(status, 'code') is False:
raise TypeError(f'The status has to be an instance of HTTPDStatus or int.Got {status} instead.')
return (content, status)