com.turbomanage.httpclient.rest.AsyncRestClient Maven / Gradle / Ivy
package com.turbomanage.httpclient.rest;
import com.turbomanage.httpclient.AbstractHttpClient;
import com.turbomanage.httpclient.AsyncHttpClient;
import com.turbomanage.httpclient.HttpResponse;
import com.turbomanage.httpclient.ParameterMap;
public abstract class AsyncRestClient {
protected AsyncHttpClient httpClient;
protected ObjectFactory objFactory;
protected ResultHandler resultHandler;
public AsyncRestClient(AsyncHttpClient httpClient, ObjectFactory factory, ResultHandler handler) {
this.httpClient = httpClient;
this.objFactory = factory;
this.resultHandler = handler;
}
public ObjectResponse get(String path, ParameterMap params) {
HttpResponse httpResponse = httpClient.get(path, params);
if (resultHandler.onResult(httpResponse)) {
return objFactory.wrapResponse(httpResponse);
}
return null;
}
public T get(String path, ParameterMap params, Class responseType) {
HttpResponse httpResponse = httpClient.get(path, params);
if (resultHandler.onResult(httpResponse)) {
if (httpResponse != null) {
ObjectResponse objResponse = objFactory.wrapResponse(httpResponse);
T result = objResponse.toObj(responseType);
return result;
}
}
return null;
}
public ObjectResponse post(String path, T obj) {
HttpResponse httpResponse = httpClient.post(path, AbstractHttpClient.JSON, toBytes(obj));
if (resultHandler.onResult(httpResponse)) {
return objFactory.wrapResponse(httpResponse);
}
return null;
}
public ObjectResponse put(String path, T obj) {
HttpResponse httpResponse = httpClient.put(path, AbstractHttpClient.JSON, toBytes(obj));
if (resultHandler.onResult(httpResponse)) {
return objFactory.wrapResponse(httpResponse);
}
return null;
}
public ObjectResponse delete(String path, ParameterMap params) {
HttpResponse httpResponse = httpClient.delete(path, params);
if (resultHandler.onResult(httpResponse)) {
return objFactory.wrapResponse(httpResponse);
}
return null;
}
public byte[] toBytes(T obj) {
return objFactory.toBytes(obj);
}
}