twitter4jads.impl.TwitterAdsAccountApiImpl Maven / Gradle / Ivy
The newest version!
package twitter4jads.impl;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.collections.CollectionUtils;
import twitter4jads.BaseAdsListResponse;
import twitter4jads.BaseAdsListResponseIterable;
import twitter4jads.BaseAdsResponse;
import twitter4jads.TwitterAdsClient;
import twitter4jads.api.TwitterAdsAccountApi;
import twitter4jads.internal.http.HttpParameter;
import twitter4jads.internal.http.HttpResponse;
import twitter4jads.internal.models4j.TwitterException;
import twitter4jads.models.ads.AdAccount;
import twitter4jads.models.ads.HttpVerb;
import twitter4jads.models.ads.PromotableUser;
import twitter4jads.models.ads.TwitterAccountPermissions;
import twitter4jads.models.ads.sort.AccountsSortByField;
import twitter4jads.util.TwitterAdUtil;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import static twitter4jads.TwitterAdsConstants.*;
/**
* User: abhay
* Date: 4/4/16
* Time: 7:54 PM
*/
public class TwitterAdsAccountApiImpl implements TwitterAdsAccountApi {
private final TwitterAdsClient twitterAdsClient;
public TwitterAdsAccountApiImpl(TwitterAdsClient twitterAdsClient) {
this.twitterAdsClient = twitterAdsClient;
}
@Override
public BaseAdsListResponseIterable getAllAccounts(boolean withDeleted, Optional sortByField) throws TwitterException {
List param = new ArrayList<>();
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2;
param.add(new HttpParameter(PARAM_WITH_DELETED, withDeleted));
if (sortByField!= null && sortByField.isPresent()) {
param.add(new HttpParameter(PARAM_SORT_BY, sortByField.get().getField()));
}
Type type = new TypeToken>() {}.getType();
return twitterAdsClient.executeHttpListRequest(baseUrl, param, type);
}
@Override
public BaseAdsResponse getAdAccountById(String accountId, boolean withDeleted) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, "accountId");
HttpParameter[] param;
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId;
param = new HttpParameter[]{new HttpParameter(PARAM_WITH_DELETED, withDeleted)};
Type type = new TypeToken>() {}.getType();
return twitterAdsClient.executeHttpRequest(baseUrl, param, type, HttpVerb.GET);
}
@Override
public List getAccountPermissions(String accountId) throws TwitterException {
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_FEATURES;
HttpResponse httpResponse = twitterAdsClient.getWithoutMergeOfParams(baseUrl, null);
List permissionsFromChannel = Lists.newArrayList();
try {
Type type = new TypeToken>() {}.getType();
BaseAdsListResponse permissions = TwitterAdUtil.constructBaseAdsListResponse(httpResponse, httpResponse.asString(), type);
if (permissions == null || CollectionUtils.isEmpty(permissions.getData())) {
throw new TwitterException("Empty response returned for Account Permissions");
}
List data = permissions.getData();
for (String key : data) {
TwitterAccountPermissions accountPermission = TwitterAccountPermissions.getAccountPermission(key);
if (accountPermission != null) {
permissionsFromChannel.add(accountPermission);
}
}
} catch (IOException e) {
throw new TwitterException("Exception occurred while getting the Account Permissions", e);
}
return permissionsFromChannel;
}
@Override
public BaseAdsListResponseIterable getPromotableUsers(String accountId, boolean withDeleted) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, "accountId");
List params = new ArrayList<>();
params.add(new HttpParameter(PARAM_WITH_DELETED, withDeleted));
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_PROMOTABLE_USERS;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpListRequest(baseUrl, params, type);
}
}