Define client target URL at runtime using Quarkus & Resteasy

Viewed 1157

I need to send HTTP requests from my Quarkus application. Following this guide, I have this RestClient:

@Path("/v1")
@RegisterRestClient
public interface CountriesService {

    @GET
    @Path("/name/{name}")
    Set<Country> getByName(@PathParam String name);
}

In the Path annotation, I can configure the path. But the domain/url to call is defined in a configuration file, according to this paragraph.

# Your configuration properties
org.acme.rest.client.CountriesService/mp-rest/url=https://restcountries.eu/rest # 
org.acme.rest.client.CountriesService/mp-rest/scope=javax.inject.Singleton #

In my case, I need this URL to be defined programmatically at runtime, as I receive it as a callback URL.

Is there a way to do that?

1 Answers

Quarkus Rest Client, and Quarkus Rest Client Reactive, implement the MicroProfile Rest specification and as such allow creating client stubs with RestClientBuilder programmatically, e.g.:

public class SomeService {
   public Response doWorkAgainstApi(URI apiUri, ApiModel apiModel) {
       RemoteApi remoteApi = RestClientBuilder.newBuilder()
            .baseUri(apiUri)
            .build(RemoteApi.class);
       return remoteApi.execute(apiModel);
   }
}

See https://download.eclipse.org/microprofile/microprofile-rest-client-2.0/microprofile-rest-client-spec-2.0.html#_sample_builder_usage

You cannot achieve this with client created with the @RegisterRestClient annotation

Related