Skip to content

Instantly share code, notes, and snippets.

@shin1ogawa
Last active August 29, 2015 13:57
Show Gist options
  • Save shin1ogawa/9419682 to your computer and use it in GitHub Desktop.
Save shin1ogawa/9419682 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.util.List;
import com.google.api.client.googleapis.batch.BatchRequest;
import com.google.api.client.googleapis.batch.json.JsonBatchCallback;
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonErrorContainer;
import com.google.api.client.http.HttpHeaders;
import com.google.api.services.admin.directory.Directory;
import com.google.api.services.admin.directory.DirectoryRequest;
import com.google.common.collect.Lists;
/**
* Wrapper for com.google.api.client.googleapis.batch.BatchRequest.
*
* <p>Sample code</p>
*
* <pre>
* List&lt;DirectoryRequest&lt;User&gt;&gt; requests =
* Lists.newArrayListWithCapacity(userEmails.size());
* for (String email : Arrays.asList("[email protected]", "[email protected]")) {
* requests.add(api.users().get(email));
* }
* List&lt;BatchResult&lt;DirectoryRequest&lt;User&gt;, User&gt;&gt; results =
* new BatchWrapper&lt;User&gt;(api).execute(requests);
* for (BatchResult&lt;DirectoryRequest&lt;User&gt;, User&gt; result : results) {
* Object key = result.getKey().get("userKey");
* if (result.getError() != null) {
* System.out.println("[!]" + key + ":" + result.getError());
* } else {
* System.out.println(key + ":" + result.getValue());
* }
* }
* </pre>
*
* <p>add dependency for Admin SDK.</p>
*
* <pre>
* &lt;dependency&gt;
* &lt;groupId&gt;com.google.apis&lt;/groupId&gt;
* &lt;artifactId&gt;google-api-services-admin-directory&lt;/artifactId&gt;
* &lt;version&gt;directory_v1-rev28-1.17.0-rc&lt;/version&gt;
* &lt;/dependency&gt;
* </pre>
* @author shin1ogawa
* @param <T> destination class type
*/
public class BatchWrapper<T> {
final Directory api;
final Class<T> dataClass;
/**
* the constructor.
* @param api Directory API
* @param clazzes
* @category constructor
*/
@SuppressWarnings("unchecked")
public BatchWrapper(Directory api, T... clazzes) {
this.api = api;
this.dataClass = (Class<T>) clazzes.getClass().getComponentType();
}
/**
* @param requests
* @return list of {@link BatchResult}
* @throws IOException
* @author shin1ogawa
*/
public List<BatchResult<DirectoryRequest<T>, T>> execute(List<DirectoryRequest<T>> requests)
throws IOException {
List<BatchResult<DirectoryRequest<T>, T>> results =
Lists.newArrayListWithCapacity(requests.size());
BatchRequest batch = api.batch();
for (DirectoryRequest<T> request : requests) {
BatchResult<DirectoryRequest<T>, T> callback =
new BatchResult<DirectoryRequest<T>, T>(request);
results.add(callback);
batch.queue(request.buildHttpRequest(), this.dataClass, GoogleJsonErrorContainer.class,
callback);
}
batch.execute();
return results;
}
/**
* @author shin1ogawa
* @param <K> Type of key
* @param <V> Type of value
*/
public static class BatchResult<K, V> extends JsonBatchCallback<V> {
final K key;
V value;
HttpHeaders responseHeaders;
GoogleJsonError error;
/**
* the constructor.
* @param key
* @category constructor
*/
public BatchResult(K key) {
this.key = key;
}
@Override
public void onSuccess(V value, HttpHeaders responseHeaders) {
this.value = value;
this.responseHeaders = responseHeaders;
}
@Override
public void onFailure(GoogleJsonError error, HttpHeaders responseHeaders) {
this.error = error;
this.responseHeaders = responseHeaders;
}
/**
* @return the value
* @category accessor
*/
public V getValue() {
return value;
}
/**
* @return the responseHeaders
* @category accessor
*/
public HttpHeaders getResponseHeaders() {
return responseHeaders;
}
/**
* @return the error
* @category accessor
*/
public GoogleJsonError getError() {
return error;
}
/**
* @return the key
* @category accessor
*/
public K getKey() {
return key;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment