// Path: src/com/winterwell/es/client/agg/AggregationResults.java
// public class AggregationResults {
// 
// 	private String name;
// 	private Map results;
// 
// 	public AggregationResults(String aggName, Map rs) {
// 		this.name = aggName;
// 		this.results = rs;
// 	}
// 
// 	@Override
// 	public String toString() {
// 		return "AggregationResults[ "+name+": "+results+"]";
// 	}
// }
// 
// Path: src/com/winterwell/es/fail/ESBulkException.java
// public class ESBulkException extends ESException {
// 	
// 	private List<Exception> errors;
// 
// 	public List<Exception> getErrors() {
// 		return errors;
// 	}
// 	
// 	public ESBulkException(List<Exception> exs) {
// 		super(exs.size()+" errors", exs.size() > 1? exs.get(0) : null);
// 		this.errors = exs;
// 	}
// 
// 	private static final long serialVersionUID = 1L;
// 
// }
// 
// Path: src/com/winterwell/es/fail/ESException.java
// public class ESException extends WrappedException implements IElasticException {
// 
// 	public transient ESHttpRequest request;
// 
// 	public ESException(String msg, Throwable ex) {
// 		super(msg, ex);
// 	}
// 
// 	public ESException(String msg) {
// 		super(msg, null);
// 	}
// 	
// 
// 	private static final long serialVersionUID = 1L;
// 
// }


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.winterwell.es.client.agg.AggregationResults;
import com.winterwell.es.fail.ESBulkException;
import com.winterwell.es.fail.ESException;
import com.winterwell.gson.Gson;
import com.winterwell.gson.GsonBuilder;
import com.winterwell.utils.Dep;
import com.winterwell.utils.Printer;
import com.winterwell.utils.StrUtils;
import com.winterwell.utils.Utils;
import com.winterwell.utils.containers.Containers;
import com.winterwell.utils.log.Log;
import com.winterwell.utils.web.IHasJson;
package com.winterwell.es.client;



/**
 * Imitates {@link GetResponse}
 * @author daniel
 *
 */
public class ESHttpResponse implements IESResponse, SearchResponse, BulkResponse, GetResponse, 
// allow response to be sent as json over http 
IHasJson 
{

	
	@Override
	public Long getVersion() {
		Map<String, Object> jmap = getJsonMap();
		Number v = (Number) jmap.get("_version");
		return v==null? null : v.longValue();
	}
	
	private final String json;
	private final RuntimeException error;
	private final transient ESHttpRequest req;
	private Map parsed;
	private boolean sourceOnly;

	/* (non-Javadoc)
	 * @see com.winterwell.es.client.IESResponse#toString()
	 */
	@Override
	public String toString() {
		return "ESHttpResponse["+StrUtils.ellipsize(Utils.or(json, ""+error), 120)+"]";
	}
	
	/**
	 * For use by wrapper sub-classes
	 * @param response
	 */
	protected ESHttpResponse(ESHttpResponse response) {
		this(response.req, response.json, response.error);
	}
	
	public ESHttpResponse(ESHttpRequest req, String json) {
		this(req, json, null);
	}

	/**
	 * 
	 * @param req
	 * @param ex Should we standardise on {@link ESException}??
	 */
	public ESHttpResponse(ESHttpRequest req, RuntimeException ex) {
		this(req, null, ex);
	}

	ESHttpResponse(ESHttpRequest req, String json, RuntimeException ex) {
// Path: src/com/winterwell/es/client/agg/AggregationResults.java
// public class AggregationResults {
// 
// 	private String name;
// 	private Map results;
// 
// 	public AggregationResults(String aggName, Map rs) {
// 		this.name = aggName;
// 		this.results = rs;
// 	}
// 
// 	@Override
// 	public String toString() {
// 		return "AggregationResults[ "+name+": "+results+"]";
// 	}
// }
// 
// Path: src/com/winterwell/es/fail/ESBulkException.java
// public class ESBulkException extends ESException {
// 	
// 	private List<Exception> errors;
// 
// 	public List<Exception> getErrors() {
// 		return errors;
// 	}
// 	
// 	public ESBulkException(List<Exception> exs) {
// 		super(exs.size()+" errors", exs.size() > 1? exs.get(0) : null);
// 		this.errors = exs;
// 	}
// 
// 	private static final long serialVersionUID = 1L;
// 
// }
// 
// Path: src/com/winterwell/es/fail/ESException.java
// public class ESException extends WrappedException implements IElasticException {
// 
// 	public transient ESHttpRequest request;
// 
// 	public ESException(String msg, Throwable ex) {
// 		super(msg, ex);
// 	}
// 
// 	public ESException(String msg) {
// 		super(msg, null);
// 	}
// 	
// 
// 	private static final long serialVersionUID = 1L;
// 
// }


// Path: src/com/winterwell/es/client/ESHttpResponse.java
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.winterwell.es.client.agg.AggregationResults;
import com.winterwell.es.fail.ESBulkException;
import com.winterwell.es.fail.ESException;
import com.winterwell.gson.Gson;
import com.winterwell.gson.GsonBuilder;
import com.winterwell.utils.Dep;
import com.winterwell.utils.Printer;
import com.winterwell.utils.StrUtils;
import com.winterwell.utils.Utils;
import com.winterwell.utils.containers.Containers;
import com.winterwell.utils.log.Log;
import com.winterwell.utils.web.IHasJson;

package com.winterwell.es.client;



/**
 * Imitates {@link GetResponse}
 * @author daniel
 *
 */
public class ESHttpResponse implements IESResponse, SearchResponse, BulkResponse, GetResponse, 
// allow response to be sent as json over http 
IHasJson 
{

	
	@Override
	public Long getVersion() {
		Map<String, Object> jmap = getJsonMap();
		Number v = (Number) jmap.get("_version");
		return v==null? null : v.longValue();
	}
	
	private final String json;
	private final RuntimeException error;
	private final transient ESHttpRequest req;
	private Map parsed;
	private boolean sourceOnly;

	/* (non-Javadoc)
	 * @see com.winterwell.es.client.IESResponse#toString()
	 */
	@Override
	public String toString() {
		return "ESHttpResponse["+StrUtils.ellipsize(Utils.or(json, ""+error), 120)+"]";
	}
	
	/**
	 * For use by wrapper sub-classes
	 * @param response
	 */
	protected ESHttpResponse(ESHttpResponse response) {
		this(response.req, response.json, response.error);
	}
	
	public ESHttpResponse(ESHttpRequest req, String json) {
		this(req, json, null);
	}

	/**
	 * 
	 * @param req
	 * @param ex Should we standardise on {@link ESException}??
	 */
	public ESHttpResponse(ESHttpRequest req, RuntimeException ex) {
		this(req, null, ex);
	}

	ESHttpResponse(ESHttpRequest req, String json, RuntimeException ex) {
		this.req = req;