Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance return types to Include HTTP response details #4767

Closed
jake-carpenter opened this issue Jun 4, 2024 · 6 comments
Closed

Enhance return types to Include HTTP response details #4767

jake-carpenter opened this issue Jun 4, 2024 · 6 comments
Labels
Status: No Recent Activity status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close type:feature New experience request

Comments

@jake-carpenter
Copy link

Is your feature request related to a problem? Please describe the problem.

One of the major shortcomings in many code generation tools is the assumption that HTTP calls are successful, otherwise throwing an error/exception. It is well-established best practice that try/catch blocks should not be used for flow control; instead, potential failures should be treated as conditions. Cleanly handling this becomes difficult when generated HTTP clients abstract away the details of an HTTP response needed for proper flow control.

The most common need to catch thrown errors/exceptions from generated code occurs with 400-level status codes. Some examples include:

  • HTTP 401 indicates that reauthentication is required.
  • HTTP 403 signals that a previously authorized user is no longer so, necessitating application action.
  • HTTP 404 suggests that a new resource at the URI must be created to ensure a good user experience.

Currently, our only option to handle these non-exceptional cases is to catch the error/exception.

Client library/SDK language

None

Describe the solution you'd like

I would like to see an option to "wrap" or "augment" return types from generated clients to include useful HTTP response information such as:

  • HTTP status code
  • HTTP headers collection
  • Any error information included in the HTTP content, as defined by the specification (if applicable)

In C#, where my primary concern lies, a common pattern is to wrap the success type with this additional information. However, other supported languages could use Either/Option/Maybe monads in their idiomatic ways.

It's important to consider that data streams passing between abstractions should be handled carefully. I am not requesting that all data from the HTTP request be included in every case.

Additional context

No response

@jake-carpenter jake-carpenter added status:waiting-for-triage An issue that is yet to be reviewed or assigned type:feature New experience request labels Jun 4, 2024
@github-project-automation github-project-automation bot moved this to Needs Triage 🔍 in Kiota Jun 4, 2024
@andrueastman
Copy link
Member

Thanks for raising this @jake-carpenter

At the moment it is possible to implement the IResponseHandler interface.
https://github.com/microsoft/kiota-abstractions-dotnet/blob/d608605b9ec506a65cea2c90090a1d990fce6df9/src/IResponseHandler.cs#L15

With this, you can probably change the response handling to not throw on various Http status code and return a value as you wish.

The example below shows how one can use it to get the raw HttpResponseMessage and then manipulate as you wish without error throwing at 400/500 codes using the NativeResponseHandler.

var nativeResponseHandler = new NativeResponseHandler();
await graphClient.Me.GetAsync(requestConfiguration => requestConfiguration.Options.Add(new ResponseHandlerOption(){ ResponseHandler = nativeResponseHandler }));

var responseMessage = nativeResponseHandler.Value as HttpResponseMessage;

Any chance this extensibility point works out for you?

@andrueastman andrueastman added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed status:waiting-for-triage An issue that is yet to be reviewed or assigned labels Jun 4, 2024
@andrueastman andrueastman moved this from Needs Triage 🔍 to Todo 📃 in Kiota Jun 4, 2024
@andrueastman andrueastman added this to the Backlog milestone Jun 5, 2024
@jake-carpenter
Copy link
Author

@andrueastman Thank you for bringing that to my attention. It's definitely nice to have that extension point as an option.

Ideally I would still like to see the generated code handle this itself to "make doing the right thing easy," but I'm glad to see it already has a flexible core.

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: Attention 👋 and removed status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close labels Jun 7, 2024
@baywet
Copy link
Member

baywet commented Jun 12, 2024

To complement Andrew's answer, you can also use the HeadersInspectionOption to access the headers.
In additions, the thrown API exception has a response status code and response headers fields.
I want to be transparent with the fact that returning the deserialized response, or throwing an exception when a failure occurs are design choices as we believe it'll simplify the implementation for the vast majority of kiota clients users. Which is why it's unlikely to change any time soon unless we get overwhelming evidence it should.

@baywet baywet added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed Needs: Attention 👋 labels Jun 12, 2024
@jake-carpenter
Copy link
Author

@baywet As someone who has used various tools like this, I find this disappointing because my experience is that these scenarios significantly increase complexity.

  • While I can access the status code in the exception, I still have to follow bad practices and catch to get it.
  • When catching an exception, we lose all context of potential problem details as defined in RFC 7807 (based on the current implementation in C#).

Missing this feature leads to:

  • Extra effort to work around the limitations with try/catch blocks or self-defined HTTP requests.
  • Poorly defined API specifications to avoid issues with client-generating libraries.
  • Low stickiness when a team encounters this use case.

Compared to a wrapper that provides the full context of the HTTP response, this approach is certainly not simpler.

I don't see it as a critical high-priority feature, but I hope it is reconsidered.

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: Attention 👋 and removed status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close Status: No Recent Activity labels Jun 18, 2024
@baywet
Copy link
Member

baywet commented Jun 18, 2024

Thanks for the additional input.

For the error status codes, assuming the problem schema is present in the description. The kiota client will deserialize all problem information for you. And the exception will contain the headers + status code.
The code block you've provided is only the last resort (when nothing was registered for the status code).

As for accessing the successful response headers/status code/raw body, this is something we're evaluating the design of in #4698

@baywet baywet added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed Needs: Attention 👋 labels Jun 18, 2024
Copy link
Contributor

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

@microsoft-github-policy-service microsoft-github-policy-service bot removed this from the Backlog milestone Jun 25, 2024
@github-project-automation github-project-automation bot moved this from Todo 📃 to Done ✔️ in Kiota Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: No Recent Activity status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close type:feature New experience request
Projects
Archived in project
Development

No branches or pull requests

3 participants