Customizing APIRoute and Request
299
if isinstance(body, dict):
sum = 0
for rate in body.values():
sum += rate
average = sum / len(body.values())
self.state.sum = sum
self.state.avg = average
return body
To activate this
ExtractionRequest
,
we need to set the
route_class
of the
APIRouter
of
the endpoints to
ExtractContentRoute
, as shown in the following snippet:
router = APIRouter()
router.route_class = ExtractContentRoute
There are three methods of choice to override when managing various request bodies:
•
body()
: This manages incoming request
data that is in bytes
•
form()
: This processes incoming form data
•
json()
: This manages
incoming parsed JSON data
•
stream()
: This accesses the body via a chunk of bytes using the
async for
construct
All of these methods return the original request body in bytes back to the service.
In
ExtractionRequest
, we have implemented three interface methods
from the given choices to
filter and process all incoming requests of the API endpoints defined in the
/api/route_extract.
py
module.
The following
create_profile()
service accepts profile data from
the client and implements
the
ExtractContentRoute
filter, which will store all of this profile data in the dictionary using
session handling:
Chia sẻ với bạn bè của bạn: