use of org.talend.components.marketo.runtime.client.type.MarketoError in project components by Talend.
the class MarketoResultTest method testMarketoResultConstruct.
@Test
public void testMarketoResultConstruct() {
result = new MarketoRecordResult();
assertEquals(emptyList(), result.getRecords());
assertEquals(0, result.getRecordCount());
assertEquals(0, result.getRemainCount());
assertEquals("", result.getStreamPosition());
assertEquals("", result.getRequestId());
assertFalse(result.isSuccess());
assertNotNull(result.getErrors());
assertEquals(0, result.getErrors().size());
result.setRecordCount(100);
result.setRemainCount(100);
result.setStreamPosition("100");
result.setRecords(new ArrayList<IndexedRecord>());
result.setRequestId("REST::666");
result.setSuccess(true);
result.setErrors(Arrays.asList(new MarketoError("REST")));
assertEquals(emptyList(), result.getRecords());
assertEquals(100, result.getRecordCount());
assertEquals(100, result.getRemainCount());
assertEquals("100", result.getStreamPosition());
assertEquals("REST::666", result.getRequestId());
assertTrue(result.isSuccess());
assertNotNull(result.getErrors());
result = new MarketoRecordResult("200", 200, 200, null);
assertEquals(Collections.emptyList(), result.getRecords());
assertEquals(200, result.getRecordCount());
assertEquals(200, result.getRemainCount());
assertEquals("200", result.getStreamPosition());
result.setErrors(Arrays.asList(new MarketoError("REST", "101", "error"), new MarketoError("error2")));
assertNotNull(result.getErrorsString());
}
use of org.talend.components.marketo.runtime.client.type.MarketoError in project components by Talend.
the class MarketoInputReaderTest method testStart.
@Test
public void testStart() throws Exception {
MarketoRecordResult mkto = new MarketoRecordResult();
mkto.setSuccess(false);
mkto.setErrors(Arrays.asList(new MarketoError("REST", "error")));
when(client.bulkImport(any(TMarketoBulkExecProperties.class))).thenReturn(mkto);
when(client.getLead(any(TMarketoInputProperties.class), any(String.class))).thenReturn(mkto);
when(client.getMultipleLeads(any(TMarketoInputProperties.class), any(String.class))).thenReturn(mkto);
when(client.getLeadActivity(any(TMarketoInputProperties.class), any(String.class))).thenReturn(mkto);
when(client.getLeadChanges(any(TMarketoInputProperties.class), any(String.class))).thenReturn(mkto);
when(client.describeCustomObject(any(TMarketoInputProperties.class))).thenReturn(mkto);
when(client.listCustomObjects(any(TMarketoInputProperties.class))).thenReturn(mkto);
when(client.getCustomObjects(any(TMarketoInputProperties.class), any(String.class))).thenReturn(mkto);
try {
assertFalse(reader.start());
fail("Should not be here");
} catch (Exception e) {
}
IndexedRecord record = new GenericData.Record(MarketoConstants.getEmptySchema());
mkto.setSuccess(true);
mkto.setRecords(Arrays.asList(record));
when(client.bulkImport(any(TMarketoBulkExecProperties.class))).thenReturn(mkto);
when(client.getLead(any(TMarketoInputProperties.class), any(String.class))).thenReturn(mkto);
when(client.getMultipleLeads(any(TMarketoInputProperties.class), any(String.class))).thenReturn(mkto);
when(client.getLeadActivity(any(TMarketoInputProperties.class), any(String.class))).thenReturn(mkto);
when(client.getLeadChanges(any(TMarketoInputProperties.class), any(String.class))).thenReturn(mkto);
when(client.describeCustomObject(any(TMarketoInputProperties.class))).thenReturn(mkto);
when(client.listCustomObjects(any(TMarketoInputProperties.class))).thenReturn(mkto);
when(client.getCustomObjects(any(TMarketoInputProperties.class), any(String.class))).thenReturn(mkto);
assertFalse(reader.start());
}
use of org.talend.components.marketo.runtime.client.type.MarketoError 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.MarketoError in project components by Talend.
the class MarketoSOAPClient method getLead.
@Override
public MarketoRecordResult getLead(TMarketoInputProperties parameters, String offset) {
LOG.debug("MarketoSOAPClient.getLead with selector:{} key:{} value:{}.", parameters.leadSelectorSOAP.getValue(), parameters.leadKeyTypeSOAP.getValue(), parameters.leadKeyValue.getValue());
String leadKeyType = parameters.leadKeyTypeSOAP.getValue().toString();
String leadKeyValue = parameters.leadKeyValue.getValue();
Schema schema = parameters.schemaInput.schema.getValue();
Map<String, String> mappings = parameters.mappingInput.getNameMappingsForMarketo();
// Create Request
ParamsGetLead request = new ParamsGetLead();
LeadKey key = new LeadKey();
key.setKeyType(valueOf(leadKeyType));
key.setKeyValue(leadKeyValue);
request.setLeadKey(key);
//
SuccessGetLead result = null;
MarketoRecordResult mkto = new MarketoRecordResult();
try {
result = getPort().getLead(request, header);
} catch (Exception e) {
LOG.error("Lead not found : {}.", e.getMessage());
mkto.setSuccess(false);
mkto.setRecordCount(0);
mkto.setRemainCount(0);
mkto.setErrors(Collections.singletonList(new MarketoError(SOAP, e.getMessage())));
return mkto;
}
if (result == null || result.getResult().getCount() == 0) {
LOG.debug(MESSAGE_REQUEST_RETURNED_0_MATCHING_LEADS);
mkto.setErrors(Collections.singletonList(new MarketoError(SOAP, MESSAGE_NO_LEADS_FOUND)));
mkto.setSuccess(true);
} else {
int counted = result.getResult().getCount();
List<IndexedRecord> results = convertLeadRecords(result.getResult().getLeadRecordList().getValue().getLeadRecords(), schema, mappings);
mkto.setRecordCount(counted);
mkto.setRemainCount(0);
mkto.setSuccess(true);
mkto.setRecords(results);
}
return mkto;
}
use of org.talend.components.marketo.runtime.client.type.MarketoError in project components by Talend.
the class MarketoSOAPClient method listOperation.
public MarketoSyncResult listOperation(ListOperationType operationType, ListOperationParameters parameters) {
LOG.debug("listOperation : {}", parameters);
ParamsListOperation paramsListOperation = new ParamsListOperation();
paramsListOperation.setListOperation(operationType);
paramsListOperation.setStrict(objectFactory.createParamsListOperationStrict(parameters.getStrict()));
ListKey listKey = new ListKey();
listKey.setKeyValue(parameters.getListKeyValue());
listKey.setKeyType(ListKeyType.valueOf(parameters.getListKeyType()));
paramsListOperation.setListKey(listKey);
ArrayOfLeadKey leadKeys = new ArrayOfLeadKey();
for (String lkv : parameters.getLeadKeyValues()) {
LeadKey lk = new LeadKey();
lk.setKeyType(valueOf(parameters.getLeadKeyType()));
lk.setKeyValue(lkv);
leadKeys.getLeadKeies().add(lk);
}
paramsListOperation.setListMemberList(leadKeys);
MarketoSyncResult mkto = new MarketoSyncResult();
mkto.setRequestId(SOAP + "::" + operationType.name());
try {
SuccessListOperation result = getPort().listOperation(paramsListOperation, header);
if (LOG.isDebugEnabled()) {
try {
JAXBContext context = JAXBContext.newInstance(SuccessListOperation.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(result, System.out);
} catch (JAXBException e) {
LOG.error(e.getMessage());
}
}
mkto.setSuccess(true);
if (!result.getResult().getStatusList().isNil()) {
mkto.setRecordCount(result.getResult().getStatusList().getValue().getLeadStatuses().size());
List<LeadStatus> statuses = result.getResult().getStatusList().getValue().getLeadStatuses();
List<SyncStatus> resultStatus = new ArrayList<>();
for (LeadStatus status : statuses) {
SyncStatus sts = new SyncStatus(Integer.parseInt(status.getLeadKey().getKeyValue()), String.valueOf(status.isStatus()));
if (!status.isStatus() && !ISMEMBEROFLIST.equals(operationType)) {
Map<String, String> reason = new HashMap<>();
reason.put("code", "20103");
reason.put("message", "Lead Not Found");
sts.setReasons(Collections.singletonList(reason));
}
resultStatus.add(sts);
}
mkto.setRecords(resultStatus);
} else {
LOG.debug("No detail about successed operation, building one...");
String success = String.valueOf(result.getResult().isSuccess());
mkto.setRecordCount(parameters.getLeadKeyValue().size());
for (String leadk : parameters.getLeadKeyValue()) {
SyncStatus status = new SyncStatus(Integer.parseInt(leadk), success);
mkto.getRecords().add(status);
}
}
} catch (Exception e) {
LOG.error("[{}] error: {}", operationType.name(), e.getMessage());
mkto.setSuccess(false);
mkto.setErrors(Collections.singletonList(new MarketoError(SOAP, e.toString())));
}
return mkto;
}
Aggregations