com.github.bingoohuang.springrestclient.utils.UniRestCallback Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-rest-client Show documentation
Show all versions of spring-rest-client Show documentation
convenient spring rest client for rest services
package com.github.bingoohuang.springrestclient.utils;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.async.Callback;
import com.mashape.unirest.http.exceptions.UnirestException;
class UniRestCallback implements Callback {
private final Class> apiClass;
private final RestLog restLog;
private volatile boolean done;
private volatile boolean cancelled;
private HttpResponse response;
public UniRestCallback(final Class> apiClass, RestLog restLog) {
this.apiClass = apiClass;
this.restLog = restLog;
}
@Override
public void completed(HttpResponse response) {
this.response = response;
done = true;
restLog.log(response);
}
@Override
public void failed(UnirestException e) {
done = true;
restLog.log(e);
}
@Override
public void cancelled() {
cancelled = true;
restLog.log("cancelled");
}
public boolean isDone() {
return done || cancelled;
}
public boolean isCancelled() {
return cancelled;
}
public HttpResponse get() throws InterruptedException {
while (!isDone()) Thread.sleep(1);
return response;
}
public HttpResponse get(long timeout) throws InterruptedException {
long start = System.currentTimeMillis();
while (!isDone() && System.currentTimeMillis() - start < timeout) Thread.sleep(1);
return response;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy