1

I am using OpenAPI Generator in a C# project to generate a client which I can then use to interact with a Thingsboard instance and I just came across the exact same problem which someone else already described here: https://serveanswer.com/questions/how-should-you-implement-an-interceptresponse-method-to-handle-unauthorized-requests

In case the link breaks at a later point of time, here is a short summary of the issue: I need to intercept all responses with a 401 Unauthorized header, get a new token and then retry the request. However, in the auto-generated ApiClient class there are only the following two partial methods for intercepting requests/responses:

partial void InterceptRequest(IRestRequest request);
partial void InterceptResponse(IRestRequest request, IRestResponse response);

I already implemented everything necessary to catch 401 responses and refresh the token. The problem is that there does not seem to be any way for me to retry the request inside InterceptResponse, because the response is passed by value so I cannot modify it outside of InterceptResponse. I cannot edit the method signatures either because they are auto-generated.

How am I supposed to deal with this so I do not have to manually check for a 401 response for every single request and then refresh the token and retry the request manually?

Chris
  • 1,417
  • 4
  • 21
  • 53

2 Answers2

1

We had this problem as well and the solution (so far) seems to be to manually edit the method signatures so that the response parameter is ref. Then you can write your own partial method to handle a retry and pass the new response back to the client.

Eg

partial void InterceptResponse(IRestRequest request, **ref** IRestResponse response)

NOTE: This gets stickier if you're working with .NET Standard or Core as the parameters have abstraction:

partial void InterceptResponse(IRestRequest request, **ref** IRestResponse<T> response)

We haven't yet worked this out. It seems that the OpenAPI generator - at least of the definitions we're working with - doesn't maintain the signatures so when you change to ref the code breaks due to trying to convert IRestResponse<T> to IRestResponse.

In my opinion the IRestResponse parameter should be ref by default since you can't affect the actual call flow if it isn't.

SteveCinq
  • 1,920
  • 1
  • 17
  • 22
0

If your generated class is part of the same project, you can implement a partial class that implements the InterceptRequest and InterceptResponse partial methods.

Here's a Github thread/explanation on the reasoning between inheritance and partial classes for the generator.

David Fenko
  • 131
  • 2
  • 8
  • How would that help getting around the problem that the `response` is being passed by value so I cannot modify it outside of `InterceptResponse`? – Chris Jun 13 '22 at 08:00
  • 1
    Right, I missed that in your question. I looked at the generated source a bit and see that Polly is used, which means you could implement a retry policy, like mentioned here: https://stackoverflow.com/a/62374272/2187610 – David Fenko Jun 17 '22 at 22:04