Skip to content

Commit

Permalink
Add Account Activity API (beta) "GET account_activity/all/webhooks". …
Browse files Browse the repository at this point in the history
…fixes #324 @45M
  • Loading branch information
みぞ@CrazyBeatCoder committed Feb 19, 2018
1 parent 4b29455 commit 9180cbd
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 38 deletions.
@@ -1,12 +1,3 @@
/*
* 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;
Expand Down
@@ -1,12 +1,3 @@
/*
* 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;
Expand Down
@@ -0,0 +1,18 @@
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 ReturnsAllUrlsServlet extends HttpServlet {

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
try (UseCase useCase = new UseCase()) {
useCase.returnsAllUrls();
}
}
}
@@ -1,12 +1,3 @@
/*
* 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;
Expand Down
@@ -1,12 +1,3 @@
/*
* 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.difine.KeysAndAccessTokens;
Expand Down
Expand Up @@ -19,6 +19,18 @@ public void registersWebhookURL() {
mTwitterClient.registersWebhookURL();
}

/**
* Returns all URLs and their statuses for the given app for all events. Currently, only one
* webhook URL can be registered to an application. We mark a URL as invalid if it fails the daily
* validation check. In order to re-enable the URL, call the update endpoint.
*
* <p>指定されたすべてのイベントのURLとそのステータスを返します。 現在、Webhook URLはアプリケーションに1つしか登録できません。
* 毎日の妥当性チェックに失敗した場合は、URLを無効とマークします。 URLを再度有効にするには、更新エンドポイントを呼び出します。
*/
public void returnsAllUrls() {
mTwitterClient.returnsAllUrls();
}

public void subscriptions() {
mTwitterClient.subscriptions();
}
Expand Down
Expand Up @@ -15,6 +15,8 @@ public class TwitterClient {
private static final Logger LOG = Logger.getLogger(TwitterClient.class.getName());
private static final String TWITTER_API_ACCOUNT_ACTIVITY_WEBHOOKS_ENV_NAME_URL_STR =
"https://api.twitter.com/1.1/account_activity/all/env-beta/webhooks.json?url=https%3A%2F%2Fapi-project-93144643231.appspot.com%2Ftwitter_hook";
private static final String TWITTER_API_ACCOUNT_ACTIVITY_WEBHOOKS_URL_STR =
"https://api.twitter.com/1.1/account_activity/all/webhooks.json";
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;
Expand All @@ -38,6 +40,24 @@ public void registersWebhookURL() {
}
}

/**
* Returns all URLs and their statuses for the given app for all events. Currently, only one
* webhook URL can be registered to an application. We mark a URL as invalid if it fails the daily
* validation check. In order to re-enable the URL, call the update endpoint.
*
* <p>指定されたすべてのイベントのURLとそのステータスを返します。 現在、Webhook URLはアプリケーションに1つしか登録できません。
* 毎日の妥当性チェックに失敗した場合は、URLを無効とマークします。 URLを再度有効にするには、更新エンドポイントを呼び出します。
*/
public void returnsAllUrls() {
try {
HttpResponse ret = mTwitter4JUtil.get(TWITTER_API_ACCOUNT_ACTIVITY_WEBHOOKS_URL_STR);
LOG.log(Level.INFO, "returnsAllUrls ret.toString(): " + ret.toString());
LOG.log(Level.INFO, "returnsAllUrls ret.asString(): " + ret.asString());
} catch (TwitterException e) {
e.printStackTrace();
}
}

public void subscriptions() {
try {
Method method = mTwitter.getClass().getDeclaredMethod("post", String.class);
Expand All @@ -62,8 +82,8 @@ 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());
LOG.log(Level.INFO, "deactivatesSubscriptions ret.toString(): " + ret.toString());
LOG.log(Level.INFO, "deactivatesSubscriptions ret.asString(): " + ret.asString());
} catch (TwitterException e) {
e.printStackTrace();
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/twitter4j/Twitter4JUtil.java
Expand Up @@ -37,6 +37,10 @@ public Twitter getTwitter() {
return mTwitter;
}

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

private HttpResponse post(String url) throws TwitterException {
mTwitter.ensureAuthorizationEnabled();
if (!mTwitter.conf.isMBeanEnabled()) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Expand Up @@ -19,6 +19,14 @@
<servlet-name>subscriptions</servlet-name>
<url-pattern>/admin/subscriptions</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>returns_all_urls</servlet-name>
<servlet-class>com.mizo0203.twitter.account.activity.api.beta.samples.ReturnsAllUrlsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>returns_all_urls</servlet-name>
<url-pattern>/admin/returns_all_urls</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>deactivates_subscriptions</servlet-name>
<servlet-class>com.mizo0203.twitter.account.activity.api.beta.samples.DeactivatesSubscriptionsServlet
Expand Down
3 changes: 3 additions & 0 deletions src/main/webapp/index.html
Expand Up @@ -22,6 +22,9 @@ <h1>Hello App Engine!</h1>
<tr>
<td><a href="admin/registersWebhookURL">registersWebhookURL</a></td>
</tr>
<tr>
<td><a href="admin/returns_all_urls">returns_all_urls</a></td>
</tr>
<tr>
<td><a href="admin/subscriptions">subscriptions</a></td>
</tr>
Expand Down

0 comments on commit 9180cbd

Please sign in to comment.