question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for advanced context in request/response interceptors in generated C# client

See original GitHub issue

Current Behavior

Currently the request interception methods generated in C# clients are:

partial void PrepareRequest(System.Net.Http.HttpClient client, System.Net.Http.HttpRequestMessage request, string url);
partial void PrepareRequest(System.Net.Http.HttpClient client, System.Net.Http.HttpRequestMessage request, System.Text.StringBuilder urlBuilder);
partial void ProcessResponse(System.Net.Http.HttpClient client, System.Net.Http.HttpResponseMessage response);

The Problem

The existing interception mechanism gives very little context about the API that is being called (basically just the URL), which blocks implementation of many useful or even critical interceptions.

For example - in one our Swagger-based API we store the OAuth scopes required on each endpoint in OpenAPI Flow objects. Hence, we need to extend the generated proxy to add the correct token to the correct endpoint. With the current approach, this is not possible (afaik).

Suggested solution

The generated interception methods should be passed a “context” object that should contain all information about the endpoint called that can be extracted from the OpenAPI specification. The signature of those methods could then look something like this:

partial void PrepareRequest(System.Net.Http.HttpClient client, System.Net.Http.HttpRequestMessage request, OpenApiRequestContext context);
partial void ProcessResponse(System.Net.Http.HttpClient client, System.Net.Http.HttpResponseMessage response, OpenApiRequestContext context);

where

public class OpenApiRequestContext
{
    public OpenApiContext Api { get; }
    public OpenApiEndPointContext EndPoint { get; }
}

I also believe that the interception methods should use Async pattern because it might be necessary to perform an I/O operation to get the access token. Hence, this would be even better:

partial Task PrepareRequestAsync(System.Net.Http.HttpClient client, System.Net.Http.HttpRequestMessage request, OpenApiRequestContext context);
partial Task ProcessResponseAsync(System.Net.Http.HttpClient client, System.Net.Http.HttpResponseMessage response, OpenApiRequestContext context);

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:12
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
yngndrw-sagecommented, May 10, 2018

I’ve also just run into the issue of needing asynchronous logic within the PrepareRequest method, in my case I need to retrieve an access token via the OAuth client credentials flow. (Which is cached, but access tokens can expire so I can’t just retrieve it once)

Update: It would also be handy to have the cancellation token available in this extension point, if it were made asynchronous.

0reactions
johanndevcommented, Apr 15, 2021

Partial methods got some nice upgrades in C# 9 - they can now return values, see: https://blog.miguelbernard.com/c-9-extending-partial-methods

Read more comments on GitHub >

github_iconTop Results From Across the Web

Coding Filters and Interceptors for your RESTFul services
Filters are used to modify or process incoming and outgoing request/response headers. They can be applied both on the server side and on...
Read more >
local-ch/lhc: 🚀 Advanced HTTP Client for Ruby. Fueled ...
The request class handles the http request, implements the interceptor pattern, loads configured endpoints, generates urls from url-templates and raises ...
Read more >
Advanced Usage — Requests 2.31.0 documentation
Advanced Usage¶. This document covers some of Requests more advanced features. Session Objects¶. The Session object allows you to persist certain parameters ...
Read more >
Introduction
A request-level interceptor is a user-written CORBA object that provides a means to insert functionality, such as security or monitoring components, into the ......
Read more >
Authentication
An overview of gRPC authentication, including built-in auth mechanisms, and how to plug in your own authentication systems.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found