com.savl.ripple.client.requests.Request Maven / Gradle / Ivy
package com.savl.ripple.client.requests;
import com.savl.ripple.client.Client;
import com.savl.ripple.client.enums.Command;
import com.savl.ripple.client.pubsub.Publisher;
import com.savl.ripple.client.responses.Response;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Iterator;
import java.util.logging.Logger;
// We can just shift to using delegation
public class Request extends Publisher {
// com.ripple.client.requests.Request // ??
public static final Logger logger = Logger.getLogger(Request.class.getName());
public static final long TIME_OUT = 60000;
static public final int VALIDATED_LEDGER = -3;
static public final int CLOSED_LEDGER = -2;
static public final int OPEN_LEDGER = -1;
public void json(JSONObject jsonObject) {
Iterator keys = jsonObject.keys();
while (keys.hasNext()) {
String next = (String) keys.next();
json(next, jsonObject.opt(next));
}
}
public static interface Builder {
void beforeRequest(Request request);
T buildTypedResponse(Response response);
}
// Base events class and aliases
public static interface events extends Publisher.Callback {}
public static interface OnSuccess extends events {}
public static interface OnError extends events {}
public static interface OnResponse extends events {}
public static interface OnTimeout extends events {}
public static abstract class Manager {
abstract public void cb(Response response, T t) throws JSONException;
public boolean retryOnUnsuccessful(Response r) {
return false;
}
public void beforeRequest(Request r) {}
}
Client client;
public Command cmd;
public Response response;
private JSONObject json;
public int id;
public long sendTime;
public Request(Command command, int assignedId, Client client) {
this.client = client;
cmd = command;
id = assignedId;
json = new JSONObject();
json("command", cmd.toString());
json("id", assignedId);
}
public JSONObject json() {
return json;
}
public void json(String key, Object value) {
json.put(key, value);
}
public void request() {
client.nowOrWhenConnected(new Client.OnConnected() {
@Override
public void called(final Client client_) {
client.sendRequest(Request.this);
}
});
}
public void bumpSendTime() {
sendTime = System.currentTimeMillis();
}
public JSONObject toJSON() {
return json();
}
public JSONObject jsonRepr() {
JSONObject repr = new JSONObject();
if (response != null) {
repr.put("response", response.message);
}
// Copy this
repr.put("request", new JSONObject(json.toString()));
return repr;
}
public void handleResponse(JSONObject msg) {
response = new Response(this, msg);
if (response.succeeded) {
emit(OnSuccess.class, response);
} else {
emit(OnError.class, response);
}
emit(OnResponse.class, response);
}
}