All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.tcurrie.rest.factory.client.RestClientMethod Maven / Gradle / Ivy

There is a newer version: 0.2.70
Show newest version
package com.github.tcurrie.rest.factory.client;

import com.github.tcurrie.rest.factory.RestParameterAdaptor;
import com.github.tcurrie.rest.factory.proxy.ProxyMethod;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.Method;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;

class RestClientMethod implements ProxyMethod {
    private static final Logger LOGGER = Logger.getLogger(RestClientMethod.class.getName());
    private final Method method;
    private final Supplier methodUrlSupplier;
    private final RestParameterAdaptor.Client parameterAdaptor;
    private final Class methodResult;

    RestClientMethod(final Method method, final Supplier methodUrlSupplier, final RestParameterAdaptor.Client parameterAdaptor, final Class methodResult) {
        this.method = method;
        this.methodUrlSupplier = methodUrlSupplier;
        this.parameterAdaptor = parameterAdaptor;
        this.methodResult = methodResult;
    }

    @Override
    public Method getMethod() {
        return method;
    }

    Supplier getMethodUrlSupplier() {
        return methodUrlSupplier;
    }

    RestParameterAdaptor.Client getParameterAdaptor() {
        return parameterAdaptor;
    }

    Class getMethodResult() {
        return methodResult;
    }

    @Override
    public T invoke(final Object[] args) {
        final String url = methodUrlSupplier.get();
        final String body = parameterAdaptor.apply(args);
        LOGGER.log(Level.INFO, "For method [{0}] and args [{1}], posting to [{2}] with [{3}]", new Object[]{method, args, url, body});
        //TODO Remove dependency on Spring's rest template and/or at least handle timeouts!
        return new RestTemplate().postForObject(url, body, methodResult);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy