Skip to content

Commit

Permalink
add action and impression for java
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaSuckro committed Mar 27, 2017
1 parent a0604d2 commit bb1cc73
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 5 deletions.
47 changes: 47 additions & 0 deletions modules/ui/app/resources/samplecode/java/Action.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
* This class contains the sample code to post a Wasabi action.
*/
public class Action {

public static void main(String[] args) throws IOException {
System.out.print(postAction("ApplicationName", "ExperimentName", "UserName") ? "Action recorded" : "Action not recorded");
}


/**
* This method calls the Wasabi Api to post an action 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 the action should be posted
* @return <code>true</code> if the action was recorded <code>false</code> otherwise
* @throws IOException
*/
private static boolean postAction(String application, String experiment, String user) throws IOException {

String urlImpression = String.format("https://abtesting.intuit.com/api/v1/events/applications/%s/experiments/%s/users/%s", application, experiment, user);

URL url = new URL(urlImpression);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("content-type", "application/json");
connection.setRequestMethod("POST");
connection.setDoOutput(true);

OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
// the following could also be created with json frameworks
writer.write("{\"events\":[{\"name\":\"ButtonClicked\", \"payload\":\"{\\\"state\\\":\\\"CA\\\"}\"}]}");
writer.flush();

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

return connection.getResponseCode() == 201 ? true : false;
}

}
5 changes: 2 additions & 3 deletions modules/ui/app/resources/samplecode/java/Assignment.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.net.URL;

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

Expand All @@ -30,8 +30,7 @@ public static void main(String[] args) throws IOException, 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;
String urlAssignment = String.format("https://abtesting.intuit.com/api/v1/assignments/applications/%s/experiments/%s/users/%s", application, experiment, user);

URL url = new URL(urlAssignment);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
Expand Down
46 changes: 46 additions & 0 deletions modules/ui/app/resources/samplecode/java/Impression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
* This class contains the sample code to post a Wasabi impression.
*/
public class Impression {

public static void main(String[] args) throws IOException {
System.out.print(postImpression("ApplicationName", "ExperimentName", "UserName") ? "Impression recorded" : "Impression not recorded");
}


/**
* This method calls the Wasabi Api to post an impression 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 the Impression should be posted
* @return <code>true</code> if the impression was recorded <code>false</code> otherwise
* @throws IOException
*/
private static boolean postImpression(String application, String experiment, String user) throws IOException {

String urlImpression = String.format("https://abtesting.intuit.com/api/v1/events/applications/%s/experiments/%s/users/%s", application, experiment, user);

URL url = new URL(urlImpression);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("content-type", "application/json");
connection.setRequestMethod("POST");
connection.setDoOutput(true);

OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write("{\"events\":[{\"name\":\"IMPRESSION\"}]}");
writer.flush();

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

return connection.getResponseCode() == 201 ? true : false;
}

}
3 changes: 1 addition & 2 deletions modules/ui/app/resources/samplecode/python/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ def post_action(application, experiment, user):
events = {'events':[{'name':'ButtonClicked', 'payload':'{\'state\':\'CA\'}'}]}

r = requests.post(urlAssignment, data = json.dumps(events), headers=headers)
print(r.status_code)
print(r.text)

if r.status_code == 201: # when the request returns 201 the action was recorded correctly
return True
return False
Expand Down
1 change: 1 addition & 0 deletions modules/ui/app/resources/samplecode/python/assignments.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def get_assignment(application, experiment, user):
if "EXPERIMENT_NOT_FOUND" in r.text:
print('The given Experiment is not found')
else:
# further exception handling should happen here
raise

if __name__ == "__main__":
Expand Down

0 comments on commit bb1cc73

Please sign in to comment.