public class DiffbotAPI extends Object implements Serializable
This class is thread-safe and should be instantiated only once. If the batch API is used this class has to be instantiated only once as it store the list request to execute in a batch.
The underlying implementations for the HTTP client (Apache, GAE url fetch, JVM default) and the JSON parser (Gson or
Jackson) can be chosen during construction DiffbotAPI(HttpTransport, JsonFactory, String).
For example to use Apache and Jackson:
private static DiffbotAPI api; ... api = new DiffbotAPI(new ApacheHttpTransport(), new JacksonFactory(), "your dev token");Note: google-http-client-jackson2 has to be in the classpath.
The Article API
Calling the API is as simple as :
Article article = api
.article(new ArticleRequest("http://www.cbs.com/shows/how_i_met_your_mother/barneys_blog/1000461/").withTags()
.withComments().withSummary());
The Frontpage API
Calling the API is as simple as :
Frontpage frontpage = api.frontpage(new FrontpageRequest("http://theverge.com"));
The Batch API
Future<Article> fArticle = api
.batch(new ArticleRequest("http://www.cbs.com/shows/how_i_met_your_mother/barneys_blog/1000461/").withTags()
.withComments().withSummary());
Future<Frontpage> fFrontpage = api.batch(new FrontpageRequest("http://theverge.com"));
Note that this can be done by multiple thread. The DiffbotAPI class is fully thread-safe. At this point no call has
been done to Diffbot. To obtain the results:
Article article = fArticle.get(); Frontpage frontpage = fFrontpage.get();The first line trigger a batch request that retrieves the results for all the requests added since the last call to
Future#get(). The second line doesn't need to do any API call as the result was retrieve during the execution
of the fist line.| Modifier and Type | Field and Description |
|---|---|
protected static String |
API_SERVER |
protected static String |
ARTICLE_URI |
protected static String |
BATCH_URI |
protected static String |
FRONTPAGE_URI |
| Constructor and Description |
|---|
DiffbotAPI(com.google.api.client.http.HttpTransport httpTransport,
com.google.api.client.json.JsonFactory jsonFactory,
String token)
Constructor allowing to set the Diffbot developer token and the underlying implementations for the HTTP client
and the JSON parser.
|
| Modifier and Type | Method and Description |
|---|---|
Article |
article(ArticleRequest articleRequest)
Call the Diffbot Article API.
|
Future<Article> |
batch(ArticleRequest articleRequest)
Add an
ArticleRequest to the batch list. |
Future<Frontpage> |
batch(FrontpageRequest frontpageRequest)
Add an
FrontpageRequest to the batch list. |
protected void |
executeBatch(Future<? extends Object> initiator) |
Frontpage |
frontpage(FrontpageRequest frontpageRequest)
Call the Diffbot Frontpage API.
|
protected static final String BATCH_URI
protected static final String API_SERVER
protected static final String ARTICLE_URI
protected static final String FRONTPAGE_URI
public DiffbotAPI(com.google.api.client.http.HttpTransport httpTransport,
com.google.api.client.json.JsonFactory jsonFactory,
String token)
throws JAXBException
The HTTP implementation can be:
ApacheHttpTransport: Apache HTTP client.NetHttpTransport: Default JVM HTTP client.UrlFetchTransport: URL
Fetch to use with Google App Engine.httpTransport - an implementation of HttpTransportjsonFactory - an implementation of JsonFactorytoken - your Diffbot developer tokenJAXBException - if JAXB is not in the classpathpublic final Article article(ArticleRequest articleRequest) throws DiffbotUnauthorizedException, DiffbotServerException, DiffbotIOException, DiffbotAPIException
articleRequest - a ArticleRequest filled with the parameters to call the Article APIArticle representing the Diffbot API resultDiffbotUnauthorizedException - if the developer token is not recognized or revokedDiffbotServerException - if a HTTP error occurs on the Diffbot serverDiffbotIOException - if an IO error (usually network related) occur during the API callDiffbotAPIException - if an API error occur on Diffbot servers while processing the requestpublic final Frontpage frontpage(FrontpageRequest frontpageRequest) throws DiffbotUnauthorizedException, DiffbotServerException, DiffbotIOException, DiffbotAPIException, DiffbotParseException
frontpageRequest - a FrontpageRequest filled with the parameters to call the Frontpage APIFrontpage representing the Diffbot API resultDiffbotUnauthorizedException - if the developer token is not recognized or revokedDiffbotServerException - if a HTTP error occurs on the Diffbot serverDiffbotIOException - if an IO error (usually network related) occur during the API callDiffbotAPIException - if an API error occur on Diffbot servers while processing the requestDiffbotParseException - if the Diffbot response cannot be parsedpublic final Future<Frontpage> batch(FrontpageRequest frontpageRequest)
FrontpageRequest to the batch list.frontpageRequest - the FrontpageRequest to add to the listFuture<Frontpage> that will be filled with an Frontpage on the next batch callpublic final Future<Article> batch(ArticleRequest articleRequest)
ArticleRequest to the batch list.articleRequest - the ArticleRequest to add to the listFuture<Article> that will be filled with an Article on the next batch callprotected final void executeBatch(Future<? extends Object> initiator) throws DiffbotUnauthorizedException, DiffbotServerException, DiffbotIOException
Copyright © 2013. All Rights Reserved.