I'm trying to access a resource with like http://192.168.1.64:5050/api/{api_key}/updater.info
.
How would I dynamically set the api_key
parameter? I've tried using a RequestInterceptor
without success where the base url is http://192.168.1.64:5050/api/{api_key}
.
@Override
public void intercept(RequestFacade request) {
request.addPathParam("api_key", apiKey);
}
Are there any other alternatives?
Use this:
and you call the method like this:
Path replacement does not happen inside the base URL of the API endpoint, only the relative URL string on the method. I'm going to assume you don't want to prefix the relative URLs on every one of your interface method declarations.
While poorly worded, the javadoc of
Endpoint
states:This means that for every request the
Endpoint
instance will be consulted for the value of the base URL.You can supply a custom
Endpoint
implementation on which you can change the API key value:If the path parameter is not in the same position in the url for each request, for example,
http://endpoint/blah/{apiKey}
andhttp://endpoint/blah/blah/{apiKey}/blah
, you could do the following.In your APIService Interface
Then in your ApiClient Class create a
RequestInterceptor