Skip to content

Commit

Permalink
Merge pull request #38 from treasure-data/refactor_check_list_id
Browse files Browse the repository at this point in the history
Refactor check list_id to avoid confusing when get 404 error
  • Loading branch information
thangnc authored Oct 16, 2017
2 parents 58fd397 + 9463509 commit 57c9ef5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
24 changes: 19 additions & 5 deletions src/main/java/org/embulk/output/mailchimp/MailChimpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.eclipse.jetty.client.HttpResponseException;
import org.eclipse.jetty.http.HttpMethod;
import org.embulk.config.ConfigException;
import org.embulk.output.mailchimp.MailChimpOutputPluginDelegate.PluginTask;
import org.embulk.output.mailchimp.helper.MailChimpHelper;
import org.embulk.output.mailchimp.model.CategoriesResponse;
import org.embulk.output.mailchimp.model.ErrorResponse;
Expand Down Expand Up @@ -56,14 +57,15 @@ public class MailChimpClient
*
* @param task the task
*/
public MailChimpClient(final MailChimpOutputPluginDelegate.PluginTask task)
public MailChimpClient(final PluginTask task)
{
mailchimpEndpoint = Joiner.on("/").join("https://{0}.api.mailchimp.com", API_VERSION);
this.client = new MailChimpHttpClient(task);
this.mapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, false);
extractDataCenter(task);
findList(task);
}

/**
Expand All @@ -75,7 +77,7 @@ public MailChimpClient(final MailChimpOutputPluginDelegate.PluginTask task)
* @return the report response
* @throws JsonProcessingException the json processing exception
*/
ReportResponse push(final ObjectNode node, MailChimpOutputPluginDelegate.PluginTask task)
ReportResponse push(final ObjectNode node, PluginTask task)
throws JsonProcessingException
{
String endpoint = MessageFormat.format(mailchimpEndpoint + "/lists/{0}",
Expand Down Expand Up @@ -123,7 +125,7 @@ void handleErrors(List<ErrorResponse> errorResponses)
* @return the map
* @throws JsonProcessingException the json processing exception
*/
Map<String, Map<String, InterestResponse>> extractInterestCategoriesByGroupNames(final MailChimpOutputPluginDelegate.PluginTask task)
Map<String, Map<String, InterestResponse>> extractInterestCategoriesByGroupNames(final PluginTask task)
throws JsonProcessingException
{
Map<String, Map<String, InterestResponse>> categories = new HashMap<>();
Expand Down Expand Up @@ -183,7 +185,7 @@ public String apply(CategoriesResponse input)
* @return the map
* @throws JsonProcessingException the json processing exception
*/
Map<String, MergeField> extractMergeFieldsFromList(MailChimpOutputPluginDelegate.PluginTask task) throws JsonProcessingException
Map<String, MergeField> extractMergeFieldsFromList(PluginTask task) throws JsonProcessingException
{
int count = 100;
int offset = 0;
Expand Down Expand Up @@ -215,7 +217,7 @@ Map<String, MergeField> extractMergeFieldsFromList(MailChimpOutputPluginDelegate
return convertMergeFieldToMap(allMergeFields);
}

private void extractDataCenter(MailChimpOutputPluginDelegate.PluginTask task)
private void extractDataCenter(final PluginTask task)
{
if (task.getAuthMethod() == OAUTH) {
// Extract data center from meta data URL
Expand Down Expand Up @@ -247,6 +249,18 @@ else if (task.getAuthMethod() == API_KEY && task.getApikey().isPresent()) {
}
}

private void findList(final PluginTask task)
{
String endpoint = MessageFormat.format(mailchimpEndpoint + "/lists/{0}",
task.getListId());
try {
client.sendRequest(endpoint, HttpMethod.GET, task);
}
catch (HttpResponseException hre) {
throw new ConfigException("The `list id` could not be found.");
}
}

private Map<String, InterestResponse> convertInterestCategoryToMap(final List<InterestResponse> interestResponseList)
{
Function<InterestResponse, String> function = new Function<InterestResponse, String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,13 @@ public boolean isResponseStatusToRetry(Response response)
{
int status = response.getStatus();

if (status == 404) {
LOG.error("Exception occurred while sending request: {}", response.getReason());
throw new ConfigException("The `list id` could not be found.");
}

return status == 429 || status / 100 != 4;
}

@Override
protected boolean isExceptionToRetry(Exception exception)
{
LOG.error("Exception to retry.", exception);
// This check is to make sure the exception is retryable, e.g: server not found, internal server error...
if (exception instanceof ConfigException || exception instanceof ExecutionException) {
return toRetry((Exception) exception.getCause());
Expand All @@ -108,7 +104,7 @@ protected boolean isExceptionToRetry(Exception exception)
return responseBody != null && !responseBody.isEmpty() ? parseJson(responseBody) : MissingNode.getInstance();
}
catch (Exception ex) {
LOG.info("Exception occurred while sending request.");
LOG.error("Exception occurred while sending request.", ex);
throw Throwables.propagate(ex);
}
}
Expand Down

0 comments on commit 57c9ef5

Please sign in to comment.