I'm trying to build a simple app that calls an API with quarkus-rest-client
.
I have to inject an API Key as a header which is the same for all resources of the API.
So I would like to put the value of this API Key (that depends on the environment dev/qa/prod
) in the application.properties
file located in src/main/resources
.
I tried different ways to achieve this:
- Use directly
com.acme.Configuration.getKey
into@ClientHeaderParam
value property - Create a StoresClientHeadersFactory class which implements ClientHeadersFactory interface to inject the configuration
Finally, I found the way described below to make it work.
My question is: Is there a better way to do it?
Here's my code:
- StoreService.java which is my client to reach the API
@Path("/stores")
@RegisterRestClient
@ClientHeaderParam(name = "ApiKey", value = "{com.acme.Configuration.getStoresApiKey}")
public interface StoresService {
@GET
@Produces("application/json")
Stores getStores();
}
- Configuration.java
@ApplicationScoped
public class Configuration {
@ConfigProperty(name = "apiKey.stores")
private String storesApiKey;
public String getKey() {
return storesApiKey;
}
public static String getStoresApiKey() {
return CDI.current().select(Configuration.class).get().getKey();
}
}
- StoresController.java which is the REST controller
@Path("/stores")
public class StoresController {
@Inject
@RestClient
StoresService storesService;
@GET
@Produces(MediaType.APPLICATION_JSON)
public Stores getStores() {
return storesService.getStores();
}
}