Skip to content

feat(http): implement body inspection handler - #736

Open
RKS (rksharma-owg) wants to merge 1 commit into
microsoft:mainfrom
rksharma-owg:feat/body-inspection-handler
Open

RKS (rksharma-owg) wants to merge 1 commit into
microsoft:mainfrom
rksharma-owg:feat/body-inspection-handler

Conversation

@rksharma-owg

Copy link
Copy Markdown
Contributor

Resolves #418

Description

This pull request implements the BodyInspectionHandler middleware and BodyInspectionHandlerOption for Kiota Python (packages/http/httpx), following the HTTP middleware specification in microsoftgraph/msgraph-sdk-design#116 and achieving parity with Kiota .NET and Kiota TypeScript.

Summary of Changes

  • BodyInspectionHandlerOption:
    • Adds configuration options with inspect_request_body: bool = False and inspect_response_body: bool = False (defaulting to disabled per spec).
    • Exposes captured request and response bodies as bytes (request_body, get_request_body(), get_response_body()).
    • Provides seekable stream accessors (get_request_body_stream(), get_response_body_stream(), request_body_stream, response_body_stream) returning fresh BytesIO copies rewound to position 0.
  • BodyInspectionHandler:
    • Middleware subclassing BaseMiddleware to intercept and non-destructively capture request and response bodies using await request.aread() and await response.aread().
    • Preserves payloads so subsequent middlewares and transport can consume streams without payload exhaustion.
    • Emits OpenTelemetry trace span BodyInspectionHandler_send with attribute "com.microsoft.kiota.handler.body_inspection.enable".
    • Resets captured bodies between requests when handler-level default options are used.
    • Supports per-request options override via request.options.
  • KiotaClientFactory:
    • Registers BodyInspectionHandler in the default middleware pipeline following HeadersInspectionHandler.
  • Tests:
    • Added unit test suite test_body_inspection_handler.py covering default options, custom options, rewound stream generation, request/response capture, streaming generators, empty payloads, disabled inspection, per-request overrides, and state reset.
    • Updated test_kiota_client_factory.py to verify pipeline registration and options handling.

Verification

  • Ran full test suite in packages/http/httpx: 123 passing tests (including all 13 new body inspection tests).
  • Ran type checks (mypy): 0 issues across all 26 source files.
  • Code formatted with isort and yapf.

- Add BodyInspectionHandlerOption with inspect_request_body and inspect_response_body flags
- Add BodyInspectionHandler middleware to non-destructively inspect request and response bodies
- Provide byte accessors and rewound BytesIO stream helpers
- Register BodyInspectionHandler in KiotaClientFactory default middleware pipeline
- Add comprehensive unit tests covering options, request/response capture, streaming, and pipeline integration
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

http - implement body inspection handler

1 participant