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

twitter4jads.impl.TwitterAdsTargetingApiImpl Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package twitter4jads.impl;

import com.google.gson.reflect.TypeToken;
import twitter4jads.*;
import twitter4jads.api.TwitterAdsTargetingApi;
import twitter4jads.internal.models4j.LocationType;
import twitter4jads.models.ads.*;
import twitter4jads.internal.http.HttpParameter;
import twitter4jads.internal.models4j.TwitterException;
import twitter4jads.util.TwitterAdUtil;
import java.lang.reflect.Type;
import java.util.*;

/**
 *
 * Date: 4/4/16
 * Time: 7:16 PM
 */
public class TwitterAdsTargetingApiImpl implements TwitterAdsTargetingApi {
    private final TwitterAdsClient twitterAdsClient;

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

    @Override
    public BaseAdsListResponseIterable getAllTargetingPlatforms(String q) throws TwitterException {
        return hitQueryForPath(q, TwitterAdsConstants.PATH_TARGETING_CRITERIA_PLATFORMS);
    }

    @Override
    public BaseAdsListResponseIterable getAllTargetingLocations(Optional locationType, String q,
                                                                                   String countryCode, Optional count)
            throws TwitterException {
        List params = validateTargetingLocationParameters(locationType, q, countryCode, count);
        String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + TwitterAdsConstants.PATH_TARGETING_CRITERIA_LOCATION;

        Type type = new TypeToken>() {
        }.getType();

        return twitterAdsClient.executeHttpListRequest(baseUrl, params, type);
    }

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

    @SuppressWarnings("Duplicates")
    private  BaseAdsListResponseIterable hitQueryForPath(String q, String queryPath) throws
            TwitterException {
        return hitQueryForPath(q, queryPath, null);
    }

    private  BaseAdsListResponseIterable hitQueryForPath(String q, String queryPath, TwitterOSType osType)
            throws TwitterException {
        final List params = new ArrayList<>();
        if (TwitterAdUtil.isNotNullOrEmpty(q)) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_Q, q));
        }
        if (TwitterAdUtil.isNotNull(osType)) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_OS_TYPE, osType.name()));
        }

        final String baseUrl = twitterAdsClient.getBaseAdsAPIUrl() + queryPath;
        final Type type = new TypeToken>() {
        }.getType();

        return twitterAdsClient.executeHttpListRequest(baseUrl, params, type);
    }
    private List validateTargetingLocationParameters(final Optional locationType,
                                                                    final String q, final String countryCode,
                                                                    final Optional count) {

        List params = new ArrayList<>();
        if (locationType != null && locationType.isPresent()) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_LOCATION_TYPE, locationType.get().name()));
        }
        if (TwitterAdUtil.isNotNullOrEmpty(q)) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_Q, q));
        }
        if (TwitterAdUtil.isNotNullOrEmpty(countryCode)) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_COUNTRY_CODE, countryCode));
        }
        if (count != null && count.isPresent()) {
            params.add(new HttpParameter(TwitterAdsConstants.PARAM_COUNT, count.get()));
        }
        return params;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy