Skip to content

Instantly share code, notes, and snippets.

@wingchen
Last active December 14, 2015 06:38
Show Gist options
  • Save wingchen/5043687 to your computer and use it in GitHub Desktop.
Save wingchen/5043687 to your computer and use it in GitHub Desktop.
Fliptop API Code Example in java The 202 retry mechanism is built up into the202RetryQueue and FliptopAPIRetryClass. In order to run this code, you will have to download 'org.apache.httpcomponents(for HttpClient)', 'org.codehaus.jackson(for json-pojo conversion)', and 'commons-lang(for DateUtils)'. If you are using Maven, you can find pom.xml in…
package com.fliptop.api;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.codehaus.jackson.map.ObjectMapper;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.commons.lang.time.DateUtils;
public class FliptopApi {
private static final String FLIPTOP_API_KEY = "your fliptop api key";
private static ObjectMapper mapper = new ObjectMapper();
// gerSocialDataViaEmail handles the actual fliptop API query,
// and different cases according to the returned
// http status code
public static void gerSocialDataViaEmail(FliptopApiEmailUnit emailItem, String format,
ConcurrentLinkedQueue<FliptopApiEmailUnit> the202RetryQueue){
if(format == null || format.equals("")) {
format = "json";
}
if (emailItem.getRetryAttempt() == 0)
System.out.println("Looking up: " + emailItem.getEmail());
else if (emailItem.getRetryAttempt() > 3){
System.out.println("Retry over 3 times, dropping the email: " + emailItem.getEmail());
return;
} else System.out.println("Retrying: " + emailItem.getEmail());
try {
String queryString = String.format(
"http://api.fliptop.com/beta/person?api_key=%s&email=%s&format=%s",
FLIPTOP_API_KEY, emailItem.getEmail(), format);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(queryString);
getRequest.addHeader("accept", "application/json");
HttpResponse response = httpClient.execute(getRequest);
if (response.getStatusLine().getStatusCode() == 200) {
FliptopApiResponseObject responseItem =
mapper.readValue(response.getEntity().getContent(), FliptopApiResponseObject.class);
// status code = 200, you have gotta data from fliptop api
// normally you would like to store the returned data into your
// database, or write it into a desinated file
System.out.println("Email: " + responseItem.getEmail());
System.out.println("Name: " + responseItem.getName());
System.out.println("Company: " + responseItem.getCompany());
System.out.println("Title: " + responseItem.getTitle());
if(responseItem.getMemberships().containsKey("facebook"))
System.out.println("facebook: " + responseItem.getMemberships().get("facebook"));
if(responseItem.getMemberships().containsKey("linkedin"))
System.out.println("linkedin: " + responseItem.getMemberships().get("linkedin"));
if(responseItem.getMemberships().containsKey("twitter"))
System.out.println("twitter: " + responseItem.getMemberships().get("twitter"));
} else if (response.getStatusLine().getStatusCode() == 202) {
// 202, put this email into the202RetryQueue
emailItem.setRetryAttempt(emailItem.getRetryAttempt() + 1);
the202RetryQueue.add(emailItem);
} else if (response.getStatusLine().getStatusCode() == 400) {
System.out.println("Incorrect syntax. email: " + emailItem.getEmail());
} else if (response.getStatusLine().getStatusCode() == 500) {
System.out.println("Unexpected server error. Please email api@fliptop.com. email: " + emailItem.getEmail());
} else {
System.out.println("Other error with email: " + emailItem.getEmail() + " status code:" +
response.getStatusLine().getStatusCode());
}
httpClient.getConnectionManager().shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ConcurrentLinkedQueue<FliptopApiEmailUnit> the202RetryQueue =
new ConcurrentLinkedQueue<FliptopApiEmailUnit>();
ConcurrentLinkedQueue<FliptopApiEmailUnit> databaseEmails =
new ConcurrentLinkedQueue<FliptopApiEmailUnit>();
// these emails are the mock data that this example is gotta hit fliptop api with.
// you might want to replace them into any data source that you have.
databaseEmails.add(new FliptopApiEmailUnit("doug@fliptop.com"));
databaseEmails.add(new FliptopApiEmailUnit("tom@fliptop.com"));
databaseEmails.add(new FliptopApiEmailUnit("this_is_a_202@fliptop.com"));
// kick off the threads
// threads are used here to increase the performance
// if performance is not a concern here,
// you can just call FliptopAPIRetryClass after
// FliptopAPIWalkThroughClass is done, like the last two commented-out lines in this method.
(new FliptopAPIWalkThroughClass(databaseEmails, the202RetryQueue)).start();
(new FliptopAPIRetryClass(databaseEmails, the202RetryQueue)).start();
// (new FliptopAPIWalkThroughClass(databaseEmails, the202RetryQueue)).run();
// (new FliptopAPIRetryClass(databaseEmails, the202RetryQueue)).run();
}
}
/**
* FliptopAPIWalkThroughClass
* the thread class to walk through the entire database emails
**/
class FliptopAPIWalkThroughClass extends Thread{
private ConcurrentLinkedQueue<FliptopApiEmailUnit> the202RetryQueue;
private ConcurrentLinkedQueue<FliptopApiEmailUnit> databaseEmails;
public void run() {
while(databaseEmails.size() > 0){
FliptopApiEmailUnit item = databaseEmails.poll();
FliptopApi.gerSocialDataViaEmail(item, "json", the202RetryQueue);
}
System.out.println("Finishing FliptopAPIWalkThroughClass...");
}
public FliptopAPIWalkThroughClass(
ConcurrentLinkedQueue<FliptopApiEmailUnit> databaseEmails,
ConcurrentLinkedQueue<FliptopApiEmailUnit> the202RetryQueue){
this.the202RetryQueue = the202RetryQueue;
this.databaseEmails = databaseEmails;
}
}
/**
* FliptopAPIRetryClass
* the thread class to conduct retries
**/
class FliptopAPIRetryClass extends Thread{
private ConcurrentLinkedQueue<FliptopApiEmailUnit> the202RetryQueue;
private ConcurrentLinkedQueue<FliptopApiEmailUnit> databaseEmails;
public void run() {
System.out.println("Start Retrying Process...");
while(databaseEmails.size() > 0 || the202RetryQueue.size() > 0){
System.out.println("Wait 30 seconds to retry...");
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
while (the202RetryQueue.size() > 0) {
FliptopApiEmailUnit item = the202RetryQueue.poll();
if(item.getLastTry().before(DateUtils.addSeconds(Calendar.getInstance().getTime(), -30))){
FliptopApi.gerSocialDataViaEmail(item, "json", the202RetryQueue);
}
}
}
System.out.println("Finishing FliptopAPIRetryClass...");
}
public FliptopAPIRetryClass(
ConcurrentLinkedQueue<FliptopApiEmailUnit> databaseEmails,
ConcurrentLinkedQueue<FliptopApiEmailUnit> the202RetryQueue){
this.the202RetryQueue = the202RetryQueue;
this.databaseEmails = databaseEmails;
}
}
/**
* FliptopApiEmailUnit is a pojo to contain email and its retry information we might need
* during the execution.
*/
class FliptopApiEmailUnit {
String email;
Date lastTry;
int retryAttempt;
public FliptopApiEmailUnit(String email){
this.email = email;
this.lastTry = Calendar.getInstance().getTime();
this.retryAttempt = 0;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getLastTry() {
return lastTry;
}
public void setLastTry(Date lastTry) {
this.lastTry = lastTry;
}
public int getRetryAttempt() {
return retryAttempt;
}
public void setRetryAttempt(int retryAttempt) {
this.retryAttempt = retryAttempt;
}
}
/**
* FliptopApiResponseObject is a pojo for the returned fliptop API data.
* We use org.codehaus.jackson to convert the returned json data into
* this POJO.
*/
class FliptopApiResponseObject {
private String image_url;
private String name;
private String first_name;
private String last_name;
private String age;
private String gender;
private String location;
private String company;
private String title;
private String email;
private HashMap<String, String> memberships;
private HashMap<String, String> membership_scores;
private HashMap<String, String> influence_scores;
public String getImage_url() {
return image_url;
}
public void setImage_url(String image_url) {
this.image_url = image_url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public HashMap<String, String> getMemberships() {
return memberships;
}
public void setMemberships(HashMap<String, String> memberships) {
this.memberships = memberships;
}
public HashMap<String, String> getMembership_scores() {
return membership_scores;
}
public void setMembership_scores(HashMap<String, String> membership_scores) {
this.membership_scores = membership_scores;
}
public HashMap<String, String> getInfluence_scores() {
return influence_scores;
}
public void setInfluence_scores(HashMap<String, String> influence_scores) {
this.influence_scores = influence_scores;
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fliptop.api</groupId>
<artifactId>fliptop_api</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>fliptop_api</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>codehaus</id>
<url>http://repository.codehaus.org/org/codehaus</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment