Skip to content

Response classes

There are several custom response classes you can use to create an instance and return them directly from your handler.

You can import them directly from nexify.responses:

from nexify.responses import (
    Response,
    HttpResponse,
    JSONResponse,
    PlainTextResponse,
    HTMLResponse,
    RedirectResponse
)

Responses

nexify.responses.Response

nexify.responses.HttpResponse

HttpResponse(
    content=None,
    status_code=HTTP_200_OK,
    headers=None,
    media_type=None,
)

Bases: Response

Response class that returns a valid API Gateway response. It is not only for HTTP API Gateway, but also for REST API Gateway.

Source code in nexify/responses.py
22
23
24
25
26
27
28
29
30
31
32
33
def __init__(
    self,
    content: Any = None,
    status_code: int | None = status.HTTP_200_OK,
    headers: Mapping[str, str] | None = None,
    media_type: str | None = None,
):
    self.status_code = status_code or self.status_code
    self.media_type = media_type or self.media_type
    self.headers = headers or {}
    self.content = content
    self.response = self.render()

charset class-attribute instance-attribute

charset = 'utf-8'

status_code class-attribute instance-attribute

status_code = status_code or status_code

media_type class-attribute instance-attribute

media_type = media_type or media_type

headers instance-attribute

headers = headers or {}

content instance-attribute

content = content

response instance-attribute

response = render()

render

render()
Source code in nexify/responses.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def render(self) -> dict[str, Any]:
    content = jsonable_encoder(self.content)
    content = self.content_converter(content)

    result = {
        "statusCode": self.status_code,
        "body": content,
        "headers": {
            "content-type": f"{self.media_type}; charset={self.charset}",
        },
    }
    if self.media_type is None and content is None:
        del result["body"]  # type: ignore[union-attr]
        del result["headers"]["content-type"]  # type: ignore[union-attr]
    result["headers"].update(self.headers)  # type: ignore[union-attr]
    return result

content_converter

content_converter(content)
Source code in nexify/responses.py
52
53
def content_converter(self, content: Any) -> Any:
    return self.content

nexify.responses.JSONResponse

JSONResponse(
    content=None,
    status_code=HTTP_200_OK,
    headers=None,
    media_type=None,
)

Bases: HttpResponse

Source code in nexify/responses.py
59
60
61
62
63
64
65
66
def __init__(
    self,
    content: Any = None,
    status_code: int | None = status.HTTP_200_OK,
    headers: Mapping[str, str] | None = None,
    media_type: str | None = None,
):
    super().__init__(content=content, status_code=status_code, headers=headers, media_type=media_type)

charset class-attribute instance-attribute

charset = 'utf-8'

status_code class-attribute instance-attribute

status_code = status_code or status_code

headers instance-attribute

headers = headers or {}

content instance-attribute

content = content

response instance-attribute

response = render()

media_type class-attribute instance-attribute

media_type = 'application/json'

render

render()
Source code in nexify/responses.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def render(self) -> dict[str, Any]:
    content = jsonable_encoder(self.content)
    content = self.content_converter(content)

    result = {
        "statusCode": self.status_code,
        "body": content,
        "headers": {
            "content-type": f"{self.media_type}; charset={self.charset}",
        },
    }
    if self.media_type is None and content is None:
        del result["body"]  # type: ignore[union-attr]
        del result["headers"]["content-type"]  # type: ignore[union-attr]
    result["headers"].update(self.headers)  # type: ignore[union-attr]
    return result

content_converter

content_converter(content)
Source code in nexify/responses.py
68
69
def content_converter(self, content: Any) -> Any:
    return json.dumps(content)

nexify.responses.PlainTextResponse

PlainTextResponse(
    content=None,
    status_code=HTTP_200_OK,
    headers=None,
    media_type=None,
)

Bases: HttpResponse

Source code in nexify/responses.py
75
76
77
78
79
80
81
82
def __init__(
    self,
    content: Any = None,
    status_code: int | None = status.HTTP_200_OK,
    headers: Mapping[str, str] | None = None,
    media_type: str | None = None,
):
    super().__init__(content=content, status_code=status_code, headers=headers, media_type=media_type)

charset class-attribute instance-attribute

charset = 'utf-8'

status_code class-attribute instance-attribute

status_code = status_code or status_code

headers instance-attribute

headers = headers or {}

content instance-attribute

content = content

response instance-attribute

response = render()

media_type class-attribute instance-attribute

media_type = 'text/plain'

render

render()
Source code in nexify/responses.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def render(self) -> dict[str, Any]:
    content = jsonable_encoder(self.content)
    content = self.content_converter(content)

    result = {
        "statusCode": self.status_code,
        "body": content,
        "headers": {
            "content-type": f"{self.media_type}; charset={self.charset}",
        },
    }
    if self.media_type is None and content is None:
        del result["body"]  # type: ignore[union-attr]
        del result["headers"]["content-type"]  # type: ignore[union-attr]
    result["headers"].update(self.headers)  # type: ignore[union-attr]
    return result

content_converter

content_converter(content)
Source code in nexify/responses.py
84
85
def content_converter(self, content: Any) -> Any:
    return str(content)

nexify.responses.HTMLResponse

HTMLResponse(
    content=None,
    status_code=HTTP_200_OK,
    headers=None,
    media_type=None,
)

Bases: HttpResponse

Source code in nexify/responses.py
91
92
93
94
95
96
97
98
def __init__(
    self,
    content: Any = None,
    status_code: int | None = status.HTTP_200_OK,
    headers: Mapping[str, str] | None = None,
    media_type: str | None = None,
):
    super().__init__(content=content, status_code=status_code, headers=headers, media_type=media_type)

charset class-attribute instance-attribute

charset = 'utf-8'

status_code class-attribute instance-attribute

status_code = status_code or status_code

headers instance-attribute

headers = headers or {}

content instance-attribute

content = content

response instance-attribute

response = render()

media_type class-attribute instance-attribute

media_type = 'text/html'

render

render()
Source code in nexify/responses.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def render(self) -> dict[str, Any]:
    content = jsonable_encoder(self.content)
    content = self.content_converter(content)

    result = {
        "statusCode": self.status_code,
        "body": content,
        "headers": {
            "content-type": f"{self.media_type}; charset={self.charset}",
        },
    }
    if self.media_type is None and content is None:
        del result["body"]  # type: ignore[union-attr]
        del result["headers"]["content-type"]  # type: ignore[union-attr]
    result["headers"].update(self.headers)  # type: ignore[union-attr]
    return result

content_converter

content_converter(content)
Source code in nexify/responses.py
100
101
def content_converter(self, content: Any) -> Any:
    return str(content)

nexify.responses.RedirectResponse

RedirectResponse(
    url,
    content=None,
    status_code=HTTP_307_TEMPORARY_REDIRECT,
    headers=None,
)

Bases: HttpResponse

Source code in nexify/responses.py
105
106
107
108
109
110
111
112
113
114
115
def __init__(
    self,
    url: str,
    content: Annotated[None, "This is not used. It is only for compatibility."] = None,
    status_code: int | None = status.HTTP_307_TEMPORARY_REDIRECT,
    headers: Mapping[str, str] | None = None,
):
    self.url = url
    headers = headers or {}
    headers["location"] = url  # type: ignore
    super().__init__(status_code=status_code, headers=headers)

media_type class-attribute instance-attribute

media_type = media_type or media_type

charset class-attribute instance-attribute

charset = 'utf-8'

status_code class-attribute instance-attribute

status_code = status_code or status_code

headers instance-attribute

headers = headers or {}

content instance-attribute

content = content

response instance-attribute

response = render()

url instance-attribute

url = url

render

render()
Source code in nexify/responses.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def render(self) -> dict[str, Any]:
    content = jsonable_encoder(self.content)
    content = self.content_converter(content)

    result = {
        "statusCode": self.status_code,
        "body": content,
        "headers": {
            "content-type": f"{self.media_type}; charset={self.charset}",
        },
    }
    if self.media_type is None and content is None:
        del result["body"]  # type: ignore[union-attr]
        del result["headers"]["content-type"]  # type: ignore[union-attr]
    result["headers"].update(self.headers)  # type: ignore[union-attr]
    return result

content_converter

content_converter(content)
Source code in nexify/responses.py
52
53
def content_converter(self, content: Any) -> Any:
    return self.content