Skip to content

Allow conditional capture of response stream with body inspection handler #575

@julealgon

Description

@julealgon

Proposal

BodyInspectionHandlerOption should provide a way to only intercept responses under specific conditions, such as only for specific status codes, or only when no factory is registered for that response in the proxy (when it "doesn't know how to parse the contents").

The current implementation is wasteful as it will unconditionally copy the stream's contents every single time, which does not align with consumer flows where we only want to inspect the contents on specific situations.

Context

We have an API that does not itself produce any 400 BadRequest results, so its OpenApi document does not contain that possibility. However, we have seen in our production environment some bad requests coming out of it. We suspect the 400 responses are coming from Azure API Management, where our API is hosted.

Because it is not explicitly mapped in the OpenAPI spec, we need a way to access the contents of the body only when kiota has failed to parse that response (or, as a fallback, we would only want to check 400 responses but that is more specific).

With the current implementation of BodyInspectionHandlerOption and BodyInspectionHandler, the consumer is forced to always inspect either the request or response: there is no way to do so conditionally.

Workarounds

As of now I don't see any option to make this conditional unless we decided to implement our own HttpClient delegating handler, defeating the entire purpose of the body inspection handler.

For now, we'll go ahead with always requesting the response body but only evaluate it when an ApiException is thrown, but this will end up consuming significantly more memory for us for no particular reason.

Related:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Needs Triage 🔍

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions