Skip to content

Commit

Permalink
First example code for assignment call in java
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaSuckro committed Mar 26, 2017
1 parent ccb0a50 commit d39c286
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions modules/ui/app/resources/samplecode/java/Assignment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
* This class contains the sample code to get a wasabi assignments.
*/
public class Assignment {

public static void main(String[] args) throws IOException, ParseException {
System.out.print(getAssignment("ApplicationName", "ExperimentName", "UserName"));
}


/**
* This method calls the Wasabi Api to get an assignment for the specified experiment and user.
*
* @param application the Applicationname the experiment is running in
* @param experiment the name of the Experiment
* @param user the user for whom an Assignment should be generated
* @return the Assignment or <code>null</code> if the configuration was wrong
* @throws IOException
* @throws ParseException
*/
private static Object getAssignment(String application, String experiment, String user) throws IOException, ParseException {

String urlAssignment = "https://abtesting.intuit.com/api/v1/assignments/applications/" + application
+ "/experiments/" + experiment + "/users/" + user;

URL url = new URL(urlAssignment);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("GET");

// give it 500 milliseconds to respond
connection.setReadTimeout(500);
connection.connect();

// read the output from the server
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

/*
The response from the server is a json with the following exemplary entries:
{"cache":true,
"payload":null,
"assignment":"b",
"context":"PROD",
"status":"EXISTING_ASSIGNMENT"}
*/
String line = reader.readLine();

JSONParser parser = new JSONParser();
JSONObject assignment = (JSONObject) parser.parse(line);

return assignment.get("assignment"); // if you are just interested in the assignment take the 'assignment' field
}

}

0 comments on commit d39c286

Please sign in to comment.