Permalink
Browse files

TFJ-630 contributor API support

removed:
StatusUpdate#setContributingTo(long)
configuration parameter added:
twitter4j.contributingTo
  • Loading branch information...
1 parent 44b574d commit 45500f134a36991810457048c2fdd8dac6cbae60 @yusuke committed Jul 18, 2012
View
37 twitter4j-core/src/main/java/twitter4j/StatusUpdate.java
@@ -39,7 +39,6 @@
private String mediaName;
private transient InputStream mediaBody;
private File mediaFile;
- private long contributingTo = -1L;
private static final long serialVersionUID = -3595502688477609916L;
public StatusUpdate(String status) {
@@ -138,39 +137,6 @@ public StatusUpdate media(String name, InputStream body) {
}
/**
- * Sets the user id contributing to
- *
- * @param contributingTo the user id contributing to
- * @see <a href="http://groups.google.com/group/twitter-api-announce/browse_thread/thread/12273c2d03c1b606">Developer Preview: Contributor API</a>
- * @since Twitter4J 3.0.0
- */
- public void setContributingTo(long contributingTo) {
- this.contributingTo = contributingTo;
- }
-
- /**
- * Sets the user id contributing to and returns this instance
- *
- * @param contributingTo the user id contributing to
- * @see <a href="http://groups.google.com/group/twitter-api-announce/browse_thread/thread/12273c2d03c1b606">Developer Preview: Contributor API</a>
- * @since Twitter4J 3.0.0
- */
- public StatusUpdate contributingTo(long contributingTo) {
- this.contributingTo = contributingTo;
- return this;
- }
-
- /**
- * Returns the user id contributing to
- *
- * @see <a href="http://groups.google.com/group/twitter-api-announce/browse_thread/thread/12273c2d03c1b606">Developer Preview: Contributor API</a>
- * @since Twitter4J 3.0.0
- */
- public long getContributingTo() {
- return this.contributingTo;
- }
-
- /**
* @since Twitter4J 2.2.5
*/
public void setPossiblySensitive(boolean possiblySensitive) {
@@ -242,9 +208,6 @@ public StatusUpdate annotation(Annotation annotation) {
params.add(new HttpParameter("media[]", mediaName, mediaBody));
params.add(new HttpParameter("possibly_sensitive", possiblySensitive));
}
- if (-1 != contributingTo) {
- appendParameter("contributingto", contributingTo, params);
- }
HttpParameter[] paramArray = new HttpParameter[params.size()];
return params.toArray(paramArray);
}
View
45 twitter4j-core/src/main/java/twitter4j/TwitterImpl.java
@@ -49,9 +49,15 @@
TwitterImpl(Configuration conf, Authorization auth) {
super(conf, auth);
INCLUDE_MY_RETWEET = new HttpParameter("include_my_retweet", 1);
+ if (conf.getContributingTo() != -1L) {
+ CONTRIBUTING_TO = new HttpParameter("contributingto", conf.getContributingTo());
+ } else {
+ CONTRIBUTING_TO = null;
+ }
}
private final HttpParameter INCLUDE_MY_RETWEET;
+ private final HttpParameter CONTRIBUTING_TO;
private HttpParameter[] mergeParameters(HttpParameter[] params1, HttpParameter[] params2) {
if (params1 != null && params2 != null) {
@@ -1753,6 +1759,13 @@ public TwitterAPIConfiguration getAPIConfiguration() throws TwitterException {
}
private HttpResponse get(String url) throws TwitterException {
+ if (CONTRIBUTING_TO != null) {
+ if (url.contains("?")) {
+ url = url + "&contributingto=" + conf.getContributingTo();
+ } else {
+ url = url + "?contributingto=" + conf.getContributingTo();
+ }
+ }
if (!conf.isMBeanEnabled()) {
return http.get(url, auth);
} else {
@@ -1769,15 +1782,15 @@ private HttpResponse get(String url) throws TwitterException {
}
}
- private HttpResponse get(String url, HttpParameter[] parameters) throws TwitterException {
+ private HttpResponse get(String url, HttpParameter[] params) throws TwitterException {
if (!conf.isMBeanEnabled()) {
- return http.get(url, parameters, auth);
+ return http.get(url, mergeContributingTo(params), auth);
} else {
// intercept HTTP call for monitoring purposes
HttpResponse response = null;
long start = System.currentTimeMillis();
try {
- response = http.get(url, parameters, auth);
+ response = http.get(url, mergeContributingTo(params), auth);
} finally {
long elapsedTime = System.currentTimeMillis() - start;
TwitterAPIMonitor.getInstance().methodCalled(url, elapsedTime, isOk(response));
@@ -1787,6 +1800,9 @@ private HttpResponse get(String url, HttpParameter[] parameters) throws TwitterE
}
private HttpResponse post(String url) throws TwitterException {
+ if (CONTRIBUTING_TO != null) {
+ return post(url, new HttpParameter[0]);
+ }
if (!conf.isMBeanEnabled()) {
return http.post(url, auth);
} else {
@@ -1803,15 +1819,15 @@ private HttpResponse post(String url) throws TwitterException {
}
}
- private HttpResponse post(String url, HttpParameter[] parameters) throws TwitterException {
+ private HttpResponse post(String url, HttpParameter[] params) throws TwitterException {
if (!conf.isMBeanEnabled()) {
- return http.post(url, parameters, auth);
+ return http.post(url, mergeContributingTo(params), auth);
} else {
// intercept HTTP call for monitoring purposes
HttpResponse response = null;
long start = System.currentTimeMillis();
try {
- response = http.post(url, parameters, auth);
+ response = http.post(url, mergeContributingTo(params), auth);
} finally {
long elapsedTime = System.currentTimeMillis() - start;
TwitterAPIMonitor.getInstance().methodCalled(url, elapsedTime, isOk(response));
@@ -1820,20 +1836,11 @@ private HttpResponse post(String url, HttpParameter[] parameters) throws Twitter
}
}
- private HttpResponse delete(String url) throws TwitterException {
- if (!conf.isMBeanEnabled()) {
- return http.delete(url, auth);
+ private HttpParameter[] mergeContributingTo(HttpParameter[] params) {
+ if (CONTRIBUTING_TO == null) {
+ return params;
} else {
- // intercept HTTP call for monitoring purposes
- HttpResponse response = null;
- long start = System.currentTimeMillis();
- try {
- response = http.delete(url, auth);
- } finally {
- long elapsedTime = System.currentTimeMillis() - start;
- TwitterAPIMonitor.getInstance().methodCalled(url, elapsedTime, isOk(response));
- }
- return response;
+ return mergeParameters(params, CONTRIBUTING_TO);
}
}
View
2 twitter4j-core/src/main/java/twitter4j/conf/Configuration.java
@@ -120,5 +120,7 @@
int getAsyncNumThreads();
+ long getContributingTo();
+
String getDispatcherImpl();
}
View
11 twitter4j-core/src/main/java/twitter4j/conf/ConfigurationBase.java
@@ -70,6 +70,7 @@
private int asyncNumThreads;
+ private long contributingTo;
private boolean includeRTsEnabled = true;
private boolean includeEntitiesEnabled = true;
@@ -156,6 +157,7 @@ protected ConfigurationBase() {
setOAuthAccessToken(null);
setOAuthAccessTokenSecret(null);
setAsyncNumThreads(1);
+ setContributingTo(-1L);
setClientVersion(Version.getVersion());
setClientURL("http://twitter4j.org/en/twitter4j-" + Version.getVersion() + ".xml");
setUserAgent("twitter4j http://twitter4j.org/ /" + Version.getVersion());
@@ -475,6 +477,15 @@ protected final void setAsyncNumThreads(int asyncNumThreads) {
}
@Override
+ public final long getContributingTo() {
+ return contributingTo;
+ }
+
+ protected final void setContributingTo(long contributingTo) {
+ this.contributingTo = contributingTo;
+ }
+
+ @Override
public final String getClientVersion() {
return clientVersion;
}
View
6 twitter4j-core/src/main/java/twitter4j/conf/ConfigurationBuilder.java
@@ -217,6 +217,12 @@ public ConfigurationBuilder setAsyncNumThreads(int asyncNumThreads) {
return this;
}
+ public ConfigurationBuilder setContributingTo(long contributingTo) {
+ checkNotBuilt();
+ configurationBean.setContributingTo(contributingTo);
+ return this;
+ }
+
public ConfigurationBuilder setClientVersion(String clientVersion) {
checkNotBuilt();
configurationBean.setClientVersion(clientVersion);
View
13 twitter4j-core/src/main/java/twitter4j/conf/PropertyConfiguration.java
@@ -72,6 +72,7 @@
public static final String UPLOAD_BASE_URL = "uploadBaseURL";
public static final String ASYNC_NUM_THREADS = "async.numThreads";
+ public static final String CONTRIBUTING_TO = "contributingTo";
public static final String ASYNC_DISPATCHER_IMPL = "async.dispatcherImpl";
public static final String INCLUDE_RTS = "includeRTs";
public static final String INCLUDE_ENTITIES = "includeEntities";
@@ -291,6 +292,9 @@ private void setFieldsWithPrefix(Properties props, String prefix) {
if (notNull(props, prefix, ASYNC_NUM_THREADS)) {
setAsyncNumThreads(getIntProperty(props, prefix, ASYNC_NUM_THREADS));
}
+ if (notNull(props, prefix, CONTRIBUTING_TO)) {
+ setContributingTo(getLongProperty(props, prefix, CONTRIBUTING_TO));
+ }
if (notNull(props, prefix, ASYNC_DISPATCHER_IMPL)) {
setDispatcherImpl(getString(props, prefix, ASYNC_DISPATCHER_IMPL));
}
@@ -390,6 +394,15 @@ protected int getIntProperty(Properties props, String prefix, String name) {
}
}
+ protected long getLongProperty(Properties props, String prefix, String name) {
+ String value = props.getProperty(prefix + name);
+ try {
+ return Long.parseLong(value);
+ } catch (NumberFormatException nfe) {
+ return -1L;
+ }
+ }
+
protected String getString(Properties props, String prefix, String name) {
return props.getProperty(prefix + name);
}

0 comments on commit 45500f1

Please sign in to comment.