openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[REQ] csharp generators - Enable override of InterceptRequest and InterceptResponse

Open coloboxp opened this issue 3 years ago • 0 comments

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

To simplify message inteception, analysis or logging, and doing it in a centralized manner, would be ideal to be able to override the InterceptRequest and InterceptResponse methods on the ApiClient.cs generated code.

Describe the solution you'd like

Add the possibility to override the message interception

Describe alternatives you've considered

Using a public virtual method to do so; I've edited the mustache file to do it on my end, as the client is generated often and editing it each time is painful.

Additional context

This is what I ended up doing for now, better alternatives are welcome :)

Added the following on my mustache files

        /// <summary>
        /// Allows for extending request processing for <see cref="ApiClient"/> generated code.
        /// </summary>
        /// <param name="request">The RestSharp request object</param>
        //partial void InterceptRequest(RestRequest request);
        public virtual void InterceptRequest(RestRequest request)
        {
            
        }

        /// <summary>
        /// Allows for extending response processing for <see cref="ApiClient"/> generated code.
        /// </summary>
        /// <param name="request">The RestSharp request object</param>
        /// <param name="response">The RestSharp response object</param>
        public virtual void InterceptResponse(RestRequest request, RestResponse response)
        {

        }

Then on my inherited ApiClient, I can do :

    public class LocalApiClient : ApiClient
    {
        public override void InterceptRequest(RestRequest request)
        {
            // my custom analyitics here
        }

        public override void InterceptResponse(RestRequest request, RestResponse response)
        {
            // my custom analyitics here
        }
    }

coloboxp avatar Feb 03 '23 09:02 coloboxp