Skip to content

Commit

Permalink
entity status in POST and PUT calls for line item and campaigns
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishek anand committed Nov 30, 2017
1 parent 5fffeca commit d0d5b85
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 54 deletions.
26 changes: 15 additions & 11 deletions twitter4j-ads/src/twitter4j/TwitterAdsConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface TwitterAdsConstants {
String V1_PREFIX_STATS_JOB_ACCOUNTS_URI = "2/stats/jobs/accounts/";

Long MAX_IMAGE_SIZE_FOR_WEBSITE_IN_BYTES = 1040000L;
long MAX_VIDEO_WEBSITE_CARD_NAME_LENGTH = 80L;
long MAX_VIDEO_WEBSITE_CARD_TITLE_LENGTH = 70L;

String PREFIX_ACCOUNTS_URI_2 = "2/accounts/";
String TWEET_PREVIEW_PATH = "/tweet/preview/";
Expand Down Expand Up @@ -66,6 +68,7 @@ public interface TwitterAdsConstants {
String PARAM_FREQUENCY_CAP = "frequency_cap";
String PARAM_DURATION_IN_DAYS = "duration_in_days";
String PARAM_PAUSED = "paused";
String PARAM_ENTITY_STATUS = "entity_status";
String PARAM_END_TIME = "end_time";
String PARAM_START_TIME = "start_time";
String PARAM_FUNDING_INSTRUMENT_ID = "funding_instrument_id";
Expand Down Expand Up @@ -152,7 +155,7 @@ public interface TwitterAdsConstants {
/**
* For Twitter Audience Platform
*/
String PATH_IAB_CATEGORIES = "1/iab_categories/";
String PATH_IAB_CATEGORIES = "2/iab_categories/";
String PATH_APP_LIST = "/app_lists/";
String PARAM_ADVERTISER_DOMAIN = "advertiser_domain";
String PARAM_CATEGORIES = "categories";
Expand Down Expand Up @@ -259,7 +262,7 @@ public interface TwitterAdsConstants {

String PATH_CAMPAIGN = "/campaigns/";
String PATH_FUNDING_INSTRUMENTS = "/funding_instruments/";
String PATH_BIDDING_RULES = "1/bidding_rules";
String PATH_BIDDING_RULES = "2/bidding_rules";
String PATH_IMAGE_DM_CARDS = "/cards/image_direct_message/";
String PATH_VIDEO_DM_CARDS = "/cards/video_direct_message";
String PATH_PROMOTED_TWEETS = "/promoted_tweets/";
Expand All @@ -270,15 +273,15 @@ public interface TwitterAdsConstants {
String PATH_SCHEDULED_PROMOTED_TWEETS = "/scheduled_promoted_tweets/";
String PATH_LINE_ITEMS = "/line_items/";
String PATH_TARGETING_CRITERIA = "/targeting_criteria/";
String PATH_TARGETING_CRITERIA_LOCATION = "1/targeting_criteria/locations";
String PATH_TARGETING_CRITERIA_INTERESTS = "1/targeting_criteria/interests";
String PATH_TARGETING_CRITERIA_PLATFORMS = "1/targeting_criteria/platforms";
String PATH_TARGETING_CRITERIA_PLATFORM_VERSIONS = "1/targeting_criteria/platform_versions";
String PATH_TARGETING_CRITERIA_DEVICES = "1/targeting_criteria/devices";
String PATH_TARGETING_CRITERIA_APP_STORE_CATEGORIES = "1/targeting_criteria/app_store_categories";
String PATH_TARGETING_CRITERIA_NETWORK_OPERATORS = "1/targeting_criteria/network_operators/";
String PATH_TARGETING_CRITERIA_EVENT = "1/targeting_criteria/events";
String PATH_TARGETING_LANGUAGES = "1/targeting_criteria/languages";
String PATH_TARGETING_CRITERIA_LOCATION = "2/targeting_criteria/locations";
String PATH_TARGETING_CRITERIA_INTERESTS = "2/targeting_criteria/interests";
String PATH_TARGETING_CRITERIA_PLATFORMS = "2/targeting_criteria/platforms";
String PATH_TARGETING_CRITERIA_PLATFORM_VERSIONS = "2/targeting_criteria/platform_versions";
String PATH_TARGETING_CRITERIA_DEVICES = "2/targeting_criteria/devices";
String PATH_TARGETING_CRITERIA_APP_STORE_CATEGORIES = "2/targeting_criteria/app_store_categories";
String PATH_TARGETING_CRITERIA_NETWORK_OPERATORS = "2/targeting_criteria/network_operators/";
String PATH_TARGETING_CRITERIA_EVENT = "2/targeting_criteria/events";
String PATH_TARGETING_LANGUAGES = "2/targeting_criteria/languages";
String PATH_TAILORED_AUDIENCES = "/tailored_audiences";
String PATH_TARGETING_SUGGESTIONS = "/targeting_suggestions/";
String PATH_TV_SHOWS = "2/targeting_criteria/tv_shows/";
Expand Down Expand Up @@ -348,6 +351,7 @@ public interface TwitterAdsConstants {
String PARAM_RETARGETING_ENABLED = "retargeting_enabled";

String PATH_WEB_EVENT_TAGS = "/web_event_tags/";
String PATH_VIDEO_WEBSITE_CARDS = "/cards/video_website/";

String PATH_ORGANIC_TWEETS_STATS ="/organic_tweets";

Expand Down
5 changes: 3 additions & 2 deletions twitter4j-ads/src/twitter4j/api/TwitterAdsCampaignApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import twitter4j.BaseAdsResponse;
import twitter4j.internal.models4j.TwitterException;
import twitter4j.models.ads.Campaign;
import twitter4j.models.ads.EntityStatus;
import twitter4j.models.ads.sort.CampaignSortByField;

import java.util.Collection;
Expand Down Expand Up @@ -59,7 +60,7 @@ BaseAdsListResponseIterable<Campaign> getAllCampaigns(String accountId, Optional
* @param dailyBudgetAmountLocalMicro (optional) Name to update the cmapaign with.
* @param startTime (optional) Start time to update the cmapaign with.
* @param endTime (optional) End time to update the cmapaign with.
* @param paused (optional) Update the paused state of campaign.
* @param status Status of the campaign
* @param standardDelivery (optional) Update the standard delivery setting of campaign.
* @param frequencyCap (if value passed greater than 0) Update integer representing the number of times for which one user could be delivered an ad to.
* @param durationInDays (if value passed greater than 0) Update integer representing the time period within which the frequency_cap frequency is achieved. Only supports values of: 1, 7 and 30.
Expand All @@ -69,7 +70,7 @@ BaseAdsListResponseIterable<Campaign> getAllCampaigns(String accountId, Optional
*/
BaseAdsResponse<Campaign> updateCampaign(String accountId, String campaignId, Optional<String> name,
Long totalBudgetAmountLocalMicro, Optional<Long> dailyBudgetAmountLocalMicro, Optional<String> startTime,
Optional<String> endTime, Optional<Boolean> paused,
Optional<String> endTime, EntityStatus status,
Optional<Boolean> standardDelivery, int frequencyCap, int durationInDays) throws TwitterException;

/**
Expand Down
9 changes: 3 additions & 6 deletions twitter4j-ads/src/twitter4j/api/TwitterAdsLineItemApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import twitter4j.BaseAdsListResponseIterable;
import twitter4j.BaseAdsResponse;
import twitter4j.internal.models4j.TwitterException;
import twitter4j.models.ads.BidType;
import twitter4j.models.ads.LineItem;
import twitter4j.models.ads.PromotedAccount;
import twitter4j.models.ads.Sentiments;
import twitter4j.models.ads.*;
import twitter4j.models.ads.sort.LineItemsSortByField;
import twitter4j.models.ads.sort.PromotedAccountsSortByField;
import twitter4j.models.video.AssociateMediaCreativeResponse;
Expand Down Expand Up @@ -65,7 +62,7 @@ BaseAdsListResponseIterable<LineItem> getAllLineItems(String accountId, Optional
* @param bidType The BidType to use on this line item.
* @param automaticallySelectBid Whether to use auto bidding on this line item.
* @param bidAmountLocalMicro (optional) Specify a new bid to set on this line item.
* @param paused (optional) Update the paused state of the line item.
* @param status Status to set
* @param includeSentiment (optional) Update the include sentiment parameter of line item.
* @param chargeBy (optional) Update the charge by parameter of line item.
* @param bidUnit (optional) Update the bid unit parameter of line item.
Expand All @@ -76,7 +73,7 @@ BaseAdsListResponseIterable<LineItem> getAllLineItems(String accountId, Optional
* @see <a href="https://dev.twitter.com/ads/reference/put/accounts/%3Aaccount_id/line_items/%3Aline_item_id">https://dev.twitter.com/ads/reference/put/accounts/%3Aaccount_id/line_items/%3Aline_item_id</a>
*/
BaseAdsResponse<LineItem> updateLineItem(String accountId, String lineItemId, BidType bidType, boolean automaticallySelectBid,
Optional<Long> bidAmountLocalMicro, Optional<Boolean> paused, Optional<Sentiments> includeSentiment,
Optional<Long> bidAmountLocalMicro, EntityStatus status, Optional<Sentiments> includeSentiment,
Optional<Boolean> matchRelevantPopularQueries, Optional<String> chargeBy,
Optional<String> bidUnit, Optional<String> advertiserDomain,
String[] iabCategories) throws TwitterException;
Expand Down
34 changes: 20 additions & 14 deletions twitter4j-ads/src/twitter4j/impl/TwitterAdsCampaignApiImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import twitter4j.internal.http.HttpParameter;
import twitter4j.internal.models4j.TwitterException;
import twitter4j.models.ads.Campaign;
import twitter4j.models.ads.EntityStatus;
import twitter4j.models.ads.HttpVerb;
import twitter4j.models.ads.sort.CampaignSortByField;
import twitter4j.util.TwitterAdUtil;
Expand Down Expand Up @@ -62,7 +63,8 @@ public BaseAdsListResponseIterable<Campaign> getAllCampaigns(String accountId, O
}
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_CAMPAIGN;

Type type = new TypeToken<BaseAdsListResponse<Campaign>>() {}.getType();
Type type = new TypeToken<BaseAdsListResponse<Campaign>>() {
}.getType();
return twitterAdsClient.executeHttpListRequest(baseUrl, params, type);
}

Expand All @@ -72,7 +74,8 @@ public BaseAdsResponse<Campaign> getCampaignById(String accountId, String campai
TwitterAdUtil.ensureNotNull(campaignId, "campaignId");
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_CAMPAIGN + campaignId;
HttpParameter[] params = new HttpParameter[]{new HttpParameter(PARAM_WITH_DELETED, withDeleted)};
Type type = new TypeToken<BaseAdsResponse<Campaign>>() {}.getType();
Type type = new TypeToken<BaseAdsResponse<Campaign>>() {
}.getType();
return twitterAdsClient.executeHttpRequest(baseUrl, params, type, HttpVerb.GET);
}

Expand All @@ -86,22 +89,24 @@ public BaseAdsResponse<Campaign> createCampaign(Campaign campaign) throws Twitte
parameters = params.toArray(new HttpParameter[params.size()]);
}
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_CAMPAIGN;
Type type = new TypeToken<BaseAdsResponse<Campaign>>() {}.getType();
Type type = new TypeToken<BaseAdsResponse<Campaign>>() {
}.getType();
return twitterAdsClient.executeHttpRequest(baseUrl, parameters, type, HttpVerb.POST);
}


@Override
public BaseAdsResponse<Campaign> updateCampaign(String accountId, String campaignId, Optional<String> name,
Long totalBudgetAmountLocalMicro, Optional<Long> dailyBudgetAmountLocalMicro, Optional<String> startTime,
Optional<String> endTime, Optional<Boolean> paused,
Optional<String> endTime, EntityStatus status,
Optional<Boolean> standardDelivery, int frequencyCap, int durationInDays) throws TwitterException {

List<HttpParameter> params =
validateUpdateCampaignParameters(accountId, campaignId, name, totalBudgetAmountLocalMicro, dailyBudgetAmountLocalMicro, startTime,
endTime, paused, standardDelivery, frequencyCap, durationInDays);
endTime, status, standardDelivery, frequencyCap, durationInDays);
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_CAMPAIGN + campaignId;
Type type = new TypeToken<BaseAdsResponse<Campaign>>() {}.getType();
Type type = new TypeToken<BaseAdsResponse<Campaign>>() {
}.getType();
return twitterAdsClient.executeHttpRequest(baseUrl, params.toArray(new HttpParameter[params.size()]), type, HttpVerb.PUT);

}
Expand All @@ -117,7 +122,8 @@ public BaseAdsResponse<Campaign> updateCampaignStatus(String accountId, String c
params.add(new HttpParameter(PARAM_PAUSED, paused));

String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_CAMPAIGN + campaignId;
Type type = new TypeToken<BaseAdsResponse<Campaign>>() {}.getType();
Type type = new TypeToken<BaseAdsResponse<Campaign>>() {
}.getType();
return twitterAdsClient.executeHttpRequest(baseUrl, params.toArray(new HttpParameter[params.size()]), type, HttpVerb.PUT);

}
Expand All @@ -127,7 +133,8 @@ public BaseAdsResponse<Campaign> deleteCampaign(String accountId, String campaig
TwitterAdUtil.ensureNotNull(accountId, "Account Id");
TwitterAdUtil.ensureNotNull(campaignId, "Campaign Id");
String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_CAMPAIGN + campaignId;
Type type = new TypeToken<BaseAdsResponse<Campaign>>() {}.getType();
Type type = new TypeToken<BaseAdsResponse<Campaign>>() {
}.getType();
return twitterAdsClient.executeHttpRequest(baseUrl, null, type, HttpVerb.DELETE);
}

Expand Down Expand Up @@ -159,9 +166,8 @@ private List<HttpParameter> validateCreateCampaignParameters(Campaign campaign)
String endTime = String.valueOf(df.format(campaign.getEndTime()));
params.add(new HttpParameter(PARAM_END_TIME, endTime));
}
if (campaign.getPaused() != null) {
Boolean paused = campaign.getPaused();
params.add(new HttpParameter(PARAM_PAUSED, paused));
if (campaign.getEntityStatus() != null) {
params.add(new HttpParameter(PARAM_ENTITY_STATUS, campaign.getEntityStatus()));
}
if (campaign.getStandardDelivery() != null) {
Boolean standardDelivery = campaign.getStandardDelivery();
Expand Down Expand Up @@ -199,7 +205,7 @@ private List<HttpParameter> getCampaignParameters(String accountId, Optional<Str

private List<HttpParameter> validateUpdateCampaignParameters(String accountId, String campaignId, Optional<String> name, Long totalBudgetAmountLocalMicro,
Optional<Long> dailyBudgetAmountLocalMicro, Optional<String> startTime,
Optional<String> endTime, Optional<Boolean> paused,
Optional<String> endTime, EntityStatus status,
Optional<Boolean> standardDelivery, int frequencyCap, int durationInDays) {
TwitterAdUtil.ensureNotNull(accountId, "AccountId");
TwitterAdUtil.ensureNotNull(campaignId, "Campaign Id");
Expand Down Expand Up @@ -227,8 +233,8 @@ private List<HttpParameter> validateUpdateCampaignParameters(String accountId, S
if (endTime != null && endTime.isPresent()) {
params.add(new HttpParameter(PARAM_END_TIME, endTime.get()));
}
if (paused != null && paused.isPresent()) {
params.add(new HttpParameter(PARAM_PAUSED, paused.get()));
if (status != null) {
params.add(new HttpParameter(PARAM_ENTITY_STATUS, status.name()));
}
if (standardDelivery != null && standardDelivery.isPresent()) {
params.add(new HttpParameter(PARAM_STANDARD_DELIVERY, standardDelivery.get()));
Expand Down
68 changes: 64 additions & 4 deletions twitter4j-ads/src/twitter4j/impl/TwitterAdsCardsApiImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
import java.util.concurrent.TimeUnit;

import static twitter4j.TwitterAdsConstants.*;
import static twitter4j.TwitterAdsConstants.PARAM_POSTER_IMAGE_ID;
import static twitter4j.TwitterAdsConstants.PATH_VIDEO_WEBSITE_CARDS;
import static twitter4j.util.TwitterAdUtil.isNotNullOrEmpty;

/**
* User: abhay
Expand Down Expand Up @@ -817,17 +818,70 @@ public BaseAdsResponse<TwitterVideoDMCard> deleteVideoDMCard(String accountId, S

@Override
public BaseAdsResponse<TwitterVideoWebsiteCard> createVideoWebsiteCard(String accountId, String name, String title, String videoId, String websiteUrl) throws TwitterException {
return null;
TwitterAdUtil.ensureNotNull(accountId, "Account Id");
TwitterAdUtil.ensureNotNull(name, "Name");
TwitterAdUtil.ensureNotNull(title, "Title");
TwitterAdUtil.ensureNotNull(videoId, "Video Id");
TwitterAdUtil.ensureNotNull(websiteUrl, "Website url");

verifyLength(name, "Name", MAX_VIDEO_WEBSITE_CARD_NAME_LENGTH);
verifyLength(title, "Title", MAX_VIDEO_WEBSITE_CARD_TITLE_LENGTH);

final List<HttpParameter> params = new ArrayList<>();
params.add(new HttpParameter(PARAM_NAME, name));
params.add(new HttpParameter(PARAM_TITLE, title));
params.add(new HttpParameter(PARAM_VIDEO_ID, videoId));
params.add(new HttpParameter(PARAM_WEBSITE_URL, websiteUrl));
final String url = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_VIDEO_WEBSITE_CARDS;
final HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
Type type = new TypeToken<BaseAdsResponse<TwitterVideoWebsiteCard>>() {
}.getType();

return twitterAdsClient.executeHttpRequest(url, parameters, type, HttpVerb.POST);
}

@Override
public BaseAdsResponse<TwitterVideoWebsiteCard> updateVideoWebsiteCard(String accountId, String cardId, String name, String title, String videoId, String websiteUrl) throws TwitterException {
return null;

TwitterAdUtil.ensureNotNull(accountId, "Account Id");
TwitterAdUtil.ensureNotNull(cardId, "Card Id");
final List<HttpParameter> params = new ArrayList<>();
if (isNotNullOrEmpty(name)) {
verifyLength(name, "Name", MAX_VIDEO_WEBSITE_CARD_NAME_LENGTH);
params.add(new HttpParameter(PARAM_NAME, name));
}

if (isNotNullOrEmpty(title)) {
verifyLength(title, "Title", MAX_VIDEO_WEBSITE_CARD_TITLE_LENGTH);
params.add(new HttpParameter(PARAM_TITLE, title));
}

if (isNotNullOrEmpty(videoId)) {
params.add(new HttpParameter(PARAM_VIDEO_ID, videoId));
}

if (isNotNullOrEmpty(websiteUrl)) {
params.add(new HttpParameter(PARAM_WEBSITE_URL, websiteUrl));
}

final String url = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_VIDEO_WEBSITE_CARDS + cardId;
final HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
Type type = new TypeToken<BaseAdsResponse<TwitterVideoWebsiteCard>>() {
}.getType();

return twitterAdsClient.executeHttpRequest(url, parameters, type, HttpVerb.PUT);
}

@Override
public BaseAdsResponse<TwitterVideoWebsiteCard> deleteVideoWebsiteCard(String accountId, String cardId) throws TwitterException {
return null;
TwitterAdUtil.ensureNotNull(accountId, "Account Id");
TwitterAdUtil.ensureNotNull(cardId, "Card Id");

final String url = twitterAdsClient.getBaseAdsAPIUrl() + PREFIX_ACCOUNTS_URI_2 + accountId + PATH_VIDEO_WEBSITE_CARDS + cardId;
Type type = new TypeToken<BaseAdsResponse<TwitterVideoWebsiteCard>>() {
}.getType();

return twitterAdsClient.executeHttpRequest(url, null, type, HttpVerb.DELETE);
}

@Override
Expand Down Expand Up @@ -1350,4 +1404,10 @@ private void verifyCtaLength(String cta, String label) throws TwitterException {
new UnsupportedOperationException(label + " cannot be more than " + DM_CARD_CTA_LENGTH + " characters"));
}
}

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"));
}
}
}

0 comments on commit d0d5b85

Please sign in to comment.