Last active
August 29, 2015 13:57
-
-
Save shin1ogawa/9419682 to your computer and use it in GitHub Desktop.
This file contains 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.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<DirectoryRequest<User>> requests = | |
* Lists.newArrayListWithCapacity(userEmails.size()); | |
* for (String email : Arrays.asList("[email protected]", "[email protected]")) { | |
* requests.add(api.users().get(email)); | |
* } | |
* List<BatchResult<DirectoryRequest<User>, User>> results = | |
* new BatchWrapper<User>(api).execute(requests); | |
* for (BatchResult<DirectoryRequest<User>, User> 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> | |
* <dependency> | |
* <groupId>com.google.apis</groupId> | |
* <artifactId>google-api-services-admin-directory</artifactId> | |
* <version>directory_v1-rev28-1.17.0-rc</version> | |
* </dependency> | |
* </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