
org.rapidoid.http.DynamicRESTClient Maven / Gradle / Ivy
The newest version!
package org.rapidoid.http;
/*
* #%L
* rapidoid-rest
* %%
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.OfType;
import org.rapidoid.annotation.Since;
import org.rapidoid.beany.Metadata;
import org.rapidoid.commons.Arr;
import org.rapidoid.concurrent.Callback;
import org.rapidoid.config.Conf;
import org.rapidoid.config.Config;
import org.rapidoid.lambda.Dynamic;
import org.rapidoid.u.U;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.Future;
@Authors("Nikolche Mihajlovski")
@Since("4.4.0")
public class DynamicRESTClient implements Dynamic {
private static final String[] VERBS = {"GET", "POST", "PUT", "DELETE"};
private final Class> clientInterface;
private final String configFile;
private final Config config;
public DynamicRESTClient(Class> clientInterface) {
this.clientInterface = clientInterface;
this.configFile = "endpoints/" + clientInterface.getSimpleName() + ".yaml";
this.config = Conf.refreshing("", configFile);
}
@SuppressWarnings("unchecked")
@Override
public Object call(Method m, Object[] args) {
U.must(!config.isEmpty(), "Cannot find the endpoint configuration (file '%s') for the interface: %s",
configFile, clientInterface.getSimpleName());
Map cfg = config.getOrFail(m.getName(), Map.class);
String verb = verbOf(cfg);
String url = (String) cfg.get(verb);
U.must(!U.isEmpty(verb), "The [verb: url] entry is not configured for the method: %s", m);
U.must(!U.isEmpty(url), "Cannot find 'url' configuration for the method: %s", m);
Class
© 2015 - 2025 Weber Informatics LLC | Privacy Policy