Response classes
!!! warning
The current page still doesn't have a translation for this language.
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
status_code
class-attribute
instance-attribute
status_code = status_code or status_code
media_type = media_type or media_type
content
instance-attribute
response
instance-attribute
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
| 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
| 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
status_code
class-attribute
instance-attribute
status_code = status_code or status_code
content
instance-attribute
response
instance-attribute
media_type = 'application/json'
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
| 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
| 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
status_code
class-attribute
instance-attribute
status_code = status_code or status_code
content
instance-attribute
response
instance-attribute
media_type
class-attribute
instance-attribute
media_type = 'text/plain'
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
| 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
| 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
status_code
class-attribute
instance-attribute
status_code = status_code or status_code
content
instance-attribute
response
instance-attribute
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
| def content_converter(self, content: Any) -> Any:
return str(content)
|
nexify.responses.RedirectResponse
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 = media_type or media_type
charset
class-attribute
instance-attribute
status_code
class-attribute
instance-attribute
status_code = status_code or status_code
content
instance-attribute
response
instance-attribute
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
| def content_converter(self, content: Any) -> Any:
return self.content
|