Skip to content

Commit

Permalink
Updated Java kit to v1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapleaf committed Jan 5, 2011
1 parent d019c63 commit e9302a0
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 7 deletions.
129 changes: 126 additions & 3 deletions java/src/com/rapleaf/api/personalization/RapleafApi.java
Expand Up @@ -18,6 +18,8 @@
import java.net.*;
import java.io.*;
import org.json.JSONObject;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
* For general information regarding the personalization API,
Expand All @@ -30,6 +32,25 @@ public class RapleafApi {
private final static String BASE_URL = "https://personalize.rlcdn.com/v4/dr";
private final static int DEFAULT_TIMEOUT = 2000;
private final int timeout;

private String MD5Hex(String s) {
String result = null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] digest = md5.digest(s.getBytes());
result = toHex(digest);
} catch (NoSuchAlgorithmException e) {}
return result;
}

private String toHex(byte[] a) {
StringBuilder sb = new StringBuilder(a.length * 2);
for (int i = 0; i < a.length; i++) {
sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
sb.append(Character.forDigit(a[i] & 0x0f, 16));
}
return sb.toString();
}

/**
* Constructor for RapleafApi class
Expand All @@ -54,11 +75,113 @@ public RapleafApi(String apiKey, int timeout) {

/**
* @param email String email for query
* @return Returns a JSONObject associated with the email parameter
* @return Returns a JSONObject associated with the parameter(s)
* @throws Exception Throws error code on all HTTP statuses outside of 200 <= status < 300
*/
public JSONObject queryByEmail(String email) throws Exception {
String url = BASE_URL + "?email=" + URLEncoder.encode(email, "UTF-8") + "&api_key=" + apiKey;
return queryByEmail(email, false);
}

/**
* @param email String email for query
* @param hash_email If true, md5 hash the email before sending
* @return Returns a JSONObject associated with the parameter(s)
* @throws Exception Throws error code on all HTTP statuses outside of 200 <= status < 300
*/
public JSONObject queryByEmail(String email, boolean hash_email) throws Exception {
if (hash_email) {
return queryByMd5(MD5Hex(email));
} else {
String url = BASE_URL + "?email=" + URLEncoder.encode(email, "UTF-8") + "&api_key=" + apiKey;
return getJsonResponse(url);
}
}

/**
* @param md5Email Md5 hashed string email for query
* @return Returns a JSONObject associated with the parameter(s)
* @throws Exception Throws error code on all HTTP statuses outside of 200 <= status < 300
*/
public JSONObject queryByMd5(String md5Email) throws Exception {
String url = BASE_URL + "?md5_email=" + URLEncoder.encode(md5Email, "UTF-8") + "&api_key=" + apiKey;
return getJsonResponse(url);
}

/**
* @param sha1Email Sha1 hashed string email for query
* @return Returns a JSONObject associated with the parameter(s)
* @throws Exception Throws error code on all HTTP statuses outside of 200 <= status < 300
*/
public JSONObject queryBySha1(String sha1Email) throws Exception {
String url = BASE_URL + "?sha1_email=" + URLEncoder.encode(sha1Email, "UTF-8") + "&api_key=" + apiKey;
return getJsonResponse(url);
}

/**
* @param first First name
* @param last Last name
* @param street Street address
* @param city City name
* @param state State initials
* @return Returns a JSONObject associated with the parameter(s)
* @throws Exception Throws error code on all HTTP statuses outside of 200 <= status < 300
*/
public JSONObject queryByNap(String first, String last, String street, String city, String state) throws Exception {
return queryByNap(first, last, street, city, state, null);
}

/**
* @param first First name
* @param last Last name
* @param street Street address
* @param city City name
* @param state State initials
* @return Returns a JSONObject associated with the parameter(s)
* @throws Exception Throws error code on all HTTP statuses outside of 200 <= status < 300
*/
public JSONObject queryByNap(String first, String last, String street, String city, String state, String email) throws Exception {
String url;
if (email != null) {
url = BASE_URL + "?email=" + URLEncoder.encode(email, "UTF-8") + "&api_key=" + apiKey +
"?first=" + URLEncoder.encode(first, "UTF-8") + "?last=" + URLEncoder.encode(last, "UTF-8") +
"?street=" + URLEncoder.encode(street, "UTF-8") + "?city=" + URLEncoder.encode(city, "UTF-8") +
"?state=" + URLEncoder.encode(state, "UTF-8");
} else {
url = BASE_URL + "&api_key=" + apiKey + "?state=" + URLEncoder.encode(state, "UTF-8") +
"?first=" + URLEncoder.encode(first, "UTF-8") + "?last=" + URLEncoder.encode(last, "UTF-8") +
"?street=" + URLEncoder.encode(street, "UTF-8") + "?city=" + URLEncoder.encode(city, "UTF-8");
}
return getJsonResponse(url);
}

/**
* @param first First name
* @param last Last name
* @param zip String containing 5 digit Zipcode + 4 digit extension separated by dash
* @return Returns a JSONObject associated with the parameter(s)
* @throws Exception Throws error code on all HTTP statuses outside of 200 <= status < 300
*/
public JSONObject queryByNaz(String first, String last, String zip4) throws Exception {
return queryByNaz(first, last, zip4, null);
}

/**
* @param first First name
* @param last Last name
* @param zip String containing 5 digit Zipcode + 4 digit extension separated by dash
* @return Returns a JSONObject associated with the parameter(s)
* @throws Exception Throws error code on all HTTP statuses outside of 200 <= status < 300
*/
public JSONObject queryByNaz(String first, String last, String zip4, String email) throws Exception {
String url;
if (email != null) {
url = BASE_URL + "?email=" + URLEncoder.encode(email, "UTF-8") + "&api_key=" + apiKey +
"?first=" + URLEncoder.encode(first, "UTF-8") + "?last=" + URLEncoder.encode(last, "UTF-8") +
"?zip4=" + zip4;
} else {
url = BASE_URL + "&api_key=" + apiKey + "?zip4=" + zip4 +
"?first=" + URLEncoder.encode(first, "UTF-8") + "?last=" + URLEncoder.encode(last, "UTF-8");
}
return getJsonResponse(url);
}

Expand All @@ -85,4 +208,4 @@ private JSONObject getJsonResponse(String urlStr) throws Exception {
}
return new JSONObject(responseBody);
}
}
}
4 changes: 2 additions & 2 deletions java/src/com/rapleaf/api/personalization/RapleafExample.java
Expand Up @@ -19,9 +19,9 @@

public class RapleafExample {
public static void main(String[] args) {
RapleafApi api = new RapleafApi("SET_ME"); // Set API key here
RapleafApi api = new RapleafApi("d4c02b8148d1cca0a2632aa08ccd3908"); // Set API key here
try {
JSONObject response = api.queryByEmail("dummy@rapleaf.com");
JSONObject response = api.queryByEmail("dummy@rapleaf.com", true);
System.out.println(response);
} catch (Exception e) {
e.printStackTrace();
Expand Down
2 changes: 1 addition & 1 deletion perl/README.txt
Expand Up @@ -20,4 +20,4 @@ For general information regarding the personalization API, visit http://www.rapl

The API is queried by calling any of the query functions belonging to the RapleafApi file. An example script, RapleafExample, is provided. The example script takes an e-mail as a command line parameter, connects to Rapleaf's database, and returns (and sends to stdout) a collection of associated key-value pairs.

In order to run the Perl API, you need to grab JSON-2.50.tar.gz from http://search.cpan.org/~makamaka/JSON-2.50/lib/JSON.pm (flushed right). The installation is described in the README file. It consists of navigating to the download directory and running Makefile.pl, make, make test, and make install (in that order).
In order to run the Perl development kit, you need to grab JSON-2.50.tar.gz from http://search.cpan.org/~makamaka/JSON-2.50/lib/JSON.pm (flushed right). The installation is described in the README file. It consists of navigating to the download directory and running Makefile.pl, make, make test, and make install (in that order).
2 changes: 1 addition & 1 deletion php5/README.txt
Expand Up @@ -18,6 +18,6 @@ Copyright 2010 Rapleaf

For general information regarding the personalization API, visit http://www.rapleaf.com/developers/api_docs/personalization/direct. The personalization API's terms and conditions are stated at http://www.rapleaf.com/developers/api_usage.

The API is queried by calling any of the query functions belonging to RapleafApi.php with the appropriate parameters. An example script, rapleaf_example.php, is provided. The example script takes an e-mail as a command line parameter, connects to Rapleaf's database, and returns (and sends to stdout) a collection of associated key-value pairs.
The API is queried by calling any of the query functions belonging to the RapleafApi file. An example script, RapleafExample, is provided. The example script takes an e-mail as a command line parameter, connects to Rapleaf's database, and returns (and sends to stdout) a collection of associated key-value pairs.

For the PHP version, we add two versions to appeal both to users running older versions of PHP and to users running PHP5.

0 comments on commit e9302a0

Please sign in to comment.