@ -42,9 +42,8 @@ If there's no `gzip` in the header, it will not try to decompress the body.
That way, the same route class can handle gzip compressed or uncompressed requests.
That way, the same route class can handle gzip compressed or uncompressed requests.
```Python hl_lines="8-15"
{* ../../docs_src/custom_request_and_route/tutorial001.py hl[8:15] *}
{!../../docs_src/custom_request_and_route/tutorial001.py!}
```
### Create a custom `GzipRoute` class
### Create a custom `GzipRoute` class
@ -56,9 +55,9 @@ This method returns a function. And that function is what will receive a request
Here we use it to create a `GzipRequest` from the original request.
Here we use it to create a `GzipRequest` from the original request.
```Python hl_lines="18-26"
{!../../docs_src/custom_request_and_route/tutorial001.py! }
{* ../../docs_src/custom_request_and_route/tutorial001.py hl[18:26] * }
```
/// note | "Technical Details"
/// note | "Technical Details"
@ -96,26 +95,25 @@ We can also use this same approach to access the request body in an exception ha
All we need to do is handle the request inside a `try` /`except` block:
All we need to do is handle the request inside a `try` /`except` block:
```Python hl_lines="13 15"
{!../../docs_src/custom_request_and_route/tutorial002.py! }
{* ../../docs_src/custom_request_and_route/tutorial002.py hl[13,15] * }
```
If an exception occurs, the`Request` instance will still be in scope, so we can read and make use of the request body when handling the error:
If an exception occurs, the`Request` instance will still be in scope, so we can read and make use of the request body when handling the error:
```Python hl_lines="16-18"
{!../../docs_src/custom_request_and_route/tutorial002.py!}
{* ../../docs_src/custom_request_and_route/tutorial002.py hl[16:18] *}
```
## Custom `APIRoute` class in a router
## Custom `APIRoute` class in a router
You can also set the `route_class` parameter of an `APIRouter` :
You can also set the `route_class` parameter of an `APIRouter` :
```Python hl_lines="26"
{* ../../docs_src/custom_request_and_route/tutorial003.py hl[26] *}
{!../../docs_src/custom_request_and_route/tutorial003.py!}
```
In this example, the *path operations* under the `router` will use the custom `TimedRoute` class, and will have an extra `X-Response-Time` header in the response with the time it took to generate the response:
In this example, the *path operations* under the `router` will use the custom `TimedRoute` class, and will have an extra `X-Response-Time` header in the response with the time it took to generate the response:
```Python hl_lines="13-20"
{!../../docs_src/custom_request_and_route/tutorial003.py!}
{* ../../docs_src/custom_request_and_route/tutorial003.py hl[13:20] *}
```