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.impl.TwitterAdsLineItemApiImpl Maven / Gradle / Ivy
package twitter4j.impl;
import com.google.common.base.Optional;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.StringUtils;
import twitter4j.BaseAdsListResponse;
import twitter4j.BaseAdsListResponseIterable;
import twitter4j.BaseAdsResponse;
import twitter4j.TwitterAdsClient;
import twitter4j.api.TwitterAdsLineItemApi;
import twitter4j.internal.http.HttpParameter;
import twitter4j.internal.http.HttpResponse;
import twitter4j.internal.models4j.TwitterException;
import twitter4j.models.ads.*;
import twitter4j.models.ads.sort.LineItemsSortByField;
import twitter4j.models.ads.sort.PromotedAccountsSortByField;
import twitter4j.models.video.AssociateMediaCreativeResponse;
import twitter4j.models.video.TwitterCallToActionType;
import twitter4j.models.video.TwitterPreRollCallToAction;
import twitter4j.util.TwitterAdUtil;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static twitter4j.TwitterAdsConstants.*;
/**
* User: abhay
* Date: 4/4/16
* Time: 8:01 PM
*/
public class TwitterAdsLineItemApiImpl implements TwitterAdsLineItemApi {
private static final Integer MAX_REQUEST_PARAMETER_SIZE = 50;
private final TwitterAdsClient twitterAdsClient;
public TwitterAdsLineItemApiImpl(TwitterAdsClient twitterAdsClient) {
this.twitterAdsClient = twitterAdsClient;
}
@Override
public BaseAdsResponse createLineItem(LineItem lineItem) throws TwitterException {
String accountId = lineItem.getAccountId();
String campaignId = lineItem.getCampaignId();
boolean automaticallySelectBid = lineItem.isAutomaticallySelectBid();
Long bidAmountLocalMicro = null;
BidType bidType;
if (!automaticallySelectBid) {
bidAmountLocalMicro = lineItem.getBidAmtInMicro();
bidType = lineItem.getBidType();
} else {
bidType = BidType.AUTO;
}
Boolean paused = lineItem.getPaused();
Sentiments includeSentiment = lineItem.getSentiment();
Boolean matchRelevantPopularQueries = lineItem.getMatchRelevantPopularQueries();
TwitterAdUtil.ensureNotNull(accountId, "accountId");
List params =
validateCreateLineItemParameters(Optional.fromNullable(campaignId), bidType, Optional.fromNullable(bidAmountLocalMicro),
automaticallySelectBid, Optional.fromNullable(lineItem.getProductType()),
lineItem.getPlacements(), lineItem.getEntityStatus(), Optional.fromNullable(includeSentiment),
Optional.fromNullable(matchRelevantPopularQueries), Optional.fromNullable(lineItem.getObjective()),
Optional.fromNullable(lineItem.getChargeBy()), Optional.fromNullable(lineItem.getBidUnit()),
Optional.fromNullable(lineItem.getAdvertiserDomain()), lineItem.getCategories(), lineItem.getWebEventTag());
HttpParameter[] parameters = null;
if (!params.isEmpty()) {
parameters = params.toArray(new HttpParameter[params.size()]);
}
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_LINE_ITEMS;
HttpResponse httpResponse = twitterAdsClient.postRequest(baseUrl, parameters);
try {
Type type = new TypeToken>() {
}.getType();
return TwitterAdUtil.constructBaseAdsResponse(httpResponse, httpResponse.asString(), type);
} catch (IOException e) {
throw new TwitterException("Failed to parse line item.");
}
}
@Override
public BaseAdsResponse updateLineItem(String accountId, String lineItemId, BidType bidType, boolean automaticallySelectBid,
Optional bidAmountLocalMicro, EntityStatus status, Optional includeSentiment,
Optional matchRelevantPopularQueries, Optional chargeBy,
Optional bidUnit, Optional advertiserDomain,
String[] iabCategories) throws TwitterException {
if (automaticallySelectBid) {
bidAmountLocalMicro = null;
bidType = BidType.AUTO;
}
List params =
validateUpdateLineItemParameters(accountId, lineItemId, bidType, automaticallySelectBid, bidAmountLocalMicro, status,
includeSentiment, matchRelevantPopularQueries, chargeBy, bidUnit, advertiserDomain, iabCategories);
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_LINE_ITEMS +
lineItemId;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(baseUrl, params.toArray(new HttpParameter[params.size()]), type, HttpVerb.PUT);
}
@Override
public BaseAdsResponse deleteLineItem(String accountId, String lineItemId) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, "Account Id");
TwitterAdUtil.ensureNotNull(lineItemId, "LineItem Id");
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_LINE_ITEMS + lineItemId;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(baseUrl, null, type, HttpVerb.DELETE);
}
@Override
public BaseAdsListResponseIterable getAllLineItems(String accountId, Optional> campaignIds, Optional> lineItemIds,
Optional> fundingInstrumentIds, Optional count, boolean withDeleted,
String cursor, Optional sortByField) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, "accountId");
String campaignIdsAsString = null;
String lineItemIdsAsString = null;
String fundingInstrumentIdsAsString = null;
if (campaignIds != null && campaignIds.isPresent()) {
TwitterAdUtil.ensureMaxSize(campaignIds.get(), MAX_REQUEST_PARAMETER_SIZE);
campaignIdsAsString = TwitterAdUtil.getCsv(campaignIds.get());
}
if (lineItemIds != null && lineItemIds.isPresent()) {
TwitterAdUtil.ensureMaxSize(lineItemIds.get(), MAX_REQUEST_PARAMETER_SIZE);
lineItemIdsAsString = TwitterAdUtil.getCsv(lineItemIds.get());
}
if (lineItemIds != null && fundingInstrumentIds.isPresent()) {
TwitterAdUtil.ensureMaxSize(fundingInstrumentIds.get(), MAX_REQUEST_PARAMETER_SIZE);
fundingInstrumentIdsAsString = TwitterAdUtil.getCsv(fundingInstrumentIds.get());
}
List params =
validateLineItemParameters(accountId, campaignIdsAsString, lineItemIdsAsString, fundingInstrumentIdsAsString, count, withDeleted,
cursor);
if (sortByField.isPresent()) {
params.add(new HttpParameter(PARAM_SORT_BY, sortByField.get().getField()));
}
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_LINE_ITEMS;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpListRequest(baseUrl, params, type);
}
@Override
public BaseAdsResponse getLineItemById(String accountId, String lineItemId, boolean withDeleted) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, "accountId");
TwitterAdUtil.ensureNotNull(lineItemId, "lineItemId");
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_LINE_ITEMS + lineItemId;
HttpParameter[] params = new HttpParameter[]{new HttpParameter(PARAM_WITH_DELETED, withDeleted)};
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(baseUrl, params, type, HttpVerb.GET);
}
@Override
public BaseAdsResponse createPromotedAccounts(String accountId, String lineItemId, String userId) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, "Account Id");
TwitterAdUtil.ensureNotNull(lineItemId, "Line Item Id");
TwitterAdUtil.ensureNotNull(userId, "User Id");
List params = new ArrayList<>();
params.add(new HttpParameter(PARAM_LINE_ITEM_ID, lineItemId));
params.add(new HttpParameter(PARAM_USER_ID, userId));
HttpResponse httpResponse;
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_PROMOTED_ACCOUNTS;
httpResponse = twitterAdsClient.postRequest(baseUrl, params.toArray(new HttpParameter[params.size()]));
try {
Type type = new TypeToken>() {
}.getType();
return TwitterAdUtil.constructBaseAdsResponse(httpResponse, httpResponse.asString(), type);
} catch (IOException e) {
throw new TwitterException("Failed to parse promoted accounts.");
}
}
@Override
public BaseAdsListResponseIterable getPromotedAccounts(String accountId, Optional> promotedAccountIds,
Optional lineItemId, boolean withDeleted,
PromotedAccountsSortByField sortByField) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, "accountId");
String promotedAccountsIdsAsString = null;
List params = new ArrayList<>();
params.add(new HttpParameter(PARAM_WITH_DELETED, withDeleted));
if (promotedAccountIds != null && promotedAccountIds.isPresent()) {
TwitterAdUtil.ensureMaxSize(promotedAccountIds.get(), MAX_REQUEST_PARAMETER_SIZE);
promotedAccountsIdsAsString = TwitterAdUtil.getCsv(promotedAccountIds.get());
}
if (lineItemId != null && lineItemId.isPresent()) {
params.add(new HttpParameter(PARAM_LINE_ITEM_ID, lineItemId.get()));
}
if (StringUtils.isNotBlank(promotedAccountsIdsAsString)) {
params.add(new HttpParameter(PARAM_PROMOTED_ACCOUNTS_IDS, promotedAccountsIdsAsString));
}
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_PROMOTED_ACCOUNTS;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpListRequest(baseUrl, params, type);
}
@Override
public BaseAdsResponse createCallToActionDetailsForPreRollViews(String accountId, String lineItemId,
TwitterCallToActionType twitterCallToActionType,
String callToActionUrl) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, "Account Id");
TwitterAdUtil.ensureNotNull(lineItemId, "Line Item Id");
TwitterAdUtil.ensureNotNull(callToActionUrl, "Call To Action Url");
TwitterAdUtil.ensureNotNull(twitterCallToActionType, "Call To Action Type");
List params = new ArrayList<>();
params.add(new HttpParameter(PARAM_LINE_ITEM_ID, lineItemId));
params.add(new HttpParameter(PARAM_ACCOUNT_ID, accountId));
params.add(new HttpParameter(PARAM_CALL_TO_ACTION, twitterCallToActionType.name()));
params.add(new HttpParameter(PARAM_CALL_TO_ACTION_URL, callToActionUrl));
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PRE_ROLL_CALL_TO_ACTION;
HttpResponse httpResponse = twitterAdsClient.postRequest(baseUrl, params.toArray(new HttpParameter[params.size()]));
try {
Type type = new TypeToken>() {
}.getType();
return TwitterAdUtil.constructBaseAdsResponse(httpResponse, httpResponse.asString(), type);
} catch (IOException e) {
throw new TwitterException("Failed to parse call to action response.");
}
}
//landing url is the url of the media creative
@Override
public BaseAdsResponse associateMediaCreativeWithAccount(String accountId, String lineItemId,
String accountMediaId, String landingUrl)
throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, "Account Id");
TwitterAdUtil.ensureNotNull(lineItemId, "Line Item Id");
TwitterAdUtil.ensureNotNull(accountMediaId, "Account Media Id");
TwitterAdUtil.ensureNotNull(landingUrl, " Landing Url");
List params = new ArrayList<>();
params.add(new HttpParameter(PARAM_ACCOUNT_ID, accountId));
params.add(new HttpParameter(PARAM_LINE_ITEM_ID, lineItemId));
params.add(new HttpParameter(PARAM_ACCOUNT_MEDIA_ID, accountMediaId));
if (TwitterAdUtil.isNotNullOrEmpty(landingUrl)) {
params.add(new HttpParameter(PARAM_LANDING_URL, landingUrl));
}
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_MEDIA_CREATIVES;
HttpResponse httpResponse = twitterAdsClient.postRequest(baseUrl, params.toArray(new HttpParameter[params.size()]));
try {
Type type = new TypeToken>() {
}.getType();
return TwitterAdUtil.constructBaseAdsResponse(httpResponse, httpResponse.asString(), type);
} catch (IOException e) {
throw new TwitterException("Failed to parse response for associate media to account", e);
}
}
@Override
public BaseAdsListResponseIterable getPromotedAccounts(String accountId, Collection promotedAccountIds, String lineItemId, boolean withDeleted) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, "accountId");
String promotedAccountsIdsAsString = null;
List params = new ArrayList<>();
params.add(new HttpParameter(PARAM_WITH_DELETED, withDeleted));
if (TwitterAdUtil.isNotNull(promotedAccountIds)) {
TwitterAdUtil.ensureMaxSize(promotedAccountIds, MAX_REQUEST_PARAMETER_SIZE);
promotedAccountsIdsAsString = TwitterAdUtil.getCsv(promotedAccountIds);
}
if (TwitterAdUtil.isNotNullOrEmpty(lineItemId)) {
params.add(new HttpParameter(PARAM_LINE_ITEM_ID, lineItemId));
}
if (TwitterAdUtil.isNotNullOrEmpty(promotedAccountsIdsAsString)) {
params.add(new HttpParameter(PARAM_PROMOTED_ACCOUNTS_IDS, promotedAccountsIdsAsString));
}
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_PROMOTED_ACCOUNTS;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpListRequest(baseUrl, params, type);
}
// -------------------------------------- Private Methods ------------------------------------------
private List validateCreateLineItemParameters(Optional campaignId, BidType bidType, Optional bidAmountLocalMicro,
boolean automaticallySelectBid, Optional productType, List placements,
String status, Optional includeSentiment, Optional matchRelevantPopularQueries,
Optional objective, Optional chargeBy, Optional bidUnit, Optional advertiserDomain,
String[] categories, String webEventTag) {
if (bidType == BidType.TARGET || bidType == BidType.MAX) {
TwitterAdUtil.ensureNotNull(bidAmountLocalMicro, "Bid amount cannot be null for TARGET or MAX Bid Type");
}
List params = new ArrayList<>();
if (campaignId != null && campaignId.isPresent()) {
params.add(new HttpParameter(PARAM_CAMPAIGN_ID, campaignId.get()));
}
if (automaticallySelectBid) {
params.add(new HttpParameter(AUTOMATICALLY_SELECT_BID, true));
} else if (bidAmountLocalMicro != null && bidAmountLocalMicro.isPresent()) {
params.add(new HttpParameter(PARAM_BID_AMOUNT_LOCAL_MICRO, bidAmountLocalMicro.get()));
if (bidType != null) {
params.add(new HttpParameter(PARAM_BID_TYPE, bidType.name()));
}
}
if (chargeBy != null && chargeBy.isPresent()) {
params.add(new HttpParameter(PARAM_CHARGE_BY, chargeBy.get()));
}
if (bidUnit != null && bidUnit.isPresent()) {
params.add(new HttpParameter(PARAM_BID_UNIT, bidUnit.get()));
}
if (StringUtils.isNotEmpty(webEventTag)) {
params.add(new HttpParameter(PARAM_PRIMARY_WEB_EVENT_TAG, webEventTag));
}
if (status != null) {
params.add(new HttpParameter(PARAM_ENTITY_STATUS, status));
}
if (includeSentiment != null && includeSentiment.isPresent()) {
params.add(new HttpParameter(PARAM_INCLUDE_SENTIMENT, includeSentiment.get().name()));
}
if (matchRelevantPopularQueries != null && matchRelevantPopularQueries.isPresent()) {
params.add(new HttpParameter(PARAM_MATCH_RELEVANT_POPULAR_QUERIES, matchRelevantPopularQueries.get()));
}
if (objective != null && objective.isPresent()) {
params.add(new HttpParameter(PARAM_OBJECTIVE, objective.get()));
// Twitter Audience Platform is supported for these objectives only
if (TwitterAdUtil.TWEET_ENGAGEMENTS.equals(objective.get()) || TwitterAdUtil.VIDEO_VIEWS.equals(objective.get()) ||
TwitterAdUtil.WEBSITE_CLICKS.equals(objective.get())) {
if (advertiserDomain != null && advertiserDomain.isPresent()) {
params.add(new HttpParameter(PARAM_ADVERTISER_DOMAIN, advertiserDomain.get()));
}
if (categories != null && TwitterAdUtil.isNotEmpty(Arrays.asList(categories))) {
params.add(new HttpParameter(PARAM_CATEGORIES, TwitterAdUtil.getCsv(Arrays.asList(categories))));
}
}
}
if (productType != null && productType.isPresent()) {
params.add(new HttpParameter(PARAM_PRODUCT_TYPE, productType.get().name()));
}
if (TwitterAdUtil.isNotEmpty(placements)) {
params.add(new HttpParameter(PARAM_PLACEMENTS, TwitterAdUtil.getCsv(placements)));
}
return params;
}
private List validateUpdateLineItemParameters(String accountId, String lineItemId, BidType bidType, boolean automaticallySelectBid,
Optional bidAmountLocalMicro, EntityStatus status, Optional includeSentiment,
Optional matchRelevantPopularQueries, Optional chargeBy, Optional bidUnit,
Optional advertiserDomain, String[] iabCategories) {
TwitterAdUtil.ensureNotNull(accountId, "AccountId");
TwitterAdUtil.ensureNotNull(lineItemId, "Line Item Id");
if (bidType == BidType.TARGET || bidType == BidType.MAX) {
TwitterAdUtil.ensureNotNull(bidAmountLocalMicro, "Bid amount cannot be null for TARGET or MAX Bid Type");
}
List params = new ArrayList<>();
if (automaticallySelectBid) {
params.add(new HttpParameter(AUTOMATICALLY_SELECT_BID, true));
params.add(new HttpParameter(PARAM_BID_AMOUNT_LOCAL_MICRO, ""));
} else if (bidAmountLocalMicro != null && bidAmountLocalMicro.isPresent()) {
if (bidType != BidType.TARGET) {
params.add(new HttpParameter(AUTOMATICALLY_SELECT_BID, false));
}
params.add(new HttpParameter(PARAM_BID_AMOUNT_LOCAL_MICRO, bidAmountLocalMicro.get()));
}
if (!automaticallySelectBid && bidType != null) {
params.add(new HttpParameter(PARAM_BID_TYPE, bidType.name()));
}
if (chargeBy != null && chargeBy.isPresent()) {
params.add(new HttpParameter(PARAM_CHARGE_BY, chargeBy.get()));
}
if (bidUnit != null && bidUnit.isPresent()) {
params.add(new HttpParameter(PARAM_BID_UNIT, bidUnit.get()));
}
if (status != null) {
params.add(new HttpParameter(PARAM_ENTITY_STATUS, status.name()));
}
if (includeSentiment != null && includeSentiment.isPresent()) {
params.add(new HttpParameter(PARAM_INCLUDE_SENTIMENT, includeSentiment.get().name()));
}
if (matchRelevantPopularQueries != null && matchRelevantPopularQueries.isPresent()) {
params.add(new HttpParameter(PARAM_MATCH_RELEVANT_POPULAR_QUERIES, matchRelevantPopularQueries.get()));
}
// Twitter Audience Platform is supported for these objectives only
if (advertiserDomain != null && advertiserDomain.isPresent()) {
params.add(new HttpParameter(PARAM_ADVERTISER_DOMAIN, advertiserDomain.get()));
}
if (iabCategories != null && TwitterAdUtil.isNotEmpty(Arrays.asList(iabCategories))) {
params.add(new HttpParameter(PARAM_CATEGORIES, TwitterAdUtil.getCsv(Arrays.asList(iabCategories))));
}
return params;
}
private List validateLineItemParameters(String accountId, String campaignIds, String lineItemIds, String fundingInstrumentIds,
Optional count, boolean withDeleted, String cursor) {
TwitterAdUtil.ensureNotNull(accountId, "accountId");
List params = new ArrayList<>();
if (StringUtils.isNotBlank(campaignIds)) {
params.add(new HttpParameter(PARAM_CAMPAIGN_IDS, campaignIds));
}
if (StringUtils.isNotBlank(lineItemIds)) {
params.add(new HttpParameter(PARAM_LINE_ITEM_IDS, lineItemIds));
}
if (StringUtils.isNotBlank(fundingInstrumentIds)) {
params.add(new HttpParameter(PARAM_FUNDING_INSTRUMENT_IDS, fundingInstrumentIds));
}
params.add(new HttpParameter(PARAM_WITH_DELETED, withDeleted));
if (count != null && count.isPresent()) {
params.add(new HttpParameter(PARAM_COUNT, count.get()));
}
if (StringUtils.isNotBlank(cursor)) {
params.add(new HttpParameter(PARAM_CURSOR, cursor));
}
return params;
}
}