Skip to content
This repository has been archived by the owner on Oct 11, 2018. It is now read-only.

Commit

Permalink
Fixed get mailchimp mailing lists which was breaking because of incor…
Browse files Browse the repository at this point in the history
…rect extraction of api key.
  • Loading branch information
rishabh9 committed Jan 5, 2017
1 parent e0a6f5f commit f218c9c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 37 deletions.
13 changes: 7 additions & 6 deletions app/controllers/IncomingDataController.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,20 @@ private String getUrl(Installation installation, String configuredListId) {
}

private Result executeInstallationEvents(String event, JsonNode json, Messages messages) {
Data data;
try {
Data data = Json.fromJson(json.findPath("data"), Data.class);
if (event.equals(Event.PLUGIN_INSTALL)) {
return installationHelper.newInstall(data, messages);
} else {
return updateHelper.updateConfiguration(data, messages);
}
data = Json.fromJson(json.findPath("data"), Data.class);
} catch (Exception e) {
log.error("Missing or invalid data object.", e);
Map<String, String> errorMessages = new HashMap<>();
errorMessages.put("payload.data", messages.at(MessageKey.MISSING_PAYLOAD_DATA));
return badRequest(ErrorUtil.toJson(BAD_REQUEST, messages.at(MessageKey.INVALID_JSON), errorMessages));
}
if (event.equals(Event.PLUGIN_INSTALL)) {
return installationHelper.newInstall(data, messages);
} else {
return updateHelper.updateConfiguration(data, messages);
}
}

private CompletionStage<Result> complete(Result result) {
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/helpers/MailingListHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public CompletionStage<Result> getMailingListsAsJson(Installation installation,
.get()
.thenApply(wsResponse -> getListsForJson(wsResponse, installation));
} else {
log.error("Error querying mailchimp api key and datacentre for instalaltion {}",
log.error("Error querying mailchimp api key and datacentre for installation {}",
installation.getId().toHexString());
return CompletableFuture.completedFuture(internalServerError(
ErrorUtil.toJson(INTERNAL_SERVER_ERROR, messages.at(MessageKey.UNEXPECTED_ERROR))));
Expand Down
56 changes: 26 additions & 30 deletions app/utils/InstallationUtility.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package utils;

import models.Installation;
import org.springframework.util.StringUtils;
import models.Preference;

import javax.inject.Singleton;
import java.util.Optional;
Expand All @@ -12,47 +12,43 @@
/**
* @author rishabh
*/
@Deprecated
@Singleton
public class InstallationUtility {

public Optional<String> getValue(Installation installation, String key) {
if (null == installation
|| null == installation.getPreferences()
|| installation.getPreferences().isEmpty()
|| !StringUtils.hasText(key)) {
return Optional.empty();
}
// for (Preference configValues : installation.getPreferences()) {
// if (key.equals(configValues.getKey()) && StringUtils.hasText(configValues.getValues())) {
// return Optional.of(configValues.getValue());
// }
// }
return Optional.empty();
}

public Optional<String> getConfiguredListId(Installation installation) {
return getValue(installation, CONFIG_MC_LIST_ID);
Optional<String> value = Optional.empty();
for (Preference preference : installation.getPreferences()) {
if (preference.getKey().equals(CONFIG_MC_LIST_ID)) {
String label = preference.getValues().get(0).getLabel();
value = Optional.ofNullable(label);
}
}
return value;
}

public Optional<String> getDataCentre(Installation installation) {
return splitApiKey(installation, 1);
Optional<String> value = Optional.empty();
for (Preference preference : installation.getPreferences()) {
if (preference.getKey().equals(CONFIG_MC_API_KEY)) {
String label = preference.getValues().get(0).getLabel();
String[] arr = label.split("-");
value = Optional.ofNullable(arr[1]);
}
}
return value;
}

public Optional<String> getApiKey(Installation installation) {
return splitApiKey(installation, 1);
}

private Optional<String> splitApiKey(Installation installation, int part) {
if (part == 0 || part == 1) {
Optional<String> apiKey = this.getValue(installation, CONFIG_MC_API_KEY);
if (apiKey.isPresent() && StringUtils.hasText(apiKey.get())) {
String[] meta = apiKey.get().split("-");
if (meta.length == 2 && StringUtils.hasText(meta[part])) {
return Optional.of(meta[part]);
}
Optional<String> value = Optional.empty();
for (Preference preference : installation.getPreferences()) {
if (preference.getKey().equals(CONFIG_MC_API_KEY)) {
String label = preference.getValues().get(0).getLabel();
String[] arr = label.split("-");
value = Optional.ofNullable(arr[0]);
}
}
return Optional.empty();
return value;
}

}

0 comments on commit f218c9c

Please sign in to comment.