Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
twitter4j.TwitterAdsClient Maven / Gradle / Ivy
package twitter4j;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import twitter4j.auth.Authorization;
import twitter4j.auth.OAuthSupport;
import twitter4j.conf.Configuration;
import twitter4j.models.ads.HttpVerb;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
import static twitter4j.util.TwitterAdUtil.constructBaseAdsResponse;
/**
* User: abhay
* Date: 4/4/16
* Time: 7:07 PM
*/
public class TwitterAdsClient extends TwitterImpl implements OAuthSupport {
public static final String ADS_API_URL = "https://ads-api.twitter.com/";
public static final Gson GSON_INSTANCE = new Gson();
public static TwitterAdsClient getInstance(Configuration conf, Authorization auth) {
return new TwitterAdsClient(conf, auth);
}
public TwitterAdsClient(Configuration conf, Authorization auth) {
super(conf, auth);
}
private String getImplicitParamsStr() {
return StringUtils.EMPTY;
}
public String getBaseAdsAPIUrl() {
return ADS_API_URL;//conf.getAdsAPIURL();
}
public BaseAdsListResponseIterable executeHttpListRequest(String baseUrl, List params, Type type) throws TwitterException {
return executeHttpListRequest(baseUrl, params, type, false);
}
public BaseAdsListResponseIterable executeHttpListRequest(String baseUrl, List params, Type type,
boolean isCostBasedRateLimit) throws TwitterException {
HttpResponse httpResponse;
if (params != null) {
httpResponse = get(baseUrl, params.toArray(new HttpParameter[params.size()]));
} else {
httpResponse = get(baseUrl);
}
BaseAdsListResponseIterable response;
try {
response = constructBaseAdsListResponse(baseUrl, httpResponse, params, type, isCostBasedRateLimit);
} catch (IOException e) {
throw new TwitterException("Failed to parse response.", e);
}
return response;
}
public BaseAdsListResponseIterable constructBaseAdsListResponse(String baseUrl, HttpResponse httpResponse, List params,
Type type, boolean isCostBasedRateLimit)
throws TwitterException, IOException {
return new BaseAdsListResponseIterable<>(this, baseUrl, params, type, httpResponse, isCostBasedRateLimit);
}
public BaseAdsResponse executeHttpRequest(String baseUrl, HttpParameter[] params, Type type, HttpVerb httpVerb) throws TwitterException {
HttpResponse httpResponse;
String stringResponse;
BaseAdsResponse response = null;
switch (httpVerb) {
case GET:
try {
httpResponse = get(baseUrl, params);
stringResponse = httpResponse.asString();
response = constructBaseAdsResponse(httpResponse, stringResponse, type);
} catch (IOException e) {
throw new TwitterException("Failed to parse response.", e);
}
break;
case PUT:
try {
httpResponse = put(baseUrl, params);
stringResponse = httpResponse.asString();
response = constructBaseAdsResponse(httpResponse, stringResponse, type);
} catch (IOException e) {
throw new TwitterException("Failed to parse response.", e);
}
break;
case POST:
try {
httpResponse = postRequest(baseUrl, params);
stringResponse = httpResponse.asString();
response = constructBaseAdsResponse(httpResponse, stringResponse, type);
} catch (IOException e) {
throw new TwitterException("Failed to parse response.", e);
}
break;
case DELETE:
try {
httpResponse = delete(baseUrl, params);
stringResponse = httpResponse.asString();
response = constructBaseAdsResponse(httpResponse, stringResponse, type);
} catch (IOException e) {
throw new TwitterException("Failed to parse response.", e);
}
break;
}
return response;
}
public HttpResponse postRequest(String url, HttpParameter[] params) throws TwitterException {
return post(url, params);
}
public HttpResponse getRequest(String url, HttpParameter[] params) throws TwitterException {
return get(url, params);
}
public HttpResponse getRequest(String url) throws TwitterException {
return get(url);
}
public HttpResponse getWithoutMergeOfParams(String url, HttpParameter[] params) throws TwitterException {
return getWithoutMergingImplicitParams(url, params);
}
public HttpResponse putRequest(String url, HttpParameter[] params) throws TwitterException {
return put(url, params);
}
public T executeRequest(String baseUrl, HttpParameter[] params, Type typeToken, HttpVerb httpVerb) throws TwitterException {
HttpResponse httpResponse;
String stringResponse;
T response = null;
switch (httpVerb) {
case GET:
try {
httpResponse = getRequest(baseUrl, params);
stringResponse = httpResponse.asString();
response = constructHTTPRequestResponse(stringResponse, typeToken);
} catch (IOException e) {
throw new TwitterException("Failed to parse response.", e);
}
break;
case PUT:
try {
httpResponse = put(baseUrl, params);
stringResponse = httpResponse.asString();
response = constructHTTPRequestResponse(stringResponse, typeToken);
} catch (IOException e) {
throw new TwitterException("Failed to parse response.", e);
}
break;
case POST:
try {
httpResponse = postRequest(baseUrl, params);
stringResponse = httpResponse.asString();
response = constructHTTPRequestResponse(stringResponse, typeToken);
} catch (IOException e) {
throw new TwitterException("Failed to parse response.", e);
}
break;
case DELETE:
try {
httpResponse = delete(baseUrl, params);
stringResponse = httpResponse.asString();
response = constructHTTPRequestResponse(stringResponse, typeToken);
} catch (IOException e) {
throw new TwitterException("Failed to parse response.", e);
}
break;
}
return response;
}
public Configuration getConf() {
return super.getConfiguration();
}
// ------------------------------------------- Private Methods -------------------------------------------------
private T constructHTTPRequestResponse(String response, Type typeToken) throws IOException {
return GSON_INSTANCE.fromJson(response, typeToken);
}
public HttpResponse get(String url) throws TwitterException {
ensureAuthorizationEnabled();
if (!conf.isMBeanEnabled()) {
return http.get(url, null, auth, this);
} else {
// intercept HTTP call for monitoring purposes
HttpResponse response = null;
long start = System.currentTimeMillis();
try {
response = http.get(url, null, auth, this);
} finally {
long elapsedTime = System.currentTimeMillis() - start;
TwitterAPIMonitor.getInstance().methodCalled(url, elapsedTime, isOk(response));
}
return response;
}
}
public HttpResponse get(String url, HttpParameter... params) throws TwitterException {
ensureAuthorizationEnabled();
if (!conf.isMBeanEnabled()) {
return http.get(url, params, auth, this);
} else {
// intercept HTTP call for monitoring purposes
HttpResponse response = null;
long start = System.currentTimeMillis();
try {
response = http.get(url, params, auth, this);
} finally {
long elapsedTime = System.currentTimeMillis() - start;
TwitterAPIMonitor.getInstance().methodCalled(url, elapsedTime, isOk(response));
}
return response;
}
}
public HttpResponse post(String url, HttpParameter... params) throws TwitterException {
ensureAuthorizationEnabled();
if (!conf.isMBeanEnabled()) {
return http.post(url, params, auth, this);
} else {
// intercept HTTP call for monitoring purposes
HttpResponse response = null;
long start = System.currentTimeMillis();
try {
response = http.post(url, params, auth, this);
} finally {
long elapsedTime = System.currentTimeMillis() - start;
TwitterAPIMonitor.getInstance().methodCalled(url, elapsedTime, isOk(response));
}
return response;
}
}
private boolean isOk(HttpResponse response) {
return response != null && response.getStatusCode() < 300;
}
protected HttpResponse put(String url, HttpParameter[] params) throws TwitterException {
ensureAuthorizationEnabled();
if (!conf.isMBeanEnabled()) {
return http.put(url, params, auth, null);
} else {
// intercept HTTP call for monitoring purposes
HttpResponse response = null;
long start = System.currentTimeMillis();
try {
response = http.put(url, params, auth, null);
} finally {
long elapsedTime = System.currentTimeMillis() - start;
TwitterAPIMonitor.getInstance().methodCalled(url, elapsedTime, isOk(response));
}
return response;
}
}
protected HttpResponse delete(String url) throws TwitterException {
ensureAuthorizationEnabled();
if (!conf.isMBeanEnabled()) {
return http.delete(url, null, auth, null);
} else {
// intercept HTTP call for monitoring purposes
HttpResponse response = null;
long start = System.currentTimeMillis();
try {
response = http.delete(url, null, auth, null);
} finally {
long elapsedTime = System.currentTimeMillis() - start;
TwitterAPIMonitor.getInstance().methodCalled(url, elapsedTime, isOk(response));
}
return response;
}
}
protected HttpResponse delete(String url, HttpParameter[] params) throws TwitterException {
ensureAuthorizationEnabled();
if (!conf.isMBeanEnabled()) {
return http.delete(url, params, auth, null);
} else {
// intercept HTTP call for monitoring purposes
HttpResponse response = null;
long start = System.currentTimeMillis();
try {
response = http.delete(url, params, auth, null);
} finally {
long elapsedTime = System.currentTimeMillis() - start;
TwitterAPIMonitor.getInstance().methodCalled(url, elapsedTime, isOk(response));
}
return response;
}
}
protected HttpResponse getWithoutMergingImplicitParams(String url, HttpParameter[] params) throws TwitterException {
ensureAuthorizationEnabled();
if (!conf.isMBeanEnabled()) {
return http.get(url, params, auth, null);
} else {
// intercept HTTP call for monitoring purposes
HttpResponse response = null;
long start = System.currentTimeMillis();
try {
response = http.get(url, params, auth, null);
} finally {
long elapsedTime = System.currentTimeMillis() - start;
TwitterAPIMonitor.getInstance().methodCalled(url, elapsedTime, isOk(response));
}
return response;
}
}
}