-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First example code for assignment call in java
- Loading branch information
1 parent
ccb0a50
commit d39c286
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} | ||
|