diff --git a/src/main/java/com/dato/deploy/PredictiveServiceClient.java b/src/main/java/com/dato/deploy/PredictiveServiceClient.java index 4a375a3..80395b0 100644 --- a/src/main/java/com/dato/deploy/PredictiveServiceClient.java +++ b/src/main/java/com/dato/deploy/PredictiveServiceClient.java @@ -6,6 +6,7 @@ import java.io.File; import java.io.IOException; import java.util.concurrent.Future; +import java.util.Base64; import org.ini4j.InvalidFileFormatException; import org.ini4j.Wini; @@ -24,6 +25,7 @@ public class PredictiveServiceClient { private String endpoint; private boolean should_verify_certificate; private int timeout; + private int schema_version; private AsyncHttpClient asyncClient; /** @@ -54,6 +56,7 @@ public PredictiveServiceClient(String endpoint, String api_key, this.timeout = 10000; // default to 10 seconds timeout initConnection(); // initialize a connection to Predictive Service. + this.schema_version = getSchema(); // find out the schema version of the Predictive Service. } /** @@ -121,8 +124,9 @@ public PredictiveServiceClientResponse query(String predictive_object_name, JSONObject requestJSON = new JSONObject(); requestJSON.put("data", request); - requestJSON.put("api_key", this.getApikey()); - + if (this.schema_version < 7) { + requestJSON.put("api_key", this.getApikey()); + } return postRequest(url, requestJSON); } @@ -147,8 +151,9 @@ public PredictiveServiceClientResponse feedback( JSONObject requestJSON = new JSONObject(); requestJSON.put("data", data); requestJSON.put("id", request_id); - requestJSON.put("api_key", this.getApikey()); - + if (this.schema_version < 7) { + requestJSON.put("api_key", this.getApikey()); + } return postRequest(url, requestJSON); } @@ -159,7 +164,12 @@ public PredictiveServiceClientResponse feedback( private PredictiveServiceClientResponse postRequest(String url, JSONObject requestJSON) { BoundRequestBuilder asyncRequest = asyncClient.preparePost(url); - asyncRequest.setHeader("content-type", "application/json"); + asyncRequest.addHeader("content-type", "application/json"); + try { + asyncRequest.addHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString(("api_key:"+this.getApikey()).getBytes("UTF-8"))); + } catch (java.io.UnsupportedEncodingException e) { + throw new PredictiveServiceClientException("Error while base64-encoding the api_key.",e); + } asyncRequest.setBody(requestJSON.toJSONString()); asyncRequest.setRequestTimeout(this.timeout); @@ -174,7 +184,28 @@ private PredictiveServiceClientResponse getRequest(String url) { Future response = asyncClient.prepareGet(url).execute(); return new PredictiveServiceClientResponse(response); } - + + /* + * Initialize a connection to the Predictive Service. + */ + public int getSchema() { + String url = constructURL(this.endpoint); + PredictiveServiceClientResponse response = getRequest(url); + if (response.getStatusCode() == 200) { + // successfully connected + JSONObject results = response.getResult(); + try { + return ((Long)results.get("schema_version")).intValue(); + } catch (Exception e) { + return -1; + } + } else { + throw new PredictiveServiceClientException( + "Error connecting to service: response: " + + response.getErrorMessage()); + } + } + /* * Initialize a connection to the Predictive Service. */ diff --git a/src/main/java/com/dato/deploy/SampleCode.java b/src/main/java/com/dato/deploy/SampleCode.java new file mode 100644 index 0000000..76951d6 --- /dev/null +++ b/src/main/java/com/dato/deploy/SampleCode.java @@ -0,0 +1,46 @@ +import com.dato.deploy.PredictiveServiceClient; +import org.json.simple.JSONObject; +import com.dato.deploy.PredictiveServiceClientResponse; +import com.dato.deploy.PredictiveServiceClientException; +/* + * A sample Java main application that connects to a running + * Dato Predictive Services. This example assumes an "add" function + * already deployed in the predictive service. The schema of "add" function + * in Python is as follow: + * + * def add(a,b): + * return a + b + * + * Replace "end_point" and "api_key" to the corresponding values in your + * predictive service. To run this Java main application using maven: + * + * >>> mvn compile + * >>> mvn exec:java -Dexec.mainClass="SampleCode" + */ + +public class SampleCode { + public static String end_point = "http://localhost:9005"; + public static String api_key = "api_key"; + + public static void main(String args[]) { + PredictiveServiceClient client = new PredictiveServiceClient(end_point,api_key,false); + System.out.println("schema version:" + client.getSchema()); + JSONObject request = new JSONObject(); + + request.put("a", 10); + request.put("b", 2); + + PredictiveServiceClientResponse response = client.query("add",request); + if (response.getStatusCode() == 200) { + // successfully connected + JSONObject results = response.getResult(); + System.out.println(results.toString()); + System.exit(0); + } else { + throw new PredictiveServiceClientException( + "Error connecting to service: response: " + + response.getErrorMessage()); + } + + } +}