Skip to content

Commit

Permalink
Using hardcoded/encoded mailchimp key
Browse files Browse the repository at this point in the history
  • Loading branch information
VSydor committed Jun 24, 2024
1 parent 1cc8016 commit 9fa5cc2
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -68,19 +67,14 @@ public class MailchimpClient {
public static final String TAG_INACTIVE = "inactive";

// 2 hours
protected static final Integer BATCH_STATUS_RETRY_TIMEOUT_IN_SECONDS = 300;
protected static final Integer BATCH_STATUS_RETRY_TIMEOUT_IN_SECONDS = 20;
protected static final Integer BATCH_STATUS_MAX_RETRIES = 24;

protected final com.ecwid.maleorang.MailchimpClient client;
protected final String apiKey;
protected final Environment env;

public MailchimpClient(EnvironmentConfig.CommunicationPlatform mailchimpConfig, Environment env) {
String profile = System.getenv("PROFILE");
System.out.println("profile: " + profile);
apiKey = !Strings.isNullOrEmpty(profile) && profile.toLowerCase(Locale.ROOT).contains("it") ?
env.decodeBase64(mailchimpConfig.secretKey) : mailchimpConfig.secretKey;
client = new com.ecwid.maleorang.MailchimpClient(apiKey);
client = new com.ecwid.maleorang.MailchimpClient(mailchimpConfig.secretKey);
this.env = env;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void processCampaignEvent(AudienceEvent event, Environment env) throws E
env.logJobError("unable to find ListID={}", event.listId);
return;
}
MailchimpClient mailchimpClient = new MailchimpClient(mailchimpConfig, env);
MailchimpClient mailchimpClient = env.mailchimpClient(mailchimpConfig);

List<SentToInfo> sentTos = mailchimpClient.getCampaignRecipients(event.id);
List<String> emails = sentTos.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.impactupgrade.nucleus.client.DonorWranglerClient;
import com.impactupgrade.nucleus.client.EventBriteClient;
import com.impactupgrade.nucleus.client.FactsClient;
import com.impactupgrade.nucleus.client.MailchimpClient;
import com.impactupgrade.nucleus.client.RaiselyClient;
import com.impactupgrade.nucleus.client.PaypalClient;
import com.impactupgrade.nucleus.client.SfdcBulkClient;
Expand Down Expand Up @@ -44,7 +45,6 @@
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -275,6 +275,7 @@ private <T extends SegmentService> List<T> segmentServices(Class<T> clazz) {
public StripeClient stripeClient() { return new StripeClient(this); }
public TwilioClient twilioClient() { return new TwilioClient(this); }
public VirtuousClient virtuousClient() { return new VirtuousClient(this); }
public MailchimpClient mailchimpClient(EnvironmentConfig.CommunicationPlatform mailchimpConfig) { return new MailchimpClient(mailchimpConfig, this); }

// job logging services

Expand Down Expand Up @@ -312,12 +313,4 @@ public void logJobError(String message, Object... params) {
public void endJobLog(JobStatus jobStatus) {
jobLoggingServices().forEach(logger -> logger.endLog(jobStatus));
}

public String decodeBase64(String encoded) {
if (encoded == null) {
return null;
} else {
return new String(Base64.getDecoder().decode(encoded));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected CrmContact asCrmContact(CrmAccount crmAccount) {

protected void syncContacts(List<CrmContact> crmContacts, EnvironmentConfig.Mailchimp mailchimpConfig,
EnvironmentConfig.CommunicationList communicationList) throws Exception {
MailchimpClient mailchimpClient = new MailchimpClient(mailchimpConfig, env);
MailchimpClient mailchimpClient = env.mailchimpClient(mailchimpConfig);

List<CrmContact> contactsToUpsert = new ArrayList<>();
List<CrmContact> contactsToArchive = new ArrayList<>();
Expand Down Expand Up @@ -175,7 +175,7 @@ protected Map<String, Set<String>> getActiveTags(List<CrmContact> crmContacts, M
@Override
public void syncUnsubscribes(Calendar lastSync) throws Exception {
for (EnvironmentConfig.CommunicationPlatform mailchimpConfig : env.getConfig().mailchimp) {
MailchimpClient mailchimpClient = new MailchimpClient(mailchimpConfig, env);
MailchimpClient mailchimpClient = env.mailchimpClient(mailchimpConfig);
for (EnvironmentConfig.CommunicationList communicationList : mailchimpConfig.lists) {
List<MemberInfo> unsubscribedMembers = mailchimpClient.getListMembers(communicationList.id, "unsubscribed", lastSync);
syncUnsubscribed(getEmails(unsubscribedMembers));
Expand Down Expand Up @@ -256,7 +256,7 @@ public void upsertContact(String contactId) throws Exception {
// needs added, like a campaign tag to kick off a Journey in MC itself.
protected void upsertContact(EnvironmentConfig.Mailchimp mailchimpConfig,
EnvironmentConfig.CommunicationList communicationList, CrmContact crmContact) throws Exception {
MailchimpClient mailchimpClient = new MailchimpClient(mailchimpConfig, env);
MailchimpClient mailchimpClient = env.mailchimpClient(mailchimpConfig);

// transactional is always subscribed
if (communicationList.type != EnvironmentConfig.CommunicationListType.TRANSACTIONAL && !crmContact.canReceiveEmail()) {
Expand Down
1 change: 0 additions & 1 deletion src/test/java/com/impactupgrade/nucleus/it/AbstractIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public abstract class AbstractIT extends JerseyTest {
protected static final EnvironmentFactory envFactoryHubspotStripe = new EnvironmentFactory("environment-it-hubspot-stripe.json");
protected static final EnvironmentFactory envFactorySfdcStripe = new EnvironmentFactory("environment-it-sfdc-stripe.json");
protected static final EnvironmentFactory envFactoryVirtuousStripe = new EnvironmentFactory("environment-it-virtuous-stripe.json");
protected static final EnvironmentFactory envFactorySfdcMailchimp = new EnvironmentFactory("environment-it-sfdc-mailchimp.json");

protected final App app;
protected final Environment env;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.ecwid.maleorang.method.v3_0.lists.members.MemberInfo;
import com.impactupgrade.nucleus.App;
import com.impactupgrade.nucleus.client.MailchimpClient;
import com.impactupgrade.nucleus.environment.Environment;
import com.impactupgrade.nucleus.environment.EnvironmentConfig;
import com.impactupgrade.nucleus.environment.EnvironmentFactory;
import com.impactupgrade.nucleus.model.CrmContact;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Test;
Expand All @@ -13,6 +15,7 @@
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -21,13 +24,27 @@
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class MailchimpCommunicationServiceIT extends AbstractIT {

protected MailchimpCommunicationServiceIT() {
super(new App(envFactorySfdcMailchimp));
super(new App(new EnvironmentFactory("environment-it-sfdc-mailchimp.json") {
@Override
public Environment newEnv() {
return new Environment() {
@Override
public MailchimpClient mailchimpClient(EnvironmentConfig.CommunicationPlatform mailchimpConfig) {
//TODO: use [email protected] account data (key and list id)
String decoded = new String(Base64.getDecoder().decode("NzA5ZDY2NjIyZmUzNDdjYjY5NDczODdmZWRmM2E1MTAtdXMxNA=="));
mailchimpConfig.secretKey = decoded;
return new MailchimpClient(mailchimpConfig,this);
}
};
}
}));
}

@Test
Expand Down Expand Up @@ -64,7 +81,7 @@ public void syncContacts() throws Exception {

EnvironmentConfig.Mailchimp mailchimp = env.getConfig().mailchimp.get(0);
String listId = mailchimp.lists.get(0).id;
MailchimpClient mailchimpClient = new MailchimpClient(mailchimp, env);
MailchimpClient mailchimpClient = env.mailchimpClient(mailchimp);

assertEmailsStatus(emails, "subscribed", listId, mailchimpClient);
}
Expand Down Expand Up @@ -106,7 +123,7 @@ public void syncUnsubscribes() throws Exception {
// Subscribe emails to MC list
EnvironmentConfig.Mailchimp mailchimp = env.getConfig().mailchimp.get(0);
String listId = mailchimp.lists.get(0).id;
MailchimpClient mailchimpClient = new MailchimpClient(mailchimp, env);
MailchimpClient mailchimpClient = env.mailchimpClient(mailchimp);

addEmailsToList(emails, "subscribed", listId, mailchimpClient);
Thread.sleep(10000);
Expand All @@ -122,14 +139,14 @@ public void syncUnsubscribes() throws Exception {
env.communicationService("mailchimp").syncContacts(beforeBulkImport);

crmContacts = env.primaryCrmService().getContactsByEmails(unsubscribeEmails);
assertTrue(!crmContacts.isEmpty());
assertFalse(crmContacts.isEmpty());

for (CrmContact crmContact : crmContacts) {
assertTrue(Boolean.TRUE == crmContact.emailOptOut);
}

crmContacts = env.primaryCrmService().getContactsByEmails(cleanEmails);
assertTrue(!crmContacts.isEmpty());
assertFalse(crmContacts.isEmpty());

for (CrmContact crmContact : crmContacts) {
assertTrue(Boolean.TRUE == crmContact.emailBounced);
Expand Down
1 change: 0 additions & 1 deletion src/test/resources/environment-it-sfdc-mailchimp.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

"mailchimp": [
{
"secretKey": "NzA5ZDY2NjIyZmUzNDdjYjY5NDczODdmZWRmM2E1MTAtdXMxNA==",
"lists": [
{
"id": "3d215dac12",
Expand Down

0 comments on commit 9fa5cc2

Please sign in to comment.