Search in sources :

Example 6 with MarketoError

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());
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError) Test(org.junit.Test)

Example 7 with MarketoError

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());
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) TMarketoInputProperties(org.talend.components.marketo.tmarketoinput.TMarketoInputProperties) IndexedRecord(org.apache.avro.generic.IndexedRecord) TMarketoBulkExecProperties(org.talend.components.marketo.tmarketobulkexec.TMarketoBulkExecProperties) IOException(java.io.IOException) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError) Test(org.junit.Test)

Example 8 with MarketoError

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;
}
Also used : BulkImport(org.talend.components.marketo.runtime.client.rest.type.BulkImport) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) BulkImportResult(org.talend.components.marketo.runtime.client.rest.response.BulkImportResult) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError)

Example 9 with MarketoError

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;
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) SuccessGetLead(com.marketo.mktows.SuccessGetLead) ArrayOfString(com.marketo.mktows.ArrayOfString) LeadKey(com.marketo.mktows.LeadKey) ArrayOfLeadKey(com.marketo.mktows.ArrayOfLeadKey) ParamsGetLead(com.marketo.mktows.ParamsGetLead) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) WebServiceException(javax.xml.ws.WebServiceException) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) ParseException(java.text.ParseException) JAXBException(javax.xml.bind.JAXBException) ComponentException(org.talend.components.api.exception.ComponentException) MalformedURLException(java.net.MalformedURLException) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError)

Example 10 with MarketoError

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;
}
Also used : ArrayOfLeadKey(com.marketo.mktows.ArrayOfLeadKey) SuccessListOperation(com.marketo.mktows.SuccessListOperation) Marshaller(javax.xml.bind.Marshaller) HashMap(java.util.HashMap) JAXBException(javax.xml.bind.JAXBException) SyncStatus(org.talend.components.marketo.runtime.client.rest.type.SyncStatus) ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext) ArrayOfString(com.marketo.mktows.ArrayOfString) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) WebServiceException(javax.xml.ws.WebServiceException) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) ParseException(java.text.ParseException) JAXBException(javax.xml.bind.JAXBException) ComponentException(org.talend.components.api.exception.ComponentException) MalformedURLException(java.net.MalformedURLException) MarketoSyncResult(org.talend.components.marketo.runtime.client.type.MarketoSyncResult) LeadStatus(com.marketo.mktows.LeadStatus) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError) ListKey(com.marketo.mktows.ListKey) ParamsListOperation(com.marketo.mktows.ParamsListOperation) LeadKey(com.marketo.mktows.LeadKey) ArrayOfLeadKey(com.marketo.mktows.ArrayOfLeadKey)

Aggregations

MarketoError (org.talend.components.marketo.runtime.client.type.MarketoError)27 MarketoRecordResult (org.talend.components.marketo.runtime.client.type.MarketoRecordResult)16 Test (org.junit.Test)12 MarketoException (org.talend.components.marketo.runtime.client.type.MarketoException)12 IndexedRecord (org.apache.avro.generic.IndexedRecord)8 MalformedURLException (java.net.MalformedURLException)7 InvalidKeyException (java.security.InvalidKeyException)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 ParseException (java.text.ParseException)7 ArrayList (java.util.ArrayList)7 JAXBException (javax.xml.bind.JAXBException)7 DatatypeConfigurationException (javax.xml.datatype.DatatypeConfigurationException)7 WebServiceException (javax.xml.ws.WebServiceException)7 ComponentException (org.talend.components.api.exception.ComponentException)7 MarketoSyncResult (org.talend.components.marketo.runtime.client.type.MarketoSyncResult)6 ArrayOfString (com.marketo.mktows.ArrayOfString)5 List (java.util.List)4 Schema (org.apache.avro.Schema)4 SyncStatus (org.talend.components.marketo.runtime.client.rest.type.SyncStatus)4 TMarketoBulkExecProperties (org.talend.components.marketo.tmarketobulkexec.TMarketoBulkExecProperties)4