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

com.constantcontact.services.emailcampaigns.EmailCampaignService Maven / Gradle / Ivy

The newest version!
package com.constantcontact.services.emailcampaigns;

import com.constantcontact.components.Component;
import com.constantcontact.components.emailcampaigns.EmailCampaignRequest;
import com.constantcontact.components.emailcampaigns.EmailCampaignResponse;
import com.constantcontact.components.generic.response.Pagination;
import com.constantcontact.components.generic.response.ResultSet;
import com.constantcontact.exceptions.service.ConstantContactServiceException;
import com.constantcontact.services.base.BaseService;
import com.constantcontact.util.RawApiResponse;
import com.constantcontact.util.Config;
import com.constantcontact.util.ConstantContactExceptionFactory;

import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;

/**
 * Service Layer Implementation for the Email Campaign operations in Constant Contact.
 * 
 * @author ConstantContact
 * 
 */
public class EmailCampaignService extends BaseService implements IEmailCampaignService {

	private String accessToken;
	private String apiKey;
	
	/**
	 * @return the accessToken
	 */
	public String getAccessToken() {
		return accessToken;
	}
	/**
	 * @param accessToken the accessToken to set
	 */
	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}
	/**
	 * @return the apiKey
	 */
	public String getApiKey() {
		return apiKey;
	}
	/**
	 * @param apiKey the apiKey to set
	 */
	public void setApiKey(String apiKey) {
		this.apiKey = apiKey;
	}

	/**
	 * Gets all the Email Campaigns.
* Implements the get Campaigns operation of the Email Campaign API by calling the ConstantContact server side. * * @param limit The limit * @param modifiedSinceTimestamp This time stamp is an ISO-8601 ordinal date supporting offset.
* It will return only the Email Campaigns modified since the supplied date.
* If you want to bypass this filter set modifiedSinceTimestamp to null. * @return A {@link ResultSet} of {@link EmailCampaignResponse} containing data as returned by the server on success;
* An exception is thrown otherwise. * @throws ConstantContactServiceException When something went wrong in the Constant Contact flow or an error is returned from server. */ public ResultSet getCampaigns(Integer limit, String modifiedSinceTimestamp) throws ConstantContactServiceException { return getCampaigns(limit, modifiedSinceTimestamp, null); } /** * Gets all the Email Campaigns.
* Implements the get Campaigns operation of the Email Campaign API by calling the ConstantContact server side. * * @param pagination A {@link com.constantcontact.components.generic.response.Pagination} instance containing the link to the next page of results. * An exception is thrown otherwise. * @throws ConstantContactServiceException When something went wrong in the Constant Contact flow or an error is returned from server. */ public ResultSet getCampaigns(Pagination pagination) throws ConstantContactServiceException { if (pagination == null || pagination.getNextLink() == null) { throw new IllegalArgumentException(Config.instance().getErrorPaginationNull()); } return getCampaigns(null, null, pagination); } /** * Gets a single Email Campaign.
* Implements the get Campaign operation of the Email Campaign API by calling the ConstantContact server side. * * @param campaignId Id of the Email Campaign to get * @return An {@link EmailCampaignResponse} containing data as returned by the server on success;
* An exception is thrown otherwise. * @throws ConstantContactServiceException When something went wrong in the Constant Contact flow or an error is returned from server. */ public EmailCampaignResponse getCampaign(String campaignId) throws ConstantContactServiceException { if (campaignId == null || !(campaignId.length() > 0)) { throw new IllegalArgumentException(Config.instance().getErrorId()); } EmailCampaignResponse campaign = null; try { String url = String.format("%1$s%2$s", Config.instance().getBaseUrl(), String.format(Config.instance().getEmailCampaignsId(), campaignId)); RawApiResponse response = getRestClient().get(url); if (response.hasData()) { campaign = Component.fromJSON(response.getBody(), EmailCampaignResponse.class); } if (response.isError()) { throw ConstantContactExceptionFactory.createServiceException(response, url); } } catch (ConstantContactServiceException e) { throw new ConstantContactServiceException(e); } catch (Exception e) { throw new ConstantContactServiceException(e); } return campaign; } /** * Adds a single Email Campaign.
* Implements the add Campaign operation of the Email Campaign API by calling the ConstantContact server side. * * @param emailCampaign The Email Campaign to add * @return An {@link EmailCampaignResponse} containing data as returned by the server on success;
* An exception is thrown otherwise. * @throws ConstantContactServiceException When something went wrong in the Constant Contact flow or an error is returned from server. */ public EmailCampaignResponse addCampaign(EmailCampaignRequest emailCampaign) throws ConstantContactServiceException { if (emailCampaign == null) { throw new IllegalArgumentException(Config.instance().getErrorId()); } EmailCampaignResponse newEmailCampaign = null; try { String url = String.format("%1$s%2$s", Config.instance().getBaseUrl(), Config.instance().getEmailCampaigns()); String json = emailCampaign.toJSON(); RawApiResponse response = getRestClient().post(url, json); if (response.hasData()) { newEmailCampaign = Component.fromJSON(response.getBody(), EmailCampaignResponse.class); } if (response.isError()) { throw ConstantContactExceptionFactory.createServiceException(response, url); } } catch (ConstantContactServiceException e) { throw new ConstantContactServiceException(e); } catch (Exception e) { throw new ConstantContactServiceException(e); } return newEmailCampaign; } /** * Updates a single Email Campaign.
* Implements the update Campaign operation of the Email Campaign API by calling the ConstantContact server side. * * @param emailCampaign The Email Campaign to update; match is done by id on server-side * @return An {@link EmailCampaignResponse} containing data as returned by the server on success;
* An exception is thrown otherwise. * @throws ConstantContactServiceException When something went wrong in the Constant Contact flow or an error is returned from server. */ public EmailCampaignResponse updateCampaign(EmailCampaignRequest emailCampaign) throws ConstantContactServiceException { if (emailCampaign == null || !(emailCampaign.getId().length() > 0)) { throw new IllegalArgumentException(Config.instance().getErrorId()); } EmailCampaignResponse updateEmailCampaign = null; try { String url = String.format("%1$s%2$s", Config.instance().getBaseUrl(), String.format(Config.instance().getEmailCampaignsId(), emailCampaign.getId())); String json = emailCampaign.toJSON(); RawApiResponse response = getRestClient().put(url, json); if (response.hasData()) { updateEmailCampaign = Component.fromJSON(response.getBody(), EmailCampaignResponse.class); } if (response.isError()) { throw ConstantContactExceptionFactory.createServiceException(response, url); } } catch (ConstantContactServiceException e) { throw new ConstantContactServiceException(e); } catch (Exception e) { throw new ConstantContactServiceException(e); } return updateEmailCampaign; } /** * Deletes a single Email Campaign.
* Implements the delete Campaign operation of the Email Campaign API by calling the ConstantContact server side. * * @param emailCampaignId Id of the Email Campaign to delete * @return true on success;
* An exception is thrown otherwise. * @throws ConstantContactServiceException When something went wrong in the Constant Contact flow or an error is returned from server. */ public boolean deleteCampaign(String emailCampaignId) throws ConstantContactServiceException { try { String url = String.format("%1$s%2$s", Config.instance().getBaseUrl(), String.format(Config.instance().getEmailCampaignsId(), emailCampaignId)); RawApiResponse response = getRestClient().delete(url); if (response.isError()) { throw ConstantContactExceptionFactory.createServiceException(response, url); } return response.getStatusCode() == HttpURLConnection.HTTP_NO_CONTENT; } catch (ConstantContactServiceException e) { throw new ConstantContactServiceException(e); } catch (Exception e) { throw new ConstantContactServiceException(e); } } /** * Gets all the Email Campaigns.
* Implements the get Campaigns operation of the Email Campaign API by calling the ConstantContact server side. * * @param limit The limit * @param modifiedSinceTimestamp This time stamp is an ISO-8601 ordinal date supporting offset.
* It will return only the Email Campaigns modified since the supplied date.
* If you want to bypass this filter set modifiedSinceTimestamp to null. * @return A {@link ResultSet} of {@link EmailCampaignResponse} containing data as returned by the server on success;
* An exception is thrown otherwise. * @throws ConstantContactServiceException When something went wrong in the Constant Contact flow or an error is returned from server. */ private ResultSet getCampaigns(Integer limit, String modifiedSinceTimestamp, Pagination pagination) throws ConstantContactServiceException { ResultSet campaigns = null; String url; if (pagination == null) { url = paginateUrl(String.format("%1$s%2$s", Config.instance().getBaseUrl(), Config.instance().getEmailCampaigns()), limit); url = paginateUrl(url, limit); try { url = appendParam(url, "modified_since", modifiedSinceTimestamp); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } else { url = paginateUrl(Config.instance().getBaseUrl(), pagination.getNextLink(), null); } try { RawApiResponse response = getRestClient().get(url); if (response.hasData()) { campaigns = Component.resultSetFromJSON(response.getBody(), EmailCampaignResponse.class); } if (response.isError()) { throw ConstantContactExceptionFactory.createServiceException(response, url); } } catch (ConstantContactServiceException e) { throw new ConstantContactServiceException(e); } catch (Exception e) { throw new ConstantContactServiceException(e); } return campaigns; } /** * Default constructor. */ public EmailCampaignService(String accessToken, String apiKey) { super(accessToken, apiKey); this.setAccessToken(accessToken); this.setApiKey(apiKey); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy