twitter4jads.impl.TwitterAdsCardsApiImpl Maven / Gradle / Ivy
The newest version!
package twitter4jads.impl;
import com.google.common.collect.Lists;
import com.google.gson.reflect.TypeToken;
import twitter4jads.*;
import twitter4jads.api.TwitterAdsCardsApi;
import twitter4jads.models.ads.cards.*;
import org.apache.commons.lang3.StringUtils;
import twitter4jads.BaseAdsListResponse;
import twitter4jads.BaseAdsListResponseIterable;
import twitter4jads.BaseAdsResponse;
import twitter4jads.TwitterAdsClient;
import twitter4jads.internal.http.HttpParameter;
import twitter4jads.internal.models4j.Media;
import twitter4jads.internal.models4j.MediaUpload;
import twitter4jads.internal.models4j.TwitterException;
import twitter4jads.models.ads.HttpVerb;
import twitter4jads.models.media.TwitterLibraryMedia;
import twitter4jads.util.TwitterAdUtil;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static twitter4jads.util.TwitterAdUtil.isNotNullOrEmpty;
/**
*
* Date: 4/4/16
* Time: 8:10 PM
*/
public class TwitterAdsCardsApiImpl implements TwitterAdsCardsApi {
private static final int CONVERSATION_CARD_HASHTAG_LENGTH = 20;
private static final int DM_CARD_CTA_LENGTH = 20;
private static final int CONVERSATION_CARD_TWEET_LENGTH = 256;
private final TwitterAdsClient twitterAdsClient;
public TwitterAdsCardsApiImpl(TwitterAdsClient twitterAdsClient) {
this.twitterAdsClient = twitterAdsClient;
}
@SuppressWarnings("Duplicates")
@Override
public BaseAdsListResponseIterable getAllImageAppDownloadCards(String accountId, List cardIds,
boolean withDeleted, Optional count)
throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, "AccountId");
List params = Lists.newArrayList();
params.add(new HttpParameter(TwitterAdsConstants.PARAM_WITH_DELETED, withDeleted));
if (TwitterAdUtil.isNotEmpty(cardIds)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_CARD_IDS, TwitterAdUtil.getCsv(cardIds)));
}
if (count != null && count.isPresent()) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_COUNT, count.get()));
}
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_IMAGE_APP_DOWNLOAD_CARDS;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpListRequest(url, params, type);
}
@SuppressWarnings("Duplicates")
@Override
public BaseAdsListResponseIterable getAllVideoAppDownloadCards(String accountId, List cardIds,
boolean withDeleted, Optional count)
throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
final List params = Lists.newArrayList();
params.add(new HttpParameter(TwitterAdsConstants.PARAM_WITH_DELETED, withDeleted));
if (TwitterAdUtil.isNotEmpty(cardIds)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_CARD_IDS, TwitterAdUtil.getCsv(cardIds)));
}
if (count != null && count.isPresent()) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_COUNT, count.get()));
}
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_VIDEO_APP_DOWNLOAD_CARDS;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpListRequest(url, params, type);
}
@SuppressWarnings("Duplicates")
@Override
public BaseAdsListResponseIterable getAllImageDMCards(String accountId, List cardIds, boolean withDeleted,
Integer count) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
final String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_IMAGE_DM_CARDS;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpListRequest(url, Lists.newArrayList(), type);
}
@SuppressWarnings("Duplicates")
@Override
public BaseAdsResponse deleteWebsiteCard(String accountId, String cardId) throws TwitterException {
TwitterAdUtil.ensureNotNull(cardId, TwitterAdsConstants.CARD_ID);
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_WEBSITE_CARDS + cardId;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, null, type, HttpVerb.DELETE);
}
@SuppressWarnings("Duplicates")
@Override
public BaseAdsResponse getWebsiteCard(String accountId, String cardId) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
TwitterAdUtil.ensureNotNull(cardId, TwitterAdsConstants.CARD_ID);
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_WEBSITE_CARDS + cardId;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, null, type, HttpVerb.GET);
}
@SuppressWarnings("Duplicates")
@Override
public BaseAdsListResponseIterable getAllWebsiteCards(String accountId, List cardIds, boolean withDeleted,
Optional count) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
List params = Lists.newArrayList();
params.add(new HttpParameter(TwitterAdsConstants.PARAM_WITH_DELETED, withDeleted));
if (TwitterAdUtil.isNotEmpty(cardIds)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_CARD_IDS, TwitterAdUtil.getCsv(cardIds)));
}
if (count != null && count.isPresent()) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_COUNT, count.get()));
}
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_WEBSITE_CARDS;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpListRequest(url, params, type);
}
@SuppressWarnings("Duplicates")
@Override
public BaseAdsResponse deleteVideoAppDownloadCard(String accountId, String cardId) throws TwitterException {
TwitterAdUtil.ensureNotNull(cardId, "Card Id");
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_VIDEO_APP_DOWNLOAD_CARDS + cardId;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, null, type, HttpVerb.DELETE);
}
@SuppressWarnings("Duplicates")
@Override
public BaseAdsResponse deleteImageAppDownloadCard(String accountId, String cardId) throws TwitterException {
TwitterAdUtil.ensureNotNull(cardId, "Card Id");
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_IMAGE_APP_DOWNLOAD_CARDS + cardId;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, null, type, HttpVerb.DELETE);
}
@SuppressWarnings("Duplicates")
@Override
public BaseAdsListResponseIterable getAllImageConversationCards(String accountId, List cardIds, boolean withDeleted, Integer count) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
final List params = Lists.newArrayList();
params.add(new HttpParameter(TwitterAdsConstants.PARAM_WITH_DELETED, withDeleted));
if (TwitterAdUtil.isNotEmpty(cardIds)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_CARD_IDS, TwitterAdUtil.getCsv(cardIds)));
}
if (TwitterAdUtil.isNotNull(count)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_COUNT, count));
}
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_IMAGE_CONVERSATION_CARDS;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpListRequest(url, params, type);
}
@SuppressWarnings("Duplicates")
@Override
public BaseAdsListResponseIterable getAllVideoConversationCards(String accountId, List cardIds, boolean withDeleted, Integer count) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
final List params = Lists.newArrayList();
params.add(new HttpParameter(TwitterAdsConstants.PARAM_WITH_DELETED, withDeleted));
if (TwitterAdUtil.isNotEmpty(cardIds)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_CARD_IDS, TwitterAdUtil.getCsv(cardIds)));
}
if (TwitterAdUtil.isNotNull(count)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_COUNT, count));
}
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_VIDEO_CONVERSATION_CARDS;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpListRequest(url, params, type);
}
@SuppressWarnings("Duplicates")
@Override
public BaseAdsListResponseIterable getAllVideoWebsiteCards(String accountId, List cardIds, boolean withDeleted,
Integer count) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
final List params = Lists.newArrayList();
params.add(new HttpParameter(TwitterAdsConstants.PARAM_WITH_DELETED, withDeleted));
if (TwitterAdUtil.isNotEmpty(cardIds)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_CARD_IDS, TwitterAdUtil.getCsv(cardIds)));
}
if (TwitterAdUtil.isNotNull(count)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_COUNT, count));
}
final String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_VIDEO_WEBSITE_CARDS;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpListRequest(url, params, type);
}
@SuppressWarnings("Duplicates")
@Override
public BaseAdsListResponseIterable getAllVideoDMCards(String accountId, List cardIds, boolean withDeleted,
Integer count) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
final List params = Lists.newArrayList();
params.add(new HttpParameter(TwitterAdsConstants.PARAM_WITH_DELETED, withDeleted));
if (TwitterAdUtil.isNotEmpty(cardIds)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_CARD_IDS, TwitterAdUtil.getCsv(cardIds)));
}
if (TwitterAdUtil.isNotNull(count)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_COUNT, count));
}
final String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_VIDEO_DM_CARDS;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpListRequest(url, params, type);
}
@SuppressWarnings("Duplicates")
@Override
public BaseAdsResponse updateWebsiteCard(String accountId, String name, String cardId, String websiteTitle, String websiteUrl, String imageMediaKey) throws TwitterException {
TwitterAdUtil.ensureNotNull(cardId, "Card Id");
List params = validateAndCreateParamsForCreateWebsiteCard(accountId, name, websiteTitle, websiteUrl, imageMediaKey);
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_WEBSITE_CARDS + cardId;
HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, parameters, type, HttpVerb.PUT);
}
@Override
public BaseAdsResponse createWebsiteCard(String accountId, String name, String websiteTitle, String websiteUrl, String imageMediaKey) throws TwitterException {
final List params = validateAndCreateParamsForCreateWebsiteCard(accountId, name, websiteTitle, websiteUrl, imageMediaKey);
final String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_WEBSITE_CARDS;
final HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, parameters, type, HttpVerb.POST);
}
@Override
public BaseAdsResponse createImageAppDownloadCard(String accountId, String name, String countryCode, String iphoneAppId, String ipadAppId, String googlePlayAppId, String iphoneDeepLink, String ipadDeepLink, String googlePlayDeepLink, String imageMediaKey, String callToAction) throws TwitterException {
List params =
validateAndCreateParamsForCreateImageAppDownloadCard(accountId, name, countryCode, iphoneAppId, ipadAppId, googlePlayAppId,
iphoneDeepLink, ipadDeepLink, googlePlayDeepLink, imageMediaKey, callToAction);
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_IMAGE_APP_DOWNLOAD_CARDS;
HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, parameters, type, HttpVerb.POST);
}
@Override
public BaseAdsResponse updateImageAppDownloadCard(String accountId, String name, String cardId, String countryCode, String iphoneAppId, String ipadAppId, String googlePlayAppId, String iphoneDeepLink, String ipadDeepLink, String googlePlayDeepLink, String imageMediaKey, String callToAction) throws TwitterException {
TwitterAdUtil.ensureNotNull(cardId, "Card Id");
List params =
validateAndCreateParamsForUpdateImageAppDownloadCard(accountId, name, countryCode, iphoneAppId, ipadAppId, googlePlayAppId,
iphoneDeepLink, ipadDeepLink, googlePlayDeepLink, imageMediaKey, callToAction);
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_IMAGE_APP_DOWNLOAD_CARDS + cardId;
HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, parameters, type, HttpVerb.PUT);
}
@Override
public BaseAdsResponse createVideoAppDownloadCard(String accountId, String name, String countryCode, String iphoneAppId, String ipadAppId, String googlePlayAppId, String iphoneDeepLink, String ipadDeepLink, String googlePlayDeepLink, String posterMediaKey, String callToAction, TwitterLibraryMedia video) throws TwitterException, IOException, InterruptedException {
List params =
validateAndCreateParamsForCreateVideoAppDownloadCard(accountId, name, countryCode, iphoneAppId, ipadAppId, googlePlayAppId,
iphoneDeepLink, ipadDeepLink, googlePlayDeepLink, posterMediaKey, video.getMediaKey(),
callToAction);
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_VIDEO_APP_DOWNLOAD_CARDS;
HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
Type type = new TypeToken>() {
}.getType();
final BaseAdsResponse twitterVideoAppDownloadResponse =
twitterAdsClient.executeHttpRequest(url, parameters, type, HttpVerb.POST);
final TwitterVideoAppDownloadCard videoAppDownloadCard = twitterVideoAppDownloadResponse.getData();
if (video.getMediaKey() != null) {
videoAppDownloadCard.setMediaKey(video.getMediaKey());
}
if (video.getMediaUrl() != null) {
videoAppDownloadCard.setMediaUrl(video.getMediaUrl());
}
if (video.getDuration() != null) {
videoAppDownloadCard.setChannelVideoLength(video.getDuration().toString());
}
videoAppDownloadCard.setPosterMediaKey(posterMediaKey);
return twitterVideoAppDownloadResponse;
}
@Override
public BaseAdsResponse updateVideoAppDownloadCard(String accountId, String name, String cardId, String countryCode, String iphoneAppId, String ipadAppId, String googlePlayAppId, String iphoneDeepLink, String ipadDeepLink, String googlePlayDeepLink, String posterMediaKey, String callToActionValue, TwitterLibraryMedia video) throws TwitterException, IOException, InterruptedException {
TwitterAdUtil.ensureNotNull(cardId, "Card Id");
String channelVideoId = null;
if (video != null) {
channelVideoId = video.getMediaKey();
}
List params =
validateAndCreateParamsForUpdateVideoAppDownloadCard(accountId, name, countryCode, iphoneAppId, ipadAppId, googlePlayAppId,
iphoneDeepLink, ipadDeepLink, googlePlayDeepLink, posterMediaKey, channelVideoId,
callToActionValue);
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_VIDEO_APP_DOWNLOAD_CARDS + cardId;
HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
Type type = new TypeToken>() {
}.getType();
final BaseAdsResponse twitterVideoAppDownloadResponse =
twitterAdsClient.executeHttpRequest(url, parameters, type, HttpVerb.PUT);
final TwitterVideoAppDownloadCard videoAppDownloadCard = twitterVideoAppDownloadResponse.getData();
if (video != null) {
videoAppDownloadCard.setMediaKey(video.getMediaKey());
}
if (video != null) {
videoAppDownloadCard.setMediaUrl(video.getMediaUrl());
}
if (video != null) {
videoAppDownloadCard.setChannelVideoLength(video.getDuration().toString());
}
videoAppDownloadCard.setPosterMediaKey(posterMediaKey);
return twitterVideoAppDownloadResponse;
}
@Override
public BaseAdsResponse createImageConversationCard(String accountId, String name, String title, String firstHashtag,
String firstTweet, String secondHashtag, String secondTweet,
String thirdHashtag, String thirdTweet, String fourthHashtag,
String fourthTweet, String thanksText, String thanksUrl,
String imageUrl, String imageMediaKey) throws TwitterException {
final List params =
validateAndCreateParamsForCreateImageConversationCard(accountId, name, title, firstHashtag, firstTweet, secondHashtag, secondTweet,
thirdHashtag, thirdTweet, fourthHashtag, fourthTweet, thanksText, thanksUrl,
imageUrl);
params.add(new HttpParameter(TwitterAdsConstants.PARAM_MEDIA_KEY, imageMediaKey));
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_IMAGE_CONVERSATION_CARDS;
HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, parameters, type, HttpVerb.POST);
}
@Override
public BaseAdsResponse updateImageConversationCard(String accountId, String cardId, String name, String title, String firstHashtag, String firstTweet, String secondHashtag,
String secondTweet, String thirdHashtag, String thirdTweet,
String fourthHashtag, String fourthTweet, String thanksText,
String thanksUrl, String imageUrl, String imageMediaKey)
throws TwitterException {
final BaseAdsResponse response = getImageConversationCard(accountId, cardId);
final TwitterImageConversationCard existingCard = response.getData();
final List params =
validateAndCreateParamsForUpdateImageConversationCard(accountId, name, title, firstHashtag, firstTweet, secondHashtag, secondTweet,
thirdHashtag, thirdTweet, fourthHashtag, fourthTweet, thanksText, thanksUrl,
imageUrl, existingCard);
params.add(new HttpParameter(TwitterAdsConstants.PARAM_MEDIA_KEY, imageMediaKey));
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_IMAGE_CONVERSATION_CARDS + cardId;
HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, parameters, type, HttpVerb.PUT);
}
@SuppressWarnings("Duplicates")
@Override
public BaseAdsResponse deleteImageConversationCard(String accountId, String cardId) throws TwitterException {
TwitterAdUtil.ensureNotNull(cardId, TwitterAdsConstants.CARD_ID);
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_IMAGE_CONVERSATION_CARDS + cardId;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, null, type, HttpVerb.DELETE);
}
@Override
public BaseAdsResponse createVideoConversationCard(String accountId, String name, String title, String firstHashtag,
String firstTweet, String secondHashtag, String secondTweet,
String thirdHashtag, String thirdTweet, String fourthHashtag,
String fourthTweet, String thanksText, String thanksUrl,
String posterMediaKey, TwitterLibraryMedia twitterVideo)
throws TwitterException {
TwitterAdUtil.ensureNotNull(twitterVideo.getMediaKey(), "Video Key");
final List params =
validateAndCreateParamsForCreateVideoConversationCard(accountId, name, title, firstHashtag, firstTweet, secondHashtag, secondTweet,
thirdHashtag, thirdTweet, fourthHashtag, fourthTweet, thanksText, thanksUrl);
if (isNotNullOrEmpty(posterMediaKey)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_POSTER_MEDIA_KEY, posterMediaKey));
}
params.add(new HttpParameter(TwitterAdsConstants.PARAM_MEDIA_KEY, twitterVideo.getMediaKey()));
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_VIDEO_CONVERSATION_CARDS;
HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, parameters, type, HttpVerb.POST);
}
@Override
public BaseAdsResponse updateVideoConversationCard(String accountId, String cardId, String name, String title, String firstHashtag, String firstTweet, String secondHashtag,
String secondTweet, String thirdHashtag, String thirdTweet,
String fourthHashtag, String fourthTweet, String thanksText,
String thanksUrl, String posterMediaKey,
TwitterLibraryMedia twitterVideo) throws TwitterException {
final BaseAdsResponse response = getVideoConversationCard(accountId, cardId);
final TwitterVideoConversationCard existingCard = response.getData();
final List params =
validateAndCreateParamsForUpdateVideoConversationCard(accountId, name, title, firstHashtag, firstTweet, secondHashtag, secondTweet,
thirdHashtag, thirdTweet, fourthHashtag, fourthTweet, thanksText, thanksUrl,
existingCard);
if (isNotNullOrEmpty(posterMediaKey)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_POSTER_MEDIA_KEY, posterMediaKey));
}
if (twitterVideo != null) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_MEDIA_KEY, twitterVideo.getMediaKey()));
}
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_VIDEO_CONVERSATION_CARDS + cardId;
HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, parameters, type, HttpVerb.PUT);
}
@SuppressWarnings("Duplicates")
@Override
public BaseAdsResponse deleteVideoConversationCard(String accountId, String cardId) throws TwitterException {
TwitterAdUtil.ensureNotNull(cardId, TwitterAdsConstants.CARD_ID);
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_VIDEO_CONVERSATION_CARDS + cardId;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, null, type, HttpVerb.DELETE);
}
@Override
public BaseAdsResponse createImageDMCard(String accountId, String name, String firstCta, Long firstWelcomeMessageId, String secondCta, Long secondWelcomeMessageId, String thirdCta, Long thirdWelcomeMessageId, String fourthCta, Long fourthWelcomeMessageId, Long recipientAccountId, String imageUrl, String imageMediaKey) throws TwitterException {
final List params =
validateAndCreateParamsForCreateImageDMCard(accountId, name, firstCta, firstWelcomeMessageId, secondCta, secondWelcomeMessageId, thirdCta,
thirdWelcomeMessageId, fourthCta, fourthWelcomeMessageId, recipientAccountId, imageUrl);
if (TwitterAdUtil.isNotNullOrEmpty(imageMediaKey)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_MEDIA_KEY, imageMediaKey));
}
final String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_IMAGE_DM_CARDS;
final HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, parameters, type, HttpVerb.POST);
}
@Override
public BaseAdsResponse createVideoDMCard(String accountId, String name, String firstCta, Long firstWelcomeMessageId, String secondCta, Long secondWelcomeMessageId, String thirdCta, Long thirdWelcomeMessageId, String fourthCta, Long fourthWelcomeMessageId, Long recipientAccountId, String imageUrl, String videoUrl, String posterMediaKey, String videoMediaKey) throws TwitterException {
final List params =
validateAndCreateParamsForCreateVideoDMCard(accountId, name, firstCta, firstWelcomeMessageId, secondCta, secondWelcomeMessageId, thirdCta,
thirdWelcomeMessageId, fourthCta, fourthWelcomeMessageId, recipientAccountId, videoUrl);
if (StringUtils.isNotBlank(posterMediaKey)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_IMAGE_MEDIA_ID, posterMediaKey));
}
params.add(new HttpParameter(TwitterAdsConstants.PARAM_MEDIA_KEY, videoMediaKey));
final String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_VIDEO_DM_CARDS;
final HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, parameters, type, HttpVerb.POST);
}
@Override
public BaseAdsResponse updateImageDMCard(String accountId, String name, String firstCta, Long firstWelcomeMessageId, String secondCta, Long secondWelcomeMessageId, String thirdCta, Long thirdWelcomeMessageId, String fourthCta, Long fourthWelcomeMessageId, String imageUrl, String imageMediaKey, String cardChannelId) throws TwitterException {
final List params =
validateAndCreateParamsForUpdateDMCard(accountId, name, firstCta, firstWelcomeMessageId, secondCta, secondWelcomeMessageId, thirdCta,
thirdWelcomeMessageId, fourthCta, fourthWelcomeMessageId, cardChannelId);
if (TwitterAdUtil.isNotNullOrEmpty(imageMediaKey)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_POSTER_MEDIA_KEY, imageMediaKey));
}
final String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_IMAGE_DM_CARDS + cardChannelId;
final HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, parameters, type, HttpVerb.PUT);
}
@Override
public BaseAdsResponse updateVideoDMCard(String accountId, String name, String firstCta, Long firstWelcomeMessageId, String secondCta, Long secondWelcomeMessageId, String thirdCta, Long thirdWelcomeMessageId, String fourthCta, Long fourthWelcomeMessageId, String imageUrl, String videoUrl, String posterMediaKey, String videoMediaKey, String channelId) throws TwitterException {
final List params =
validateAndCreateParamsForUpdateDMCard(accountId, name, firstCta, firstWelcomeMessageId, secondCta, secondWelcomeMessageId, thirdCta,
thirdWelcomeMessageId, fourthCta, fourthWelcomeMessageId, channelId);
if (TwitterAdUtil.isNotNullOrEmpty(posterMediaKey)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_POSTER_MEDIA_KEY, posterMediaKey));
}
if (TwitterAdUtil.isNotNullOrEmpty(videoMediaKey)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_MEDIA_KEY, videoMediaKey));
}
final String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_VIDEO_DM_CARDS + channelId;
final HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, parameters, type, HttpVerb.PUT);
}
@SuppressWarnings("Duplicates")
@Override
public BaseAdsResponse deleteImageDMCard(String accountId, String cardId) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
TwitterAdUtil.ensureNotNull(cardId, TwitterAdsConstants.CARD_ID);
final String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_IMAGE_DM_CARDS + cardId;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, null, type, HttpVerb.DELETE);
}
@SuppressWarnings("Duplicates")
@Override
public BaseAdsResponse deleteVideoDMCard(String accountId, String cardId) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
TwitterAdUtil.ensureNotNull(cardId, TwitterAdsConstants.CARD_ID);
final String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_VIDEO_DM_CARDS + cardId;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, null, type, HttpVerb.DELETE);
}
@Override
public BaseAdsResponse createVideoWebsiteCard(String accountId, String name, String title, String videoMediaKey, String websiteUrl) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, "Account Id");
TwitterAdUtil.ensureNotNull(name, "Name");
TwitterAdUtil.ensureNotNull(title, "Title");
TwitterAdUtil.ensureNotNull(videoMediaKey, "Video Key");
TwitterAdUtil.ensureNotNull(websiteUrl, "Website url");
verifyLength(name, "Name", TwitterAdsConstants.MAX_VIDEO_WEBSITE_CARD_NAME_LENGTH);
verifyLength(title, "Title", TwitterAdsConstants.MAX_VIDEO_WEBSITE_CARD_TITLE_LENGTH);
final List params = validateAndCreateParamsForCreateVideoWebsiteCard(accountId, name, title, videoMediaKey, websiteUrl);
final String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_VIDEO_WEBSITE_CARDS;
final HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, parameters, type, HttpVerb.POST);
}
@Override
public BaseAdsResponse updateVideoWebsiteCard(String accountId, String cardId, String name, String title, String videoMediaKey, String websiteUrl) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
TwitterAdUtil.ensureNotNull(cardId, TwitterAdsConstants.CARD_ID);
final List params =
validateAndCreateParamsForUpdateVideoWebsiteCard(name, title, videoMediaKey, websiteUrl);
final String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_VIDEO_WEBSITE_CARDS + cardId;
final HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, parameters, type, HttpVerb.PUT);
}
@SuppressWarnings("Duplicates")
@Override
public BaseAdsResponse deleteVideoWebsiteCard(String accountId, String cardId) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
TwitterAdUtil.ensureNotNull(cardId, TwitterAdsConstants.CARD_ID);
final String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_VIDEO_WEBSITE_CARDS + cardId;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, null, type, HttpVerb.DELETE);
}
// --------------------------------------------------------------------- PRIVATE METHODS ---------------------------------------------
private String getMediaId(String image) {
Media media;
try {
TwitterAdUtil.ensureNotNull(image, "image");
InputStream fileBody = new URL(image).openStream();
media = twitterAdsClient.upload(new MediaUpload(fileBody));
} catch (Exception e) {
throw new RuntimeException(e);
}
if (media != null) {
return media.getMediaIdString();
}
return null;
}
private BaseAdsResponse getVideoConversationCard(String accountId, String cardId) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_VIDEO_CONVERSATION_CARDS + cardId;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, null, type, HttpVerb.GET);
}
private List validateAndCreateParamsForCreateWebsiteCard(String accountId, String name, String websiteTitle, String websiteUrl,
String mediaKey) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, "AccountId");
TwitterAdUtil.ensureNotNull(name, "Name");
TwitterAdUtil.ensureNotNull(websiteTitle, "WebsiteTitle");
TwitterAdUtil.ensureNotNull(websiteUrl, "WebsiteUrl");
final List params = Lists.newArrayList();
params.add(new HttpParameter(TwitterAdsConstants.PARAM_NAME, name));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_WEBSITE_TITLE, websiteTitle));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_WEBSITE_URL, websiteUrl));
if (isNotNullOrEmpty(mediaKey)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_MEDIA_KEY, mediaKey));
}
return params;
}
private void verifyHashtagLength(String hashtag, String label) throws TwitterException {
if (hashtag.length() > CONVERSATION_CARD_HASHTAG_LENGTH) {
throw new TwitterException(
new UnsupportedOperationException(label + " cannot be more than " + CONVERSATION_CARD_HASHTAG_LENGTH + " characters"));
}
}
private void verifyTweetLength(String tweet, String label) throws TwitterException {
if (tweet.length() > CONVERSATION_CARD_TWEET_LENGTH) {
throw new TwitterException(
new UnsupportedOperationException(label + " cannot be more than " + CONVERSATION_CARD_TWEET_LENGTH + " characters"));
}
}
private List validateAndCreateParamsForUpdateConversationCard(String accountId, String name, String title, String firstHashtag,
String firstTweet, String secondHashtag, String secondTweet,
String thirdHashtag, String thirdTweet, String fourthHashtag,
String fourthTweet,
String thanksText, String thanksUrl,
AbstractConversationCard existingCard) throws TwitterException {
final List params =
validateAndCreateParamsForCreateConversationCard(accountId, name, title, firstHashtag, firstTweet, secondHashtag, secondTweet,
thirdHashtag, thirdTweet, fourthHashtag, fourthTweet, thanksText, thanksUrl);
if (!isNotNullOrEmpty(title) && isNotNullOrEmpty(existingCard.getTitle())) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_TITLE, ""));
}
if (!isNotNullOrEmpty(secondHashtag) && isNotNullOrEmpty(existingCard.getSecondHashtag())) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_SECOND_CTA, ""));
}
if (!isNotNullOrEmpty(secondTweet) && isNotNullOrEmpty(existingCard.getSecondTweet())) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_SECOND_CTA_TWEET, ""));
}
if (!isNotNullOrEmpty(thirdHashtag) && isNotNullOrEmpty(existingCard.getThirdHashtag())) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_THIRD_CTA, ""));
}
if (!isNotNullOrEmpty(thirdTweet) && isNotNullOrEmpty(existingCard.getThirdTweet())) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_THIRD_CTA_TWEET, ""));
}
if (!isNotNullOrEmpty(fourthHashtag) && isNotNullOrEmpty(existingCard.getFourthHashtag())) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_FOURTH_CTA, ""));
}
if (!isNotNullOrEmpty(fourthTweet) && isNotNullOrEmpty(existingCard.getFourthTweet())) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_FOURTH_CTA_TWEET, ""));
}
if (!isNotNullOrEmpty(thanksUrl) && isNotNullOrEmpty(existingCard.getThankUrl())) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_THANK_YOU_URL, ""));
}
return params;
}
@SuppressWarnings("Duplicates")
private List validateAndCreateParamsForCreateAppDownloadCard(String accountId, String name, String countryCode,
String iphoneAppId, String ipadAppId, String googlePlayAppId,
String iphoneDeepLink, String ipadDeepLink, String googlePlayDeepLink,
String customAppDescription, String mediaId, String callToAction)
throws TwitterException {
List params =
getCardHttpParameters(accountId, name, countryCode, iphoneAppId, ipadAppId, googlePlayAppId, iphoneDeepLink, ipadDeepLink,
googlePlayDeepLink);
if (isNotNullOrEmpty(mediaId)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_CUSTOM_ICON_MEDIA_ID, mediaId));
}
if (isNotNullOrEmpty(callToAction)) {
params.add(new HttpParameter(TwitterAdsConstants.APP_CTA, callToAction));
}
if (isNotNullOrEmpty(customAppDescription)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_CUSTOM_APP_DESCRIPTION, customAppDescription));
}
return params;
}
private List validateAndCreateParamsForUpdateVideoConversationCard(String accountId, String name, String title,
String firstHashtag, String firstTweet, String secondHashtag,
String secondTweet, String thirdHashtag, String thirdTweet,
String fourthHashtag, String fourthTweet, String thanksText,
String thanksUrl, TwitterVideoConversationCard existingCard)
throws TwitterException {
return validateAndCreateParamsForUpdateConversationCard(accountId, name, title, firstHashtag, firstTweet, secondHashtag, secondTweet,
thirdHashtag, thirdTweet, fourthHashtag, fourthTweet, thanksText, thanksUrl,
existingCard);
}
private List getCardHttpParameters(String accountId, String name, String countryCode, String iphoneAppId, String ipadAppId,
String googlePlayAppId, String iphoneDeepLink, String ipadDeepLink, String googlePlayDeepLink)
throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, "AccountId");
TwitterAdUtil.ensureNotNull(countryCode, "App Country Code");
TwitterAdUtil.ensureNotNull(name, "Name");
final List params = Lists.newArrayList();
params.add(new HttpParameter(TwitterAdsConstants.PARAM_NAME, name));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_COUNTRY_CODE, countryCode));
// This is done in order to satisfy the condition: atleast one of the app ids is provided
if (!(TwitterAdUtil.isNotNullOrEmpty(googlePlayAppId) || TwitterAdUtil.isNotNullOrEmpty(ipadAppId) ||
TwitterAdUtil.isNotNullOrEmpty(iphoneAppId))) {
throw new TwitterException(
new UnsupportedOperationException("Please provide atleast one valid store app id to create an app download card"));
}
if (isNotNullOrEmpty(iphoneAppId)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_IPHONE_APP_ID, iphoneAppId));
if (isNotNullOrEmpty(iphoneDeepLink)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_IPHONE_DEEP_LINK, iphoneDeepLink));
}
}
if (isNotNullOrEmpty(ipadAppId)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_IPAD_APP_ID, ipadAppId));
if (isNotNullOrEmpty(ipadDeepLink)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_IPAD_DEEP_LINK, ipadDeepLink));
}
}
if (isNotNullOrEmpty(googlePlayAppId)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_GOOGLEPLAY_APP_ID, googlePlayAppId));
if (isNotNullOrEmpty(googlePlayDeepLink)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_GOOGLEPLAY_DEEP_LINK, googlePlayDeepLink));
}
}
return params;
}
private BaseAdsResponse getImageConversationCard(String accountId, String cardId) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, "Account Id");
String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_IMAGE_CONVERSATION_CARDS + cardId;
Type type = new TypeToken>() {
}.getType();
return twitterAdsClient.executeHttpRequest(url, null, type, HttpVerb.GET);
}
private List validateAndCreateParamsForUpdateAppDownloadCard(String accountId, String name, String countryCode,
String iphoneAppId, String ipadAppId, String googlePlayAppId,
String iphoneDeepLink, String ipadDeepLink, String googlePlayDeepLink,
String customAppDescription, String mediaId, String callToAction)
throws TwitterException {
List params =
getCardHttpParametersForUpdate(accountId, name, countryCode, iphoneAppId, ipadAppId, googlePlayAppId, iphoneDeepLink, ipadDeepLink,
googlePlayDeepLink);
if (isNotNullOrEmpty(mediaId)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_CUSTOM_ICON_MEDIA_ID, mediaId));
}
if (isNotNullOrEmpty(callToAction)) {
params.add(new HttpParameter(TwitterAdsConstants.APP_CTA, callToAction));
}
if (isNotNullOrEmpty(customAppDescription)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_CUSTOM_APP_DESCRIPTION, customAppDescription));
}
return params;
}
private List validateAndCreateParamsForCreateVideoConversationCard(String accountId, String name, String title, String firstHashtag,
String firstTweet, String secondHashtag, String secondTweet,
String thirdHashtag, String thirdTweet, String fourthHashtag,
String fourthTweet, String thanksText, String thanksUrl)
throws TwitterException {
return validateAndCreateParamsForCreateConversationCard(accountId, name, title, firstHashtag, firstTweet, secondHashtag, secondTweet,
thirdHashtag, thirdTweet, fourthHashtag, fourthTweet, thanksText, thanksUrl);
}
private List getCardHttpParametersForUpdate(String accountId, String name, String countryCode, String iphoneAppId,
String ipadAppId, String googlePlayAppId, String iphoneDeepLink, String ipadDeepLink,
String googlePlayDeepLink) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
TwitterAdUtil.ensureNotNull(countryCode, "App Country Code");
TwitterAdUtil.ensureNotNull(name, "Name");
final List params = Lists.newArrayList();
params.add(new HttpParameter(TwitterAdsConstants.PARAM_NAME, name));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_COUNTRY_CODE, countryCode));
// This is done in order to satisfy the condition: atleast one of the app ids is provided
if (!(TwitterAdUtil.isNotNullOrEmpty(googlePlayAppId) || TwitterAdUtil.isNotNullOrEmpty(ipadAppId) ||
TwitterAdUtil.isNotNullOrEmpty(iphoneAppId))) {
throw new TwitterException(
new UnsupportedOperationException("Please provide atleast one valid store app id to create an app download card"));
}
iphoneAppId = iphoneAppId == null ? "" : iphoneAppId;
iphoneDeepLink = iphoneDeepLink == null ? "" : iphoneDeepLink;
ipadAppId = ipadAppId == null ? "" : ipadAppId;
ipadDeepLink = ipadDeepLink == null ? "" : ipadDeepLink;
googlePlayAppId = googlePlayAppId == null ? "" : googlePlayAppId;
googlePlayDeepLink = googlePlayDeepLink == null ? "" : googlePlayDeepLink;
params.add(new HttpParameter(TwitterAdsConstants.PARAM_IPHONE_APP_ID, iphoneAppId));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_IPHONE_DEEP_LINK, iphoneDeepLink));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_IPAD_APP_ID, ipadAppId));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_IPAD_DEEP_LINK, ipadDeepLink));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_GOOGLEPLAY_APP_ID, googlePlayAppId));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_GOOGLEPLAY_DEEP_LINK, googlePlayDeepLink));
return params;
}
private List validateAndCreateParamsForCreateImageAppDownloadCard(String accountId, String name, String countryCode,
String iphoneAppId, String ipadAppId, String googlePlayAppId,
String iphoneDeepLink, String ipadDeepLink,
String googlePlayDeepLink, String mediaKey, String callToAction)
throws TwitterException {
List params =
getCardHttpParameters(accountId, name, countryCode, iphoneAppId, ipadAppId, googlePlayAppId, iphoneDeepLink, ipadDeepLink,
googlePlayDeepLink);
if (isNotNullOrEmpty(mediaKey)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_MEDIA_KEY, mediaKey));
}
if (isNotNullOrEmpty(callToAction)) {
params.add(new HttpParameter(TwitterAdsConstants.APP_CTA, callToAction));
}
return params;
}
private List validateAndCreateParamsForUpdateImageAppDownloadCard(String accountId, String name, String countryCode,
String iphoneAppId, String ipadAppId, String googlePlayAppId,
String iphoneDeepLink, String ipadDeepLink,
String googlePlayDeepLink, String mediaKey, String callToAction)
throws TwitterException {
List params =
getCardHttpParametersForUpdate(accountId, name, countryCode, iphoneAppId, ipadAppId, googlePlayAppId, iphoneDeepLink, ipadDeepLink,
googlePlayDeepLink);
if (isNotNullOrEmpty(mediaKey)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_MEDIA_KEY, mediaKey));
}
if (isNotNullOrEmpty(callToAction)) {
params.add(new HttpParameter(TwitterAdsConstants.APP_CTA, callToAction));
}
return params;
}
private List validateAndCreateParamsForCreateImageConversationCard(String accountId, String name, String title,
String firstHashtag, String firstTweet, String secondHashtag,
String secondTweet, String thirdHashtag, String thirdTweet,
String fourthHashtag, String fourthTweet, String thanksText,
String thanksUrl, String imageUrl) throws TwitterException {
TwitterAdUtil.ensureNotNull(imageUrl, "Image");
return validateAndCreateParamsForCreateConversationCard(accountId, name, title, firstHashtag, firstTweet, secondHashtag, secondTweet,
thirdHashtag, thirdTweet, fourthHashtag, fourthTweet, thanksText, thanksUrl);
}
private List validateAndCreateParamsForCreateConversationCard(String accountId, String name, String title, String firstHashtag,
String firstTweet, String secondHashtag, String secondTweet,
String thirdHashtag, String thirdTweet, String fourthHashtag,
String fourthTweet, String thanksText, String thanksUrl)
throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
TwitterAdUtil.ensureNotNull(name, "Name");
TwitterAdUtil.ensureNotNull(firstHashtag, "First Cta Hashtag");
TwitterAdUtil.ensureNotNull(firstTweet, "First Cta Tweet");
TwitterAdUtil.ensureNotNull(thanksText, "Thanks tweet text");
verifyHashtagLength(firstHashtag, "First hashtag");
verifyTweetLength(firstTweet, "First Tweet");
if (isNotNullOrEmpty(title) && isNotNullOrEmpty(secondHashtag)) {
throw new TwitterException(new UnsupportedOperationException("Card Title cannot be used with second hashtag"));
}
if (!isNotNullOrEmpty(title) && !isNotNullOrEmpty(secondHashtag)) {
throw new TwitterException(new UnsupportedOperationException("Atleast one of card title or second hashtag is compulsory"));
}
if (isNotNullOrEmpty(secondHashtag) && !isNotNullOrEmpty(secondTweet)) {
throw new TwitterException(new UnsupportedOperationException("Please provide tweet along with second hashtag"));
}
final List params = new ArrayList<>();
params.add(new HttpParameter(TwitterAdsConstants.PARAM_NAME, name));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_FIRST_CTA, firstHashtag));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_FIRST_CTA_TWEET, firstTweet));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_THANK_YOU_TEXT, thanksText));
if (TwitterAdUtil.isNotNull(title)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_TITLE, title));
}
if (TwitterAdUtil.isNotNull(secondHashtag)) {
verifyHashtagLength(secondHashtag, "Second hashtag");
params.add(new HttpParameter(TwitterAdsConstants.PARAM_SECOND_CTA, secondHashtag));
}
if (TwitterAdUtil.isNotNull(secondTweet)) {
verifyTweetLength(secondTweet, "Second Tweet");
params.add(new HttpParameter(TwitterAdsConstants.PARAM_SECOND_CTA_TWEET, secondTweet));
}
if (TwitterAdUtil.isNotNull(thirdHashtag)) {
verifyHashtagLength(thirdHashtag, "Third hashtag");
params.add(new HttpParameter(TwitterAdsConstants.PARAM_THIRD_CTA, thirdHashtag));
}
if (TwitterAdUtil.isNotNull(thirdTweet)) {
verifyTweetLength(thirdTweet, "Third Tweet");
params.add(new HttpParameter(TwitterAdsConstants.PARAM_THIRD_CTA_TWEET, thirdTweet));
}
if (TwitterAdUtil.isNotNull(fourthHashtag)) {
verifyHashtagLength(fourthHashtag, "Fourth hashtag");
params.add(new HttpParameter(TwitterAdsConstants.PARAM_FOURTH_CTA, fourthHashtag));
}
if (TwitterAdUtil.isNotNull(fourthTweet)) {
verifyTweetLength(fourthTweet, "Fourth Tweet");
params.add(new HttpParameter(TwitterAdsConstants.PARAM_FOURTH_CTA_TWEET, fourthTweet));
}
if (TwitterAdUtil.isNotNull(thanksUrl)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_THANK_YOU_URL, thanksUrl));
}
return params;
}
@SuppressWarnings("Duplicates")
private List validateAndCreateParamsForCreateVideoAppDownloadCard(String accountId, String name, String countryCode,
String iphoneAppId, String ipadAppId, String googlePlayAppId,
String iphoneDeepLink, String ipadDeepLink,
String googlePlayDeepLink, String posterMediaKey,
String videoMediaKey, String callToAction)
throws TwitterException {
List params =
getCardHttpParameters(accountId, name, countryCode, iphoneAppId, ipadAppId, googlePlayAppId, iphoneDeepLink, ipadDeepLink,
googlePlayDeepLink);
if (isNotNullOrEmpty(videoMediaKey)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_MEDIA_KEY, videoMediaKey));
}
if (isNotNullOrEmpty(posterMediaKey)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_POSTER_MEDIA_KEY, posterMediaKey));
}
if (isNotNullOrEmpty(callToAction)) {
params.add(new HttpParameter(TwitterAdsConstants.APP_CTA, callToAction));
}
return params;
}
private List validateAndCreateParamsForUpdateVideoAppDownloadCard(String accountId, String name, String countryCode,
String iphoneAppId, String ipadAppId, String googlePlayAppId,
String iphoneDeepLink, String ipadDeepLink,
String googlePlayDeepLink, String posterMediaKey,
String videoMediaKey, String callToAction)
throws TwitterException {
List params =
getVideoCardHttpParametersForUpdate(accountId, name, countryCode, iphoneAppId, ipadAppId, googlePlayAppId, iphoneDeepLink,
ipadDeepLink, googlePlayDeepLink);
if (isNotNullOrEmpty(posterMediaKey)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_POSTER_MEDIA_KEY, posterMediaKey));
}
if (isNotNullOrEmpty(videoMediaKey)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_MEDIA_KEY, videoMediaKey));
}
if (isNotNullOrEmpty(callToAction)) {
params.add(new HttpParameter(TwitterAdsConstants.APP_CTA, callToAction));
}
return params;
}
private List getVideoCardHttpParametersForUpdate(String accountId, String name, String countryCode, String iphoneAppId,
String ipadAppId, String googlePlayAppId, String iphoneDeepLink,
String ipadDeepLink, String googlePlayDeepLink) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
TwitterAdUtil.ensureNotNull(countryCode, "App Country Code");
TwitterAdUtil.ensureNotNull(name, "Name");
final List params = Lists.newArrayList();
params.add(new HttpParameter(TwitterAdsConstants.PARAM_NAME, name));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_COUNTRY_CODE, countryCode));
// This is done in order to satisfy the condition: atleast one of the app ids is provided
if (!(TwitterAdUtil.isNotNullOrEmpty(googlePlayAppId) || TwitterAdUtil.isNotNullOrEmpty(ipadAppId) ||
TwitterAdUtil.isNotNullOrEmpty(iphoneAppId))) {
throw new TwitterException(
new UnsupportedOperationException("Please provide atleast one valid store app id to create an app download card"));
}
if (TwitterAdUtil.isNotNull(iphoneAppId)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_IPHONE_APP_ID, iphoneAppId));
if (TwitterAdUtil.isNotNull(iphoneDeepLink)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_IPHONE_DEEP_LINK, iphoneDeepLink));
}
}
if (TwitterAdUtil.isNotNull(ipadAppId)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_IPAD_APP_ID, ipadAppId));
if (TwitterAdUtil.isNotNull(ipadDeepLink)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_IPAD_DEEP_LINK, ipadDeepLink));
}
}
if (TwitterAdUtil.isNotNull(googlePlayAppId)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_GOOGLEPLAY_APP_ID, googlePlayAppId));
if (TwitterAdUtil.isNotNull(googlePlayDeepLink)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_GOOGLEPLAY_DEEP_LINK, googlePlayDeepLink));
}
}
return params;
}
private List validateAndCreateParamsForUpdateImageConversationCard(String accountId, String name, String title,
String firstHashtag, String firstTweet, String secondHashtag,
String secondTweet, String thirdHashtag, String thirdTweet,
String fourthHashtag, String fourthTweet, String thanksText,
String thanksUrl, String imageUrl,
TwitterImageConversationCard existingCard)
throws TwitterException {
TwitterAdUtil.ensureNotNull(imageUrl, "Image");
return validateAndCreateParamsForUpdateConversationCard(accountId, name, title, firstHashtag, firstTweet, secondHashtag, secondTweet,
thirdHashtag, thirdTweet, fourthHashtag, fourthTweet, thanksText, thanksUrl,
existingCard);
}
private List validateAndCreateParamsForCreateVideoWebsiteCard(String accountId, String name, String title, String videoMediaKey,
String websiteUrl) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
TwitterAdUtil.ensureNotNull(name, "Name");
TwitterAdUtil.ensureNotNull(title, "Title");
TwitterAdUtil.ensureNotNull(videoMediaKey, "Video Id");
TwitterAdUtil.ensureNotNull(websiteUrl, "Website url");
verifyLength(name, "Name", TwitterAdsConstants.MAX_VIDEO_WEBSITE_CARD_NAME_LENGTH);
verifyLength(title, "Title", TwitterAdsConstants.MAX_VIDEO_WEBSITE_CARD_TITLE_LENGTH);
final List params = new ArrayList<>();
params.add(new HttpParameter(TwitterAdsConstants.PARAM_NAME, name));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_TITLE, title));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_MEDIA_KEY, videoMediaKey));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_WEBSITE_URL, websiteUrl));
return params;
}
private List validateAndCreateParamsForUpdateVideoWebsiteCard(String name, String title, String videoMediaKey, String websiteUrl)
throws TwitterException {
final List params = new ArrayList<>();
if (isNotNullOrEmpty(name)) {
verifyLength(name, "Name", TwitterAdsConstants.MAX_VIDEO_WEBSITE_CARD_NAME_LENGTH);
params.add(new HttpParameter(TwitterAdsConstants.PARAM_NAME, name));
}
if (isNotNullOrEmpty(title)) {
verifyLength(title, "Title", TwitterAdsConstants.MAX_VIDEO_WEBSITE_CARD_TITLE_LENGTH);
params.add(new HttpParameter(TwitterAdsConstants.PARAM_TITLE, title));
}
if (isNotNullOrEmpty(videoMediaKey)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_MEDIA_KEY, videoMediaKey));
}
if (isNotNullOrEmpty(websiteUrl)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_WEBSITE_URL, websiteUrl));
}
return params;
}
private void verifyLength(String field, String label, long length) throws TwitterException {
if (field.length() > length) {
throw new TwitterException(new UnsupportedOperationException(label + " cannot be more than " + length + " characters"));
}
}
private List validateAndCreateParamsForCreateImageDMCard(String accountId, String name, String firstCta,
Long firstWelcomeMessageId,
String secondCta, Long secondWelcomeMessageId, String thirdCta,
Long thirdWelcomeMessageId, String fourthCta, Long fourthWelcomeMessageId,
Long recipientAccountId, String imageUrl) throws TwitterException {
TwitterAdUtil.ensureNotNull(imageUrl, "Image Url");
return validateAndCreateParamsForCreateDMCard(accountId, name, firstCta, firstWelcomeMessageId, secondCta, secondWelcomeMessageId, thirdCta,
thirdWelcomeMessageId, fourthCta, fourthWelcomeMessageId, recipientAccountId);
}
private List validateAndCreateParamsForCreateVideoDMCard(String accountId, String name, String firstCta,
Long firstWelcomeMessageId,
String secondCta, Long secondWelcomeMessageId, String thirdCta,
Long thirdWelcomeMessageId, String fourthCta, Long fourthWelcomeMessageId,
Long recipientAccountId, String videoUrl) throws TwitterException {
TwitterAdUtil.ensureNotNull(videoUrl, "Video Url");
return validateAndCreateParamsForCreateDMCard(accountId, name, firstCta, firstWelcomeMessageId, secondCta, secondWelcomeMessageId, thirdCta,
thirdWelcomeMessageId, fourthCta, fourthWelcomeMessageId, recipientAccountId);
}
private List validateAndCreateParamsForUpdateDMCard(String accountId, String name, String firstCta,
Long firstWelcomeMessageId,
String secondCta, Long secondWelcomeMessageId, String thirdCta,
Long thirdWelcomeMessageId, String fourthCta, Long fourthWelcomeMessageId,
String channelId) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, "AccountId");
TwitterAdUtil.ensureNotNull(channelId, "Card Channel Id");
final List params = new ArrayList<>();
if (StringUtils.isNotBlank(name)) {
params.add(new HttpParameter(TwitterAdsConstants.PARAM_NAME, name));
}
if (TwitterAdUtil.isNotNull(firstCta) && TwitterAdUtil.isNotNull(firstWelcomeMessageId)) {
verifyCtaLength(firstCta, "First Cta");
params.add(new HttpParameter(TwitterAdsConstants.PARAM_FIRST_CTA, firstCta));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_FIRST_CTA_WELCOME_MESSAGE_ID, firstWelcomeMessageId));
}
if (TwitterAdUtil.isNotNull(secondCta) && TwitterAdUtil.isNotNull(secondWelcomeMessageId)) {
verifyCtaLength(secondCta, "Second Cta");
params.add(new HttpParameter(TwitterAdsConstants.PARAM_SECOND_CTA, secondCta));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_SECOND_CTA_WELCOME_MESSAGE_ID, secondWelcomeMessageId));
}
if (TwitterAdUtil.isNotNull(thirdCta) && TwitterAdUtil.isNotNull(thirdWelcomeMessageId)) {
verifyCtaLength(thirdCta, "Third Cta");
params.add(new HttpParameter(TwitterAdsConstants.PARAM_THIRD_CTA, thirdCta));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_THIRD_CTA_WELCOME_MESSAGE_ID, thirdWelcomeMessageId));
}
if (TwitterAdUtil.isNotNull(fourthCta) && TwitterAdUtil.isNotNull(fourthWelcomeMessageId)) {
verifyCtaLength(fourthCta, "Fourth Cta");
params.add(new HttpParameter(TwitterAdsConstants.PARAM_FOURTH_CTA, fourthCta));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_FOURTH_CTA_WELCOME_MESSAGE_ID, fourthWelcomeMessageId));
}
return params;
}
private List validateAndCreateParamsForCreateDMCard(String accountId, String name, String firstCta, Long firstWelcomeMessageId,
String secondCta, Long secondWelcomeMessageId, String thirdCta,
Long thirdWelcomeMessageId, String fourthCta, Long fourthWelcomeMessageId,
Long recipientAccountId) throws TwitterException {
TwitterAdUtil.ensureNotNull(accountId, "AccountId");
TwitterAdUtil.ensureNotNull(name, "Card Name");
TwitterAdUtil.ensureNotNull(firstCta, "First Cta");
TwitterAdUtil.ensureNotNull(firstWelcomeMessageId, "First Welcome Message ID");
TwitterAdUtil.ensureNotNull(recipientAccountId, "Promotable Account ID");
verifyCtaLength(firstCta, "First Cta");
final List params = new ArrayList<>();
params.add(new HttpParameter(TwitterAdsConstants.PARAM_NAME, name));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_RECIPIENT_USER_ID, recipientAccountId));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_FIRST_CTA, firstCta));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_FIRST_CTA_WELCOME_MESSAGE_ID, firstWelcomeMessageId));
if (TwitterAdUtil.isNotNull(secondCta) && TwitterAdUtil.isNotNull(secondWelcomeMessageId)) {
verifyCtaLength(secondCta, "Second Cta");
params.add(new HttpParameter(TwitterAdsConstants.PARAM_SECOND_CTA, secondCta));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_SECOND_CTA_WELCOME_MESSAGE_ID, secondWelcomeMessageId));
}
if (TwitterAdUtil.isNotNull(thirdCta) && TwitterAdUtil.isNotNull(thirdWelcomeMessageId)) {
verifyCtaLength(thirdCta, "Third Cta");
params.add(new HttpParameter(TwitterAdsConstants.PARAM_THIRD_CTA, thirdCta));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_THIRD_CTA_WELCOME_MESSAGE_ID, thirdWelcomeMessageId));
}
if (TwitterAdUtil.isNotNull(fourthCta) && TwitterAdUtil.isNotNull(fourthWelcomeMessageId)) {
verifyCtaLength(fourthCta, "Fourth Cta");
params.add(new HttpParameter(TwitterAdsConstants.PARAM_FOURTH_CTA, fourthCta));
params.add(new HttpParameter(TwitterAdsConstants.PARAM_FOURTH_CTA_WELCOME_MESSAGE_ID, fourthWelcomeMessageId));
}
return params;
}
private void verifyCtaLength(String cta, String label) throws TwitterException {
if (cta.length() > DM_CARD_CTA_LENGTH) {
throw new TwitterException(
new UnsupportedOperationException(label + " cannot be more than " + DM_CARD_CTA_LENGTH + " characters"));
}
}
}