use of org.talend.components.marketo.runtime.client.type.MarketoException in project components by Talend.
the class MarketoBulkExecClient method executeDownloadFileRequest.
public void executeDownloadFileRequest(File filename) throws MarketoException {
String err;
try {
URL url = new URL(current_uri.toString());
HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
urlConn.setRequestMethod("GET");
urlConn.setRequestProperty("accept", "text/json");
int responseCode = urlConn.getResponseCode();
if (responseCode == 200) {
InputStream inStream = urlConn.getInputStream();
FileUtils.copyInputStreamToFile(inStream, filename);
} else {
err = String.format("Download failed for %s. Status: %d", filename, responseCode);
throw new MarketoException(REST, err);
}
} catch (IOException e) {
err = String.format("Download failed for %s. Cause: %s", filename, e.getMessage());
LOG.error(err);
throw new MarketoException(REST, err);
}
}
use of org.talend.components.marketo.runtime.client.type.MarketoException in project components by Talend.
the class MarketoBulkExecClient method bulkImport.
/**
* Imports a spreadsheet of leads or custom objects into the target instance
*
* POST /bulk/v1/leads/import.json
*
* POST /bulk/v1/customobjects/{apiName}/import.json
*
* @param parameters
* @return
*/
public MarketoRecordResult bulkImport(TMarketoBulkExecProperties parameters) {
String importFilename = parameters.bulkFilePath.getValue();
String format = parameters.bulkFileFormat.getValue().name();
Integer pollWaitTime = parameters.pollWaitTime.getValue();
String logDownloadPath = parameters.logDownloadPath.getValue();
String lookupField;
Integer listId;
String partitionName;
String customObjectName;
current_uri = new StringBuilder(bulkPath);
Boolean isImportingLeads = parameters.bulkImportTo.getValue().equals(BulkImportTo.Leads);
if (isImportingLeads) {
lookupField = parameters.lookupField.getValue().name();
listId = parameters.listId.getValue();
partitionName = parameters.partitionName.getValue();
//
current_uri.append(API_PATH_BULK_LEADS).append(//
fmtParams(FIELD_ACCESS_TOKEN, accessToken, true)).append(fmtParams(FIELD_LOOKUP_FIELD, lookupField));
if (listId != null) {
current_uri.append(fmtParams(FIELD_LIST_ID, listId));
}
if (!StringUtils.isEmpty(partitionName)) {
current_uri.append(fmtParams(FIELD_PARTITION_NAME, partitionName));
}
} else {
customObjectName = parameters.customObjectName.getValue();
//
current_uri.append(String.format(API_PATH_BULK_CUSTOMOBJECTS, customObjectName)).append(fmtParams(FIELD_ACCESS_TOKEN, accessToken, true));
}
current_uri.append(fmtParams(FIELD_FORMAT, format));
//
MarketoRecordResult mkto = new MarketoRecordResult();
try {
LOG.debug("bulkImport {}.", current_uri);
BulkImportResult rs = executePostFileRequest(BulkImportResult.class, importFilename);
LOG.debug("rs = {}.", rs);
mkto.setRequestId(REST + "::" + rs.getRequestId());
mkto.setSuccess(rs.isSuccess());
if (!mkto.isSuccess()) {
mkto.setRecordCount(0);
mkto.setErrors(Arrays.asList(new MarketoError(REST, rs.getErrors().get(0).getCode(), messages.getMessage("bulkimport.error.import", rs.getErrors().get(0).getMessage()))));
return mkto;
}
BulkImport bulkResult = rs.getResult().get(0);
if (bulkResult.getStatus().equals(BULK_STATUS_FAILED)) {
// request Failed
String err = messages.getMessage("bulkimport.status.failed", bulkResult.getMessage());
LOG.error("{}.", err);
mkto.setSuccess(false);
mkto.setErrors(Arrays.asList(new MarketoError(REST, err)));
} else if (bulkResult.getStatus().equals(BULK_STATUS_COMPLETE)) {
// already Complete
bulkResult = getStatusesForBatch(bulkResult, logDownloadPath);
} else {
// Importing, Queued
while (true) {
try {
LOG.warn(messages.getMessage("bulkimport.status.waiting", pollWaitTime));
Thread.sleep(pollWaitTime * 1000L);
current_uri = new StringBuilder(bulkPath);
if (bulkResult.isBulkLeadsImport()) {
current_uri.append(String.format(API_PATH_BULK_LEADS_RESULT_STATUS, bulkResult.getBatchId()));
} else {
current_uri.append(String.format(API_PATH_BULK_CUSTOMOBJECTS_RESULT, bulkResult.getObjectApiName(), bulkResult.getBatchId(), "status"));
}
current_uri.append(fmtParams(FIELD_ACCESS_TOKEN, accessToken, true));
LOG.debug("status = {}.", current_uri);
rs = (BulkImportResult) executeGetRequest(BulkImportResult.class);
if (rs.isSuccess() && rs.getResult().get(0).getStatus().equals(BULK_STATUS_COMPLETE)) {
bulkResult = getStatusesForBatch(rs.getResult().get(0), logDownloadPath);
break;
} else {
LOG.warn(messages.getMessage("bulkimport.status.current", rs.getResult().get(0).getStatus()));
}
} catch (InterruptedException e) {
LOG.error("Sleep interrupted : {}", e);
throw new MarketoException(REST, "Sleep interrupted : " + e.getLocalizedMessage());
}
}
}
mkto.setRecords(Arrays.asList(bulkResult.toIndexedRecord()));
mkto.setRecordCount(1);
mkto.setRemainCount(0);
} catch (MarketoException e) {
mkto.setSuccess(false);
mkto.setErrors(Arrays.asList(e.toMarketoError()));
}
return mkto;
}
use of org.talend.components.marketo.runtime.client.type.MarketoException in project components by Talend.
the class MarketoLeadClient method getRecordResultForLead.
public MarketoRecordResult getRecordResultForLead(InputOperation operation, String requestParams, int limit, Schema schema, Map<String, String> mappings) {
MarketoRecordResult mkto = new MarketoRecordResult();
try {
PaginateResult result = null;
switch(operation) {
case getLead:
case getMultipleLeads:
result = executeFakeGetRequestForLead(requestParams);
break;
case getLeadActivity:
result = (LeadActivitiesResult) executeGetRequest(LeadActivitiesResult.class);
break;
case getLeadChanges:
result = (LeadChangesResult) executeGetRequest(LeadChangesResult.class);
break;
default:
throw new IllegalArgumentException("Invalid operation for getRecordResultForLead: " + operation);
}
mkto.setSuccess(result.isSuccess());
if (mkto.isSuccess()) {
mkto.setRecordCount(result.getResult().isEmpty() ? 0 : result.getResult().size());
mkto.setRemainCount((result.getNextPageToken() != null) ? limit : 0);
mkto.setStreamPosition(result.getNextPageToken());
if (mkto.getRecordCount() > 0) {
switch(operation) {
case getLead:
case getMultipleLeads:
mkto.setRecords(convertLeadRecords(((LeadResult) result).getResult(), schema, mappings));
break;
case getLeadActivity:
mkto.setRecords(convertLeadActivityRecords(((LeadActivitiesResult) result).getResult(), schema, mappings));
break;
case getLeadChanges:
mkto.setRecords(convertLeadChangesRecords(((LeadChangesResult) result).getResult(), schema, mappings));
break;
default:
throw new IllegalArgumentException("Invalid operation for getRecordResultForLead: " + operation);
}
}
} else {
mkto.setErrors(result.getErrors());
}
} catch (MarketoException e) {
LOG.error("Error {}.", e.toString());
mkto.setSuccess(false);
mkto.setErrors(Collections.singletonList(e.toMarketoError()));
}
return mkto;
}
use of org.talend.components.marketo.runtime.client.type.MarketoException in project components by Talend.
the class MarketoLeadClient method getMultipleLeads.
@Override
public MarketoRecordResult getMultipleLeads(TMarketoInputProperties parameters, String offset) {
String filter;
String[] filterValues;
String[] fields = parameters.mappingInput.getMarketoColumns(parameters.schemaInput.schema.getValue()).toArray(new String[] {});
int batchLimit = parameters.batchSize.getValue() > REST_API_BATCH_LIMIT ? REST_API_BATCH_LIMIT : parameters.batchSize.getValue();
StringBuilder input = new StringBuilder();
if (parameters.leadSelectorREST.getValue().equals(LeadKeySelector)) {
filter = parameters.leadKeyTypeREST.getValue().toString();
if ("Custom".equals(filter)) {
filter = parameters.customLeadKeyType.getValue();
}
filterValues = parameters.leadKeyValues.getValue().split(",");
current_uri = //
new StringBuilder(basicPath).append(//
API_PATH_LEADS).append(fmtParams(FIELD_ACCESS_TOKEN, accessToken, true));
input.append(FIELD_FILTER_TYPE + "=" + filter);
if (fields != null && fields.length > 0) {
input.append(fmtParams(FIELD_FIELDS, csvString(fields)));
}
if (filterValues != null && filterValues.length > 0) {
input.append(fmtParams(FIELD_FILTER_VALUES, csvString(filterValues)));
}
input.append(fmtParams(FIELD_BATCH_SIZE, batchLimit));
if (offset != null && offset.length() > 0) {
input.append(fmtParams(FIELD_NEXT_PAGE_TOKEN, offset));
}
LOG.debug("MultipleLeads: {} body{}", current_uri, input);
} else {
int listId;
if (parameters.listParam.getValue().equals(ListParam.STATIC_LIST_NAME)) {
try {
listId = getListIdByName(parameters.listParamListName.getValue());
} catch (MarketoException e) {
LOG.error("getListIdByName Error: `{}`.", e.toString());
MarketoRecordResult mkto = new MarketoRecordResult();
mkto.setSuccess(false);
mkto.setErrors(Collections.singletonList(e.toMarketoError()));
return mkto;
}
} else {
listId = parameters.listParamListId.getValue();
}
current_uri = //
new StringBuilder(basicPath).append(//
API_PATH_LIST).append(//
listId).append(//
API_PATH_LEADS_JSON).append(fmtParams(FIELD_ACCESS_TOKEN, accessToken, true));
if (fields != null && fields.length > 0) {
input.append(fmtParams(FIELD_FIELDS, csvString(fields)));
}
input.append(fmtParams(FIELD_BATCH_SIZE, batchLimit));
if (offset != null) {
input.append(fmtParams(FIELD_NEXT_PAGE_TOKEN, offset));
}
LOG.debug("LeadsByList : {} body: {}.", current_uri, input);
}
return getRecordResultForLead(parameters.inputOperation.getValue(), input.toString(), batchLimit, parameters.schemaInput.schema.getValue(), parameters.mappingInput.getNameMappingsForMarketo());
}
use of org.talend.components.marketo.runtime.client.type.MarketoException in project components by Talend.
the class MarketoLeadClient method getListIdByName.
public Integer getListIdByName(String listName) throws MarketoException {
current_uri = //
new StringBuilder(basicPath).append(//
API_PATH_LISTS_JSON).append(//
fmtParams(FIELD_ACCESS_TOKEN, accessToken, true)).append(fmtParams(FIELD_NAME, listName));
StaticListResult getResponse = (StaticListResult) executeGetRequest(StaticListResult.class);
if (getResponse == null) {
throw new MarketoException(REST, messages.getMessage("error.response.null"));
}
if (!getResponse.isSuccess()) {
throw new MarketoException(REST, getResponse.getErrors().toString());
}
if (getResponse.getResult().isEmpty()) {
throw new MarketoException(REST, "No list match `" + listName + "`.");
}
return getResponse.getResult().get(0).getId();
}
Aggregations