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

twitter4jads.impl.TwitterAdsMediaApiImpl Maven / Gradle / Ivy

The newest version!
package twitter4jads.impl;

import com.google.common.collect.Lists;
import com.google.common.reflect.TypeToken;
import twitter4jads.*;
import twitter4jads.api.TwitterAdsMediaApi;
import twitter4jads.models.media.*;
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.TwitterException;
import twitter4jads.models.ads.HttpVerb;
import twitter4jads.util.TwitterAdUtil;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 *
 * Date: 16/05/16 12:25 PM.
 */
public class TwitterAdsMediaApiImpl implements TwitterAdsMediaApi {


    public static final int TWITTER_MAX_LIBRARY_GET_COUNT = 50;
    private final TwitterAdsClient twitterAdsClient;

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

    @Override
    public BaseAdsListResponseIterable getAccountMediaForAccount(String accountId, String sortBy) throws TwitterException {
        TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);

        final String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_ACCOUNT_MEDIA;
        final List parameters = Lists.newArrayList();
        parameters.add(new HttpParameter(TwitterAdsConstants.PARAM_ACCOUNT_ID, accountId));
        if (StringUtils.isNotBlank(sortBy)) {
            parameters.add(new HttpParameter(TwitterAdsConstants.PARAM_SORT_BY, sortBy));
        }

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

    @Override
    public BaseAdsResponse getAccountMediaById(String accountId, String accountMediaId) throws TwitterException {
        TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
        TwitterAdUtil.ensureNotNull(accountMediaId, "Account Media Id");

        final String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_ACCOUNT_MEDIA + "/" + accountMediaId;
        final Type type = new TypeToken>() {
        }.getType();
        return twitterAdsClient.executeHttpRequest(baseUrl, null, type, HttpVerb.GET);
    }

    @Override
    public BaseAdsListResponseIterable getMediaCreativesForAccount(String accountId, Boolean fetchDeleted)
            throws TwitterException {
        TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);

        final String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_MEDIA_CREATIVES;
        final List parameters = Lists.newArrayList();
        parameters.add(new HttpParameter(TwitterAdsConstants.PARAM_ACCOUNT_ID, accountId));
        if (fetchDeleted != null) {
            parameters.add(new HttpParameter(TwitterAdsConstants.PARAM_WITH_DELETED, fetchDeleted));
        }

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

    @Override
    public BaseAdsResponse getMediaCreativeByKeyFromLibrary(String accountId, String mediaKey) throws TwitterException {
        TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
        TwitterAdUtil.ensureNotNull(mediaKey, "Media Key");

        final String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_MEDIA_LIBRARY + "/" + mediaKey;
        final Type type = new TypeToken>() {
        }.getType();
        return twitterAdsClient.executeHttpRequest(baseUrl, null, type, HttpVerb.GET);
    }

    @Override
    public BaseAdsListResponseIterable getMediaFromLibraryForAccount(String accountId, Integer count, String cursor,
                                                                                          TwitterMediaLibraryType mediaType, String q)
            throws TwitterException {
        TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);

        final String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_MEDIA_LIBRARY;
        final List parameters = Lists.newArrayList();
        parameters.add(new HttpParameter(TwitterAdsConstants.PARAM_ACCOUNT_ID, accountId));
        if (StringUtils.isNotBlank(cursor)) {
            parameters.add(new HttpParameter(TwitterAdsConstants.PARAM_CURSOR, cursor));
        }
        if (mediaType != null) {
            parameters.add(new HttpParameter(TwitterAdsConstants.PARAM_MEDIA_TYPE, mediaType.name()));
        }
        if (count == null) {
            parameters.add(new HttpParameter(TwitterAdsConstants.PARAM_COUNT, 50));
        }
        if (TwitterAdUtil.isNotNullOrEmpty(q)) {
            parameters.add(new HttpParameter(TwitterAdsConstants.PARAM_Q, q));
        }

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


    @Override
    public BaseAdsResponse deleteMediaCreative(String accountId, String mediaKey) throws TwitterException {
        TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
        TwitterAdUtil.ensureNotNull(mediaKey, "Media Id");

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

    @Override
    public BaseAdsResponse createAccountMediaCreative(String accountId, String lineItemId, String accountMediaId,
                                                                                   String landingUrl) throws TwitterException {
        TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
        TwitterAdUtil.ensureNotNull(lineItemId, "Line Item Id");
        TwitterAdUtil.ensureNotNull(accountMediaId, "Account Media Id");

        final List params = new ArrayList<>();
        params.add(new HttpParameter(TwitterAdsConstants.PARAM_ACCOUNT_MEDIA_ID, accountMediaId));
        params.add(new HttpParameter(TwitterAdsConstants.PARAM_ACCOUNT_ID, accountId));
        params.add(new HttpParameter(TwitterAdsConstants.PARAM_LINE_ITEM_ID, lineItemId));
        if (StringUtils.isNotBlank(landingUrl)) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_LANDING_URL, landingUrl));
        }

        final String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_MEDIA_CREATIVES;
        final Type type = new TypeToken>() {
        }.getType();
        return twitterAdsClient.executeHttpRequest(baseUrl, params.toArray(new HttpParameter[params.size()]), type, HttpVerb.POST);
    }

    @Override
    public TwitterLibraryMedia createAndGetLibraryMedia(String accountId, String mediaKey, TwitterMediaLibraryCategory mediaCategory, String name,
                                                        String title, String description, String posterImageMediaId, String fileName)
            throws TwitterException {
        BaseAdsResponse channelResponse =
                associateMediaToLibrary(accountId, mediaKey, mediaCategory, name, title, description, posterImageMediaId, fileName);
        if (channelResponse.getData() == null) {
            throw new TwitterException("Could not associate media to library, please retry");
        }

        final TwitterLibraryMedia video = channelResponse.getData();
        return twitterAdsClient.waitForMediaProcessing(accountId, video.getMediaKey(), TimeUnit.MINUTES.toMillis(10));
    }

    @Override
    public BaseAdsResponse updateLibraryMediaByKey(String accountId, String mediaKey, String name, String title,
                                                                        String description, String posterMediaKey, String fileName)
            throws TwitterException {
        TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
        TwitterAdUtil.ensureNotNull(mediaKey, "Video Media Key");
        final List params = Lists.newArrayList();

        if (TwitterAdUtil.isNotNullOrEmpty(name)) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_NAME, name));
        }
        if (TwitterAdUtil.isNotNullOrEmpty(title)) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_TITLE, title));
        }
        if (TwitterAdUtil.isNotNullOrEmpty(description)) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_DESCRIPTION, description));
        }
        if (TwitterAdUtil.isNotNullOrEmpty(posterMediaKey)) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_POSTER_MEDIA_KEY, posterMediaKey));
        }
        if (TwitterAdUtil.isNotNullOrEmpty(fileName)) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_FILE_NAME, fileName));
        }

        final String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_MEDIA_LIBRARY + "/" + mediaKey;
        final Type type = new TypeToken>() {
        }.getType();
        return twitterAdsClient.executeHttpRequest(url, params.toArray(new HttpParameter[params.size()]), type, HttpVerb.PUT);
    }

    @Override
    public TwitterLibraryMedia waitForProcessingAndGetMedia(String accountId, String mediaKey) throws TwitterException {
        TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
        TwitterAdUtil.ensureNotNull(mediaKey, "Media Key");
        return twitterAdsClient.waitForMediaProcessing(accountId, mediaKey, TimeUnit.MINUTES.toMillis(4));
    }

    @Override
    public BaseAdsResponse deleteLibraryMediaByKey(String accountId, String mediaKey) throws TwitterException {
        TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);
        TwitterAdUtil.ensureNotNull(mediaKey, "Media Key");

        final String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_MEDIA_LIBRARY + TwitterAdsConstants.SLASH + mediaKey;
        Type type = new TypeToken>() {
        }.getType();

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

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

    private BaseAdsResponse associateMediaToLibrary(String accountId, String mediaKey, TwitterMediaLibraryCategory mediaCategory,
                                                                         String name, String title,
                                                                         String description,
                                                                         String posterMediaKey, String fileName)
            throws TwitterException {
        TwitterAdUtil.ensureNotNull(accountId, TwitterAdsConstants.ACCOUNT_ID);

        final List params = new ArrayList<>();

        TwitterAdUtil.ensureNotNull(mediaKey, "Media Key");
        params.add(new HttpParameter(TwitterAdsConstants.PARAM_MEDIA_KEY, mediaKey));

        if (StringUtils.isNotBlank(description)) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_DESCRIPTION, description));
        }
        if (StringUtils.isNotBlank(fileName)) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_FILE_NAME, fileName));
        }
        if (StringUtils.isNotBlank(name)) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_NAME, name));
        }
        if (StringUtils.isNotBlank(posterMediaKey)) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_POSTER_MEDIA_KEY, posterMediaKey));
        }
        if (StringUtils.isNotBlank(title)) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_TITLE, title));
        }

        final String url = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PREFIX_ACCOUNTS_URI + accountId + TwitterAdsConstants.PATH_MEDIA_LIBRARY;
        final HttpParameter[] parameters = params.toArray(new HttpParameter[params.size()]);
        Type type = new TypeToken>() {
        }.getType();

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy