📝 clarify custom post_process docs
This commit is contained in:
parent
972225d8bc
commit
b9ad2467df
1 changed files with 5 additions and 4 deletions
|
@ -57,7 +57,7 @@ Unlike `process_request`, returning a value here doesn't change anything. We're
|
||||||
|
|
||||||
This is a helper function that is available for you to override; it's not often used by middleware, but there are some ([like the pydantic middleware](middleware/pydantic.md)) that call `on_error` when there is a validation failure.
|
This is a helper function that is available for you to override; it's not often used by middleware, but there are some ([like the pydantic middleware](middleware/pydantic.md)) that call `on_error` when there is a validation failure.
|
||||||
|
|
||||||
## post_process(self, request: Request, response: HttpResponse, rendered_response: str) -> str:
|
## post_process(self, request: Request, response: HttpResponse, rendered_response: str) -> str | bytes:
|
||||||
|
|
||||||
> New in 1.3.0!
|
> New in 1.3.0!
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ There are three things passed to `post_process`:
|
||||||
|
|
||||||
- `request`: the request object. It's provided here purely for reference purposes; while you can technically change it here, it won't have any effect on the response.
|
- `request`: the request object. It's provided here purely for reference purposes; while you can technically change it here, it won't have any effect on the response.
|
||||||
- `response`: the response object. The full HTML of the response has already been rendered, but the headers can still be modified here. This object can be modified in place, like in `process_response`.
|
- `response`: the response object. The full HTML of the response has already been rendered, but the headers can still be modified here. This object can be modified in place, like in `process_response`.
|
||||||
- `rendered_response`: the full HTML of the response as a string. This is the final output that will be sent to the client. Every instance of `post_process` must return the full HTML of the response, so if you want to make changes, you'll need to return the modified string.
|
- `rendered_response`: the full HTML of the response as a string or bytes. This is the final output that will be sent to the client. Every instance of `post_process` must return the full HTML of the response, so if you want to make changes, you'll need to return the modified string. A string is _strongly_ preferred, but bytes are also acceptable; keep in mind that you'll be making things harder for any `post_process` middleware that comes after you.
|
||||||
|
|
||||||
Note that this function *must* return the full HTML of the response (provided at the start as `rendered_response`. Each invocation of `post_process` overwrites the entire output of the response, so make sure to return everything that you want to send. For example, here's a middleware that ~~breaks~~ adjusts the capitalization of the response and also demonstrates passing variables into the middleware and modifies the headers with the type of transformation:
|
Note that this function *must* return the full HTML of the response (provided at the start as `rendered_response`. Each invocation of `post_process` overwrites the entire output of the response, so make sure to return everything that you want to send. For example, here's a middleware that ~~breaks~~ adjusts the capitalization of the response and also demonstrates passing variables into the middleware and modifies the headers with the type of transformation:
|
||||||
|
|
||||||
|
@ -75,13 +75,13 @@ Note that this function *must* return the full HTML of the response (provided at
|
||||||
import random
|
import random
|
||||||
|
|
||||||
from spiderweb.request import Request
|
from spiderweb.request import Request
|
||||||
|
from spiderweb.response import HttpResponse
|
||||||
from spiderweb.middleware import SpiderwebMiddleware
|
from spiderweb.middleware import SpiderwebMiddleware
|
||||||
from spiderweb.exceptions import ConfigError
|
from spiderweb.exceptions import ConfigError
|
||||||
|
|
||||||
|
|
||||||
class CaseTransformMiddleware(SpiderwebMiddleware):
|
class CaseTransformMiddleware(SpiderwebMiddleware):
|
||||||
# this breaks everything, but it's hilarious so it's worth it.
|
# this breaks everything, but it's hilarious so it's worth it. Blame Sam.
|
||||||
# Blame Sam.
|
|
||||||
def post_process(self, request: Request, response: HttpResponse, rendered_response: str) -> str:
|
def post_process(self, request: Request, response: HttpResponse, rendered_response: str) -> str:
|
||||||
valid_options = ["spongebob", "random"]
|
valid_options = ["spongebob", "random"]
|
||||||
# grab the value from the extra data passed into the server object
|
# grab the value from the extra data passed into the server object
|
||||||
|
@ -109,6 +109,7 @@ class CaseTransformMiddleware(SpiderwebMiddleware):
|
||||||
)
|
)
|
||||||
|
|
||||||
# usage:
|
# usage:
|
||||||
|
from spiderweb import SpiderwebRouter
|
||||||
|
|
||||||
app = SpiderwebRouter(
|
app = SpiderwebRouter(
|
||||||
middleware=["CaseTransformMiddleware"],
|
middleware=["CaseTransformMiddleware"],
|
||||||
|
|
Loading…
Add table
Reference in a new issue