Last active
August 29, 2015 13:58
-
-
Save thiagolocatelli/10022133 to your computer and use it in GitHub Desktop.
StripeCursor implementation and test
This file contains hidden or 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
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Map; | |
import com.stripe.model.StripeCollection; | |
import com.stripe.net.APIResource; | |
public class StripeCursor<T extends APIResource> implements Iterator<T> { | |
private boolean hasKey; | |
private String key; | |
private int current = 0; | |
private List<T> mLocalList = new ArrayList<T>(); | |
private T currentObject; | |
private Map<String, Object> queryParams; | |
private Class<T> localClass; | |
private StripeCollection<T> mStripeCollection; | |
public StripeCursor(Map<String, Object> params, Class<T> clazz) throws StripeCursorException { | |
queryParams = params; | |
queryParams.put("include[]", "total_count"); | |
localClass = clazz; | |
try { | |
query(); | |
} catch (Exception e) { | |
throw new StripeCursorException(e); | |
} | |
} | |
public StripeCursor(Map<String, Object> params, String apiKey, Class<T> clazz) | |
throws StripeCursorException { | |
key = apiKey; | |
hasKey = true; | |
localClass = clazz; | |
queryParams = params; | |
queryParams.put("include[]", "total_count"); | |
try { | |
query(); | |
} catch (Exception e) { | |
throw new StripeCursorException(e); | |
} | |
} | |
public int getCount() { | |
return mStripeCollection.getTotalCount(); | |
} | |
@Override | |
public boolean hasNext() { | |
return mStripeCollection.getHasMore(); | |
} | |
@Override | |
public T next() { | |
if(hasNext()) { | |
if(current == mLocalList.size() && mStripeCollection.getHasMore()) { | |
queryParams.put("starting_after", getId(currentObject)); | |
queryMore(); | |
} | |
currentObject = mLocalList.get(current); | |
current++; | |
return currentObject; | |
} | |
return null; | |
} | |
@Override | |
public void remove() { | |
//Not implemented, no reason to remove items from the list | |
} | |
private void queryMore() { | |
try { | |
query(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
@SuppressWarnings("unchecked") | |
private void query() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { | |
Method method = null; | |
Method[] allMethods = localClass.getDeclaredMethods(); | |
for (Method m : allMethods) { | |
if("all".equals(m.getName())) { | |
if(!hasKey && m.getParameterTypes().length == 1) { | |
method = m; | |
} | |
if(hasKey && m.getParameterTypes().length == 2) { | |
method = m; | |
} | |
} | |
} | |
Object object = localClass.newInstance(); | |
Object result = null; | |
if(method.getParameterTypes().length == 1) { | |
result = method.invoke(object, queryParams); | |
} | |
else { | |
result = method.invoke(object, queryParams, key); | |
} | |
mStripeCollection = (StripeCollection<T>) result; | |
mLocalList.addAll(mStripeCollection.getData()); | |
} | |
private String getId(Object object) { | |
Method getId = getMethodByName(object.getClass(), "getId"); | |
Object result = null; | |
try { | |
result = getId.invoke(object); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return (String) result; | |
} | |
@SuppressWarnings({ "rawtypes", "unchecked" }) | |
private Method getMethodByName(Class clazz, String name) { | |
Method method = null; | |
try { | |
method = clazz.getMethod(name); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return method; | |
} | |
public static class StripeCursorException extends Exception { | |
private static final long serialVersionUID = 1L; | |
public StripeCursorException(Exception rootCause) { | |
super(rootCause); | |
} | |
} | |
public StripeCollection<T> getCollection() { | |
return mStripeCollection; | |
} | |
} |
This file contains hidden or 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
public class TestCursor { | |
public static void main(String args[]) throws StripeCursorException { | |
String apiKey = "API KEY"; | |
Map<String, Object> createdParams = new HashMap<String, Object>(); | |
createdParams.put("gte", (int) (DateUtils.getDayInPast(30).getTime() / 1000)); | |
Map<String, Object> params = new HashMap<String, Object>(); | |
params.put("created", createdParams); | |
StripeCursor<Charge> cursor = new StripeCursor<Charge>(params, apiKey, Charge.class); | |
System.out.println("Total: " + cursor.getCount()); | |
while(cursor.hasNext()) { | |
Charge chg = cursor.next(); | |
System.out.println("amount: " + chg.getAmount()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment