com.sigopt.net.APIMethodCaller Maven / Gradle / Ivy
package com.sigopt.net;
import com.sigopt.exception.APIException;
import java.lang.reflect.Type;
import java.util.Map;
public class APIMethodCaller {
APIMethod apiMethod;
APIMethod.Builder apiMethodBuilder = new APIMethod.Builder();
Class klass;
Type typeOfT;
public APIMethodCaller(String method, String path) {
this(method, path, null);
}
public APIMethodCaller(String method, String path, Class klass) {
this(method, path, null, klass);
}
public APIMethodCaller(String method, String path, Type typeOfT) {
this(method, path, null, typeOfT);
}
public APIMethodCaller(String method, String path, Object instance, Class klass) {
this.apiMethodBuilder.method(method).path(path).instance(instance);
this.klass = klass;
}
public APIMethodCaller(String method, String path, Object instance, Type typeOfT) {
this.apiMethodBuilder.method(method).path(path).instance(instance);
this.typeOfT = typeOfT;
}
public T call() throws APIException {
this.apiMethod = apiMethodBuilder.build();
apiMethod.execute();
if(klass != null) {
if (APIObject.class.isAssignableFrom(klass) || APIResource.class.isAssignableFrom(klass)) {
return APIResource.constructFromJson(apiMethod.response.body, klass);
} else {
return null;
}
} else if(typeOfT != null) {
return APIResource.constructTypedFromJson(apiMethod.response.body, typeOfT);
} else {
return null;
}
}
public APIMethod getApiMethod() {
return apiMethod;
}
public APIMethodCaller params(Map params) {
this.apiMethodBuilder.params(params);
return this;
}
public APIMethodCaller addParam(String key, Object value) {
this.apiMethodBuilder.addParam(key, value);
return this;
}
public APIMethodCaller headers(Map headers) {
this.apiMethodBuilder.headers(headers);
return this;
}
public APIMethodCaller addHeader(String key, String value) {
this.apiMethodBuilder.addHeader(key, value);
return this;
}
public APIMethodCaller clientToken(String clientToken) {
this.apiMethodBuilder.clientToken(clientToken);
return this;
}
public APIMethodCaller userToken(String userToken) {
this.apiMethodBuilder.userToken(userToken);
return this;
}
public APIMethodCaller apiBase(String apiBase) {
this.apiMethodBuilder.apiBase(apiBase);
return this;
}
}