PlaytikaOSS / feign-reactive

Reactive Feign client inspired by https://github.com/OpenFeign project
Apache License 2.0
600 stars 119 forks source link

Add support of setting dynamic URI through method parameters #627

Open mkrasilnikow opened 9 months ago

mkrasilnikow commented 9 months ago

In Feign client I can change URI in runtime, without creating new instance of Feign client through builder, like:

    @FeignClient(name = "dummy-name", url = "this-is-a-placeholder")
    public interface MyClient {
        @PostMapping(path = "/create")
        UserDto createUser(URI baseUrl, @RequestBody UserDto userDto);
    }
   //call service:
    URI determinedBasePathUri = URI.create("https://my-host.com");
    myClient.createUser(determinedBasePathUri, userDto);

But in ReactiveFeign I can't do so. Only through WebReactiveFeign .builder like this:

IcecreamServiceApi client = 
         WebReactiveFeign  //WebClient based reactive feign  
        .<IcecreamServiceApi>builder()
        .target(IcecreamServiceApi.class, "http://www.myUrl.com")

Is it possible to reuse the approach from Feign to ReactiveFeign client?

Thanks!

mkrasilnikow commented 9 months ago

Original question from StackOverflow: https://stackoverflow.com/questions/65257778/using-dynamic-url-for-spring-reactivefeignclient

marcusvoltolim commented 4 months ago

Workaround with Java21 using ReactiveHttpRequestInterceptor and @RequestHeader:

@ReactiveFeignClient(name = "ReactiveDynamicUrlApi", url = BASE_URL, path = "v1/api/product", configuration = ReactiveDynamicUrlApi.RequestInterceptor.class)
interface ReactiveDynamicUrlApi {

    String BASE_URL = "base-url";

    @PostMapping(path = "{id}", headers = "Content-Type=application/json")
    Mono<String> post(@RequestHeader(BASE_URL) String baseUrl, @PathVariable String id, @RequestHeader String token, @RequestBody Map requestBody);

    class RequestInterceptor implements ReactiveHttpRequestInterceptor {

        @Override
        public Mono<ReactiveHttpRequest> apply(final ReactiveHttpRequest request) {
            var uri = URI.create(request.headers().get(BASE_URL).getFirst() + request.uri().getPath());
            request.headers().remove(BASE_URL);
            return Mono.just(new ReactiveHttpRequest(request, uri));
        }

    }

}