Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions src/main/java/com/dato/deploy/PredictiveServiceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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.
}

/**
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);

Expand All @@ -174,7 +184,28 @@ private PredictiveServiceClientResponse getRequest(String url) {
Future<Response> 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.
*/
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/dato/deploy/SampleCode.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
}