avaje/avaje-http

Support avaje-inject aspects on generated http-client interfaces.

SentryMan opened this issue · 4 comments

I got a situation like this using the new Scoped Values.

      // I have a custom logger that will log MDC from Scoped Values
      final var mdc =
          MDC_SV.isBound() ? new HashMap<>(MDC_SV.get()) : new HashMap<String, String>();
      mdc.put("Downstream-API", "api call");

      final var response =
          ScopedValue.callWhere(mdc, () -> apiInterface.call(param, "application/json", "web").toString());

I'd like to just create an aspect and have that take care of running a request in an SV context instead of repeating myself.

I think that for client interfaces annotated with @Component we could add @Component and the aspect annotations to the generated HTTP client. In this way, the inject generator can create DI/Proxy classes.

inject generator can create DI/Proxy classes

The http client instance is created by ServiceLoader and not via DI, so to me a avaje-inject proxy isn't going to be useful here ... BUT ... http client already has API for wrapping requests - RequestIntercept so to me that is the first thing to look at for adding generic before/after logic for http client requests?

Problem is that SVs don't work like threadlocals, you can't just set it and do something somewhere else, it will only be bound inside the execution of the runnable/callable/supplier provided.

inject generator can create DI/Proxy classes

The http client instance is created by ServiceLoader and not via DI, so to me a avaje-inject proxy isn't going to be useful here

I'm essentially proposing an alternative way to create an http client instance, if you add DI annotations you can instanciate via an http client in the beanscope, instead of service loader

@SentryMan Should this issue be closed by the PR that was just merged? I'll re-open for now as I'm not sure.

Yeah, DI and Aspect annotations are copied to the generated client, causing inject to generate proxy/DI classes