Skip to content

Commit

Permalink
Add Account Activity API (beta) "DELETE account_activity/all/:env_nam…
Browse files Browse the repository at this point in the history
…e/subscriptions". fixes #331 @2h
  • Loading branch information
みぞ@CrazyBeatCoder committed Feb 18, 2018
1 parent a83d285 commit 4b29455
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2015 Google Inc. <p> Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy of the
* License at <p> http://www.apache.org/licenses/LICENSE-2.0 <p> Unless required by applicable law
* or agreed to in writing, software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*/

package com.mizo0203.twitter.account.activity.api.beta.samples;

import com.mizo0203.twitter.account.activity.api.beta.samples.domain.UseCase;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class DeactivatesSubscriptionsServlet extends HttpServlet {

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
try (UseCase useCase = new UseCase()) {
useCase.deactivatesSubscriptions();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,15 @@ public void registersWebhookURL() {
public void subscriptions() {
mTwitterClient.subscriptions();
}

/**
* Deactivates subscription(s) for the provided user context and app for all activities. After
* deactivation, all All events for the requesting user will no longer be sent to the webhook URL.
*
* <p>提供されたユーザーコンテキストのサブスクリプションとすべてのアクティビティのアプリケーションを非アクティブ化します。
* 非アクティブ化後、要求元ユーザのすべてのすべてのイベントはWebHook URLに送信されなくなります。
*/
public void deactivatesSubscriptions() {
mTwitterClient.deactivatesSubscriptions();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.mizo0203.twitter.account.activity.api.beta.samples.repo;

import com.mizo0203.twitter.account.activity.api.beta.samples.domain.difine.KeysAndAccessTokens;
import twitter4j.HttpResponse;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.Twitter4JUtil;
import twitter4j.TwitterException;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand All @@ -19,17 +18,11 @@ public class TwitterClient {
private static final String TWITTER_API_ACCOUNT_ACTIVITY_SUBSCRIPTIONS_ENV_NAME_URL_STR =
"https://api.twitter.com/1.1/account_activity/all/env-beta/subscriptions.json";
private final Twitter mTwitter;
private final Twitter4JUtil mTwitter4JUtil;

public TwitterClient() {
mTwitter = createTwitterInstance();
}

private static Twitter createTwitterInstance() {
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(KeysAndAccessTokens.CONSUMER_KEY, KeysAndAccessTokens.CONSUMER_SECRET);
twitter.setOAuthAccessToken(
new AccessToken(KeysAndAccessTokens.TOKEN, KeysAndAccessTokens.TOKEN_SECRET));
return twitter;
mTwitter4JUtil = new Twitter4JUtil();
mTwitter = mTwitter4JUtil.getTwitter();
}

public void registersWebhookURL() {
Expand Down Expand Up @@ -57,4 +50,22 @@ public void subscriptions() {
LOG.log(Level.SEVERE, "subscriptions", e);
}
}

/**
* Deactivates subscription(s) for the provided user context and app for all activities. After
* deactivation, all All events for the requesting user will no longer be sent to the webhook URL.
*
* <p>提供されたユーザーコンテキストのサブスクリプションとすべてのアクティビティのアプリケーションを非アクティブ化します。
* 非アクティブ化後、要求元ユーザのすべてのすべてのイベントはWebHook URLに送信されなくなります。
*/
public void deactivatesSubscriptions() {
try {
HttpResponse ret =
mTwitter4JUtil.delete(TWITTER_API_ACCOUNT_ACTIVITY_SUBSCRIPTIONS_ENV_NAME_URL_STR);
LOG.log(Level.INFO, "subscriptions ret.toString(): " + ret.toString());
LOG.log(Level.INFO, "subscriptions ret.asString(): " + ret.asString());
} catch (TwitterException e) {
e.printStackTrace();
}
}
}
65 changes: 65 additions & 0 deletions src/main/java/twitter4j/Twitter4JUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package twitter4j;

import com.mizo0203.twitter.account.activity.api.beta.samples.domain.difine.KeysAndAccessTokens;
import twitter4j.auth.AccessToken;

import java.lang.reflect.Field;

public class Twitter4JUtil {
private final HttpParameter[] IMPLICIT_PARAMS;
private final TwitterImpl mTwitter;

public Twitter4JUtil() {
mTwitter = createTwitterInstance();
IMPLICIT_PARAMS = createHttpParameterArray(mTwitter);
}

private static TwitterImpl createTwitterInstance() {
TwitterImpl twitter = (TwitterImpl) new TwitterFactory().getInstance();
twitter.setOAuthConsumer(KeysAndAccessTokens.CONSUMER_KEY, KeysAndAccessTokens.CONSUMER_SECRET);
twitter.setOAuthAccessToken(
new AccessToken(KeysAndAccessTokens.TOKEN, KeysAndAccessTokens.TOKEN_SECRET));
return twitter;
}

private static HttpParameter[] createHttpParameterArray(TwitterImpl twitter) {
try {
Field field = twitter.getClass().getDeclaredField("IMPLICIT_PARAMS");
field.setAccessible(true);
return (HttpParameter[]) field.get(twitter);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
}

public Twitter getTwitter() {
return mTwitter;
}

private HttpResponse post(String url) throws TwitterException {
mTwitter.ensureAuthorizationEnabled();
if (!mTwitter.conf.isMBeanEnabled()) {
return mTwitter.http.post(url, IMPLICIT_PARAMS, mTwitter.auth, mTwitter);
} else {
// intercept HTTP call for monitoring purposes
HttpResponse response = null;
long start = System.currentTimeMillis();
try {
response = mTwitter.http.post(url, IMPLICIT_PARAMS, mTwitter.auth, mTwitter);
} finally {
long elapsedTime = System.currentTimeMillis() - start;
TwitterAPIMonitor.getInstance().methodCalled(url, elapsedTime, isOk(response));
}
return response;
}
}

public HttpResponse delete(String url) throws TwitterException {
return mTwitter.http.delete(url, IMPLICIT_PARAMS, mTwitter.auth, mTwitter);
}

private boolean isOk(HttpResponse response) {
return response != null && response.getStatusCode() < 300;
}
}
10 changes: 10 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
<servlet-name>subscriptions</servlet-name>
<url-pattern>/admin/subscriptions</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>deactivates_subscriptions</servlet-name>
<servlet-class>com.mizo0203.twitter.account.activity.api.beta.samples.DeactivatesSubscriptionsServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>deactivates_subscriptions</servlet-name>
<url-pattern>/admin/deactivates_subscriptions</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>twitter_hook</servlet-name>
<servlet-class>com.mizo0203.twitter.account.activity.api.beta.samples.TwitterHookHandlerServlet</servlet-class>
Expand Down
3 changes: 3 additions & 0 deletions src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ <h1>Hello App Engine!</h1>
<tr>
<td><a href="admin/subscriptions">subscriptions</a></td>
</tr>
<tr>
<td><a href="admin/deactivates_subscriptions">deactivates_subscriptions</a></td>
</tr>
</table>
</body>
</html>

0 comments on commit 4b29455

Please sign in to comment.