All Downloads are FREE. Search and download functionalities are using the official Maven repository.

twitter4jads.impl.TwitterAdsCampaignApiImpl Maven / Gradle / Ivy

The newest version!
package twitter4jads.impl;

import com.google.gson.reflect.TypeToken;
import twitter4jads.*;
import twitter4jads.api.TwitterAdsCampaignApi;
import twitter4jads.BaseAdsListResponse;
import twitter4jads.BaseAdsListResponseIterable;
import twitter4jads.BaseAdsResponse;
import twitter4jads.TwitterAdsClient;
import twitter4jads.internal.http.HttpParameter;
import twitter4jads.internal.models4j.TwitterException;
import twitter4jads.models.ads.Campaign;
import twitter4jads.models.ads.EntityStatus;
import twitter4jads.models.ads.HttpVerb;
import twitter4jads.models.ads.sort.CampaignSortByField;
import twitter4jads.util.TwitterAdUtil;

import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 *
 * Date: 4/7/16
 * Time: 12:26 PM
 */
public class TwitterAdsCampaignApiImpl implements TwitterAdsCampaignApi {

    private static final Integer MAX_REQUEST_PARAMETER_SIZE = 200;
    private final TwitterAdsClient twitterAdsClient;

    public TwitterAdsCampaignApiImpl(TwitterAdsClient twitterAdsClient) {
        this.twitterAdsClient = twitterAdsClient;
    }

    @Override
    public BaseAdsListResponseIterable getAllCampaigns(String accountId, Optional> campaignIds,
                                                                 Optional> fundingInstrumentIds, boolean withDeleted, Optional count,
                                                                 Optional cursor, Optional sortByField, Optional q) throws TwitterException {
        TwitterAdUtil.ensureNotNull(accountId, "accountId");
        String campaignIdsAsString = null;
        String fundingInstrumentIdsAsString = null;
        if (campaignIds != null && campaignIds.isPresent()) {
            TwitterAdUtil.ensureMaxSize(campaignIds.get(), MAX_REQUEST_PARAMETER_SIZE);
            campaignIdsAsString = TwitterAdUtil.getCsv(campaignIds.get());
        }
        if (fundingInstrumentIds != null && fundingInstrumentIds.isPresent()) {
            TwitterAdUtil.ensureMaxSize(fundingInstrumentIds.get(), MAX_REQUEST_PARAMETER_SIZE);
            fundingInstrumentIdsAsString = TwitterAdUtil.getCsv(fundingInstrumentIds.get());
        }

        List params =
                getCampaignParameters(accountId, Optional.ofNullable(campaignIdsAsString), Optional.ofNullable(fundingInstrumentIdsAsString), withDeleted, count, cursor, q);

        if (sortByField != null && sortByField.isPresent()) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_SORT_BY, sortByField.get().getField()));
        }
        final String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_CAMPAIGN;

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

    @Override
    public BaseAdsResponse getCampaignById(String accountId, String campaignId, boolean withDeleted) throws TwitterException {
        TwitterAdUtil.ensureNotNull(accountId, "accountId");
        TwitterAdUtil.ensureNotNull(campaignId, "campaignId");

        final String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_CAMPAIGN + campaignId;
        final HttpParameter[] params = new HttpParameter[]{new HttpParameter(TwitterAdsConstants.PARAM_WITH_DELETED, withDeleted)};

        final Type type = new TypeToken>() {
        }.getType();
        return twitterAdsClient.executeHttpRequest(baseUrl, params, type, HttpVerb.GET);
    }

    @Override
    public BaseAdsResponse createCampaign(String accountId, Campaign campaign) throws TwitterException {
        TwitterAdUtil.ensureNotNull(accountId, "Account Id");

        final List params = validateCreateCampaignParameters(campaign);
        HttpParameter[] parameters = null;
        if (!params.isEmpty()) {
            parameters = params.toArray(new HttpParameter[0]);
        }

        final String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_CAMPAIGN;
        Type type = new TypeToken>() {
        }.getType();
        return twitterAdsClient.executeHttpRequest(baseUrl, parameters, type, HttpVerb.POST);
    }

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

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

    }

    @Override
    public BaseAdsResponse deleteCampaign(String accountId, String campaignId) throws TwitterException {
        TwitterAdUtil.ensureNotNull(accountId, "Account Id");
        TwitterAdUtil.ensureNotNull(campaignId, "Campaign Id");

        final String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_CAMPAIGN + campaignId;
        final Type type = new TypeToken>() {
        }.getType();
        return twitterAdsClient.executeHttpRequest(baseUrl, null, type, HttpVerb.DELETE);
    }

    // ------------------------------------------------------------------------ PRIVATE METHODS -----------------------------------------------------

    private List validateCreateCampaignParameters(Campaign campaign) {
        TwitterAdUtil.ensureNotNull(campaign.getName(), "Name");
        final String name = campaign.getName();

        TwitterAdUtil.ensureNotNull(campaign.getFundingInstrumentId(), "Funding Instrument ID");
        final String fundingInstrumentId = campaign.getFundingInstrumentId();

        TwitterAdUtil.ensureNotNull(campaign.getStartTime(), "Start Time");
        final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        final String startTime = df.format(campaign.getStartTime());

        TwitterAdUtil.ensureNotNull(campaign.getDailyBudgetInMicro(), "Daily Budget Amount");
        final Long dailyBudgetAmountLocalMicro = campaign.getDailyBudgetInMicro();

        final List params = new ArrayList<>();
        final Long totalBudgetAmountLocalMicro = campaign.getTotalBudgetInMicro();
        if (totalBudgetAmountLocalMicro != null) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_TOTAL_BUDGET_AMOUNT_LOCAL_MICRO, totalBudgetAmountLocalMicro));
        }

        params.add(new HttpParameter(TwitterAdsConstants.PARAM_NAME, name));
        params.add(new HttpParameter(TwitterAdsConstants.PARAM_FUNDING_INSTRUMENT_ID, fundingInstrumentId));
        params.add(new HttpParameter(TwitterAdsConstants.PARAM_START_TIME, startTime));
        params.add(new HttpParameter(TwitterAdsConstants.PARAM_DAILY_BUDGET_AMOUNT_LOCAL_MICRO, dailyBudgetAmountLocalMicro));

        if (campaign.getEndTime() != null) {
            String endTime = df.format(campaign.getEndTime());
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_END_TIME, endTime));
        }
        if (campaign.getEntityStatus() != null) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_ENTITY_STATUS, campaign.getEntityStatus()));
        }
        if (campaign.getStandardDelivery() != null) {
            Boolean standardDelivery = campaign.getStandardDelivery();
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_STANDARD_DELIVERY, standardDelivery));
        }
        if (campaign.getFrequencyCap() != null) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_FREQUENCY_CAP, campaign.getFrequencyCap()));
        }
        if (campaign.getDurationInDays() != null) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_DURATION_IN_DAYS, campaign.getDurationInDays()));
        }
        return params;
    }

    private List getCampaignParameters(String accountId, Optional campaignIds, Optional fundingInstrumentIds, boolean withDeleted,
                                                      Optional count, Optional cursor, Optional q) {
        TwitterAdUtil.ensureNotNull(accountId, "accountId");
        final List params = new ArrayList<>();
        params.add(new HttpParameter(TwitterAdsConstants.PARAM_WITH_DELETED, withDeleted));
        if (campaignIds != null && campaignIds.isPresent()) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_CAMPAIGN_IDS, campaignIds.get()));
        }
        if (fundingInstrumentIds != null && fundingInstrumentIds.isPresent()) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_FUNDING_INSTRUMENT_IDS, fundingInstrumentIds.get()));
        }
        if (count != null && count.isPresent()) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_COUNT, count.get()));
        }
        if (cursor != null && cursor.isPresent()) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_CURSOR, cursor.get()));
        }

        if (q != null && q.isPresent()) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_Q, q.get()));
        }

        return params;
    }

    private List validateUpdateCampaignParameters(String accountId, String campaignId, Optional name, Long totalBudgetAmountLocalMicro,
                                                                 Optional dailyBudgetAmountLocalMicro, Optional startTime,
                                                                 Optional endTime, EntityStatus status,
                                                                 Optional standardDelivery, int frequencyCap, int durationInDays) {
        TwitterAdUtil.ensureNotNull(accountId, "AccountId");
        TwitterAdUtil.ensureNotNull(campaignId, "Campaign Id");
        final List params = new ArrayList<>();
        //The Ones that can be changed to null

        //The Ones that can be changed to null
        if (totalBudgetAmountLocalMicro == null || totalBudgetAmountLocalMicro >= 0) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_TOTAL_BUDGET_AMOUNT_LOCAL_MICRO, String.valueOf(totalBudgetAmountLocalMicro)));
        }

        if (endTime != null && endTime.isPresent()) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_END_TIME, String.valueOf(endTime)));
        }
        //The Ones that cannot be changed to null below
        if (name != null && name.isPresent()) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_NAME, name.get()));
        }
        if (dailyBudgetAmountLocalMicro != null && dailyBudgetAmountLocalMicro.isPresent()) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_DAILY_BUDGET_AMOUNT_LOCAL_MICRO, dailyBudgetAmountLocalMicro.get()));
        }
        if (startTime != null && startTime.isPresent()) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_START_TIME, startTime.get()));
        }
        if (endTime != null && endTime.isPresent()) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_END_TIME, endTime.get()));
        }
        if (status != null) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_ENTITY_STATUS, status.name()));
        }
        if (standardDelivery != null && standardDelivery.isPresent()) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_STANDARD_DELIVERY, standardDelivery.get()));
        }
        if (frequencyCap > 0) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_FREQUENCY_CAP, frequencyCap));
        }
        if (durationInDays > 0) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_DURATION_IN_DAYS, durationInDays));
        }

        return params;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy