Search in sources :

Example 16 with MarketoError

use of org.talend.components.marketo.runtime.client.type.MarketoError in project components by Talend.

the class MarketoSOAPClient method getMultipleLeads.

/**
 * In getMultipleLeadsJSON you have to add includeAttributes base fields like Email. Otherwise, they return null from
 * API. WTF ?!? It's like that...
 */
@Override
public MarketoRecordResult getMultipleLeads(TMarketoInputProperties parameters, String offset) {
    LOG.debug("MarketoSOAPClient.getMultipleLeadsJSON with {}", parameters.leadSelectorSOAP.getValue());
    Schema schema = parameters.schemaInput.schema.getValue();
    Map<String, String> mappings = parameters.mappingInput.getNameMappingsForMarketo();
    int bSize = parameters.batchSize.getValue();
    // 
    // Create Request
    // 
    ParamsGetMultipleLeads request = new ParamsGetMultipleLeads();
    // 
    if (parameters.leadSelectorSOAP.getValue().equals(LeadKeySelector)) {
        LOG.info("LeadKeySelector - Key type:  {} with value : {}.", parameters.leadKeyTypeSOAP.getValue().toString(), parameters.leadKeyValues.getValue());
        LeadKeySelector keySelector = new LeadKeySelector();
        keySelector.setKeyType(valueOf(parameters.leadKeyTypeSOAP.getValue().toString()));
        ArrayOfString aos = new ArrayOfString();
        String[] keys = parameters.leadKeyValues.getValue().split("(,|;|\\s)");
        for (String s : keys) {
            LOG.debug("Adding leadKeyValue : {}.", s);
            aos.getStringItems().add(s);
        }
        keySelector.setKeyValues(aos);
        request.setLeadSelector(keySelector);
    } else if (parameters.leadSelectorSOAP.getValue().equals(LastUpdateAtSelector)) {
        LOG.debug("LastUpdateAtSelector - since {} to  {}.", parameters.oldestUpdateDate.getValue(), parameters.latestUpdateDate.getValue());
        LastUpdateAtSelector leadSelector = new LastUpdateAtSelector();
        try {
            DatatypeFactory factory = newInstance();
            Date oldest = MarketoUtils.parseDateString(parameters.oldestUpdateDate.getValue());
            Date latest = MarketoUtils.parseDateString(parameters.latestUpdateDate.getValue());
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(latest);
            JAXBElement<XMLGregorianCalendar> until = objectFactory.createLastUpdateAtSelectorLatestUpdatedAt(factory.newXMLGregorianCalendar(gc));
            GregorianCalendar since = new GregorianCalendar();
            since.setTime(oldest);
            leadSelector.setOldestUpdatedAt(factory.newXMLGregorianCalendar(since));
            leadSelector.setLatestUpdatedAt(until);
            request.setLeadSelector(leadSelector);
        } catch (ParseException | DatatypeConfigurationException e) {
            LOG.error("Error for LastUpdateAtSelector : {}.", e.getMessage());
            throw new ComponentException(e);
        }
    } else // 
    if (parameters.leadSelectorSOAP.getValue().equals(StaticListSelector)) {
        LOG.info("StaticListSelector - List type : {} with value : {}.", parameters.listParam.getValue(), parameters.listParamListName.getValue());
        StaticListSelector staticListSelector = new StaticListSelector();
        if (parameters.listParam.getValue().equals(STATIC_LIST_NAME)) {
            JAXBElement<String> listName = objectFactory.createStaticListSelectorStaticListName(parameters.listParamListName.getValue());
            staticListSelector.setStaticListName(listName);
        } else {
            // you can find listId by examining the URL : https://app-abq.marketo.com/#ST29912B2
            // #ST29912B2 :
            // #ST -> Static list identifier
            // 29912 -> our list FIELD_ID !
            // B2 -> tab in the UI
            JAXBElement<Integer> listId = objectFactory.createStaticListSelectorStaticListId(// 
            parameters.listParamListId.getValue());
            staticListSelector.setStaticListId(listId);
        }
        request.setLeadSelector(staticListSelector);
    } else {
        // Duh !
        LOG.error("Unknown LeadSelector : {}.", parameters.leadSelectorSOAP.getValue());
        throw new ComponentException(new Exception("Incorrect parameter value for LeadSelector : " + parameters.leadSelectorSOAP.getValue()));
    }
    // attributes
    // curiously we have to put some basic fields like Email in attributes if we have them feed...
    ArrayOfString attributes = new ArrayOfString();
    for (String s : mappings.values()) {
        attributes.getStringItems().add(s);
    }
    attributes.getStringItems().add("Company");
    request.setIncludeAttributes(attributes);
    // batchSize : another curious behavior... Don't seem to work properly with leadKeySelector...
    // nevertheless, the server automatically adjust batch size according request.
    JAXBElement<Integer> batchSize = new ObjectFactory().createParamsGetMultipleLeadsBatchSize(bSize);
    request.setBatchSize(batchSize);
    // stream position
    if (offset != null && !offset.isEmpty()) {
        request.setStreamPosition(new ObjectFactory().createParamsGetMultipleLeadsStreamPosition(offset));
    }
    // 
    // 
    // Request execution
    // 
    SuccessGetMultipleLeads result = null;
    MarketoRecordResult mkto = new MarketoRecordResult();
    try {
        result = getPort().getMultipleLeads(request, header);
    } catch (Exception e) {
        LOG.error("Lead not found : {}.", e.getMessage());
        mkto.setSuccess(false);
        mkto.setErrors(Collections.singletonList(new MarketoError(SOAP, e.getMessage())));
        return mkto;
    }
    if (result == null || result.getResult().getReturnCount() == 0) {
        LOG.debug(MESSAGE_REQUEST_RETURNED_0_MATCHING_LEADS);
        mkto.setSuccess(true);
        mkto.setErrors(Collections.singletonList(new MarketoError(SOAP, MESSAGE_NO_LEADS_FOUND)));
        mkto.setRecordCount(0);
        mkto.setRemainCount(0);
        return mkto;
    } else {
        String streamPos = result.getResult().getNewStreamPosition();
        int recordCount = result.getResult().getReturnCount();
        int remainCount = result.getResult().getRemainingCount();
        // Process results
        List<IndexedRecord> results = convertLeadRecords(result.getResult().getLeadRecordList().getValue().getLeadRecords(), schema, mappings);
        return new MarketoRecordResult(true, streamPos, recordCount, remainCount, results);
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ArrayOfString(com.marketo.mktows.ArrayOfString) StaticListSelector(com.marketo.mktows.StaticListSelector) StaticListSelector(org.talend.components.marketo.tmarketoinput.TMarketoInputProperties.LeadSelector.StaticListSelector) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError) ObjectFactory(com.marketo.mktows.ObjectFactory) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult) LeadKeySelector(org.talend.components.marketo.tmarketoinput.TMarketoInputProperties.LeadSelector.LeadKeySelector) LeadKeySelector(com.marketo.mktows.LeadKeySelector) LastUpdateAtSelector(com.marketo.mktows.LastUpdateAtSelector) LastUpdateAtSelector(org.talend.components.marketo.tmarketoinput.TMarketoInputProperties.LeadSelector.LastUpdateAtSelector) DatatypeFactory(javax.xml.datatype.DatatypeFactory) GregorianCalendar(java.util.GregorianCalendar) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) JAXBElement(javax.xml.bind.JAXBElement) ParamsGetMultipleLeads(com.marketo.mktows.ParamsGetMultipleLeads) Date(java.util.Date) 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) ArrayOfString(com.marketo.mktows.ArrayOfString) ComponentException(org.talend.components.api.exception.ComponentException) SuccessGetMultipleLeads(com.marketo.mktows.SuccessGetMultipleLeads)

Example 17 with MarketoError

use of org.talend.components.marketo.runtime.client.type.MarketoError in project components by Talend.

the class MarketoSOAPClient method syncMultipleLeads.

@Override
public MarketoSyncResult syncMultipleLeads(TMarketoOutputProperties parameters, List<IndexedRecord> leads) {
    MarketoSyncResult mkto = new MarketoSyncResult();
    try {
        ParamsSyncMultipleLeads request = new ParamsSyncMultipleLeads();
        ArrayOfLeadRecord leadRecords = new ArrayOfLeadRecord();
        for (IndexedRecord r : leads) {
            leadRecords.getLeadRecords().add(convertToLeadRecord(r, parameters.mappingInput.getNameMappingsForMarketo()));
        }
        JAXBElement<Boolean> dedup = objectFactory.createParamsSyncMultipleLeadsDedupEnabled(parameters.deDupeEnabled.getValue());
        request.setDedupEnabled(dedup);
        request.setLeadRecordList(leadRecords);
        SuccessSyncMultipleLeads result = getPort().syncMultipleLeads(request, header);
        // 
        if (LOG.isDebugEnabled()) {
            try {
                JAXBContext context = JAXBContext.newInstance(SuccessSyncLead.class);
                Marshaller m = context.createMarshaller();
                m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                m.marshal(result, System.out);
            } catch (JAXBException e) {
                LOG.error(e.getMessage());
            }
        }
        // 
        List<SyncStatus> records = new ArrayList<>();
        for (com.marketo.mktows.SyncStatus status : result.getResult().getSyncStatusList().getSyncStatuses()) {
            SyncStatus s = new SyncStatus(status.getLeadId(), status.getStatus().value());
            s.setErrorMessage(status.getError().getValue());
            records.add(s);
        }
        mkto.setSuccess(result.getResult().getSyncStatusList() != null);
        mkto.setRecords(records);
    } catch (Exception e) {
        LOG.error(e.toString());
        mkto.setSuccess(false);
        mkto.setErrors(Collections.singletonList(new MarketoError(SOAP, e.getMessage())));
    }
    return mkto;
}
Also used : Marshaller(javax.xml.bind.Marshaller) IndexedRecord(org.apache.avro.generic.IndexedRecord) JAXBException(javax.xml.bind.JAXBException) SyncStatus(org.talend.components.marketo.runtime.client.rest.type.SyncStatus) ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext) ParamsSyncMultipleLeads(com.marketo.mktows.ParamsSyncMultipleLeads) 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) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError) ArrayOfLeadRecord(com.marketo.mktows.ArrayOfLeadRecord) SuccessSyncMultipleLeads(com.marketo.mktows.SuccessSyncMultipleLeads)

Example 18 with MarketoError

use of org.talend.components.marketo.runtime.client.type.MarketoError in project components by Talend.

the class MarketoRESTClientTestIT method testIsAccessTokenExpired.

@Test
public void testIsAccessTokenExpired() throws Exception {
    MarketoSource source = new MarketoSource();
    source.initialize(null, iprops);
    MarketoClientService client = source.getClientService(null);
    assertFalse(((MarketoRESTClient) client).isAccessTokenExpired(null));
    MarketoError err = new MarketoError("REST", "200", "dfddf");
    assertFalse(((MarketoRESTClient) client).isAccessTokenExpired(Arrays.asList(err)));
    err.setCode("602");
    assertTrue(((MarketoRESTClient) client).isAccessTokenExpired(Arrays.asList(err)));
}
Also used : MarketoSource(org.talend.components.marketo.runtime.MarketoSource) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError) Test(org.junit.Test)

Example 19 with MarketoError

use of org.talend.components.marketo.runtime.client.type.MarketoError in project components by Talend.

the class MarketoClientCustomObjectsTestIT method testGetCustomObjectsFail.

@Test
public void testGetCustomObjectsFail() throws Exception {
    MarketoSource source = new MarketoSource();
    source.initialize(null, irProps);
    MarketoRESTClient client = (MarketoRESTClient) source.getClientService(null);
    irProps.customObjectName.setValue(TEST_CO_NAME_SMARTPHONE);
    // cannot search by brand, must be a dedupe
    irProps.customObjectFilterType.setValue(FIELD_CO_SMARTPHONE_BRAND);
    // field.
    irProps.customObjectFilterValues.setValue(TEST_SMARTPHONE_BRAND_SAMSUNG);
    irProps.batchSize.setValue(500);
    irProps.schemaInput.schema.setValue(MarketoConstants.getCustomObjectRecordSchema());
    MarketoRecordResult result = client.getCustomObjects(irProps, null);
    assertFalse(result.isSuccess());
    assertEquals(0, result.getRecordCount());
    assertNotNull(result.getErrors());
    Object err = result.getErrors().get(0);
    assertTrue(err instanceof MarketoError);
    assertEquals("REST", ((MarketoError) err).getApiMode());
    assertEquals("1003", ((MarketoError) err).getCode());
    assertEquals("Invalid filterType 'brand'", ((MarketoError) err).getMessage());
}
Also used : MarketoSource(org.talend.components.marketo.runtime.MarketoSource) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError) Test(org.junit.Test)

Example 20 with MarketoError

use of org.talend.components.marketo.runtime.client.type.MarketoError in project components by Talend.

the class MarketoBaseRESTClient method executeFakeGetRequest.

public MarketoRecordResult executeFakeGetRequest(Schema schema, String input) throws MarketoException {
    InputStreamReader reader = httpFakeGet(input, false);
    // TODO refactor this part with method executeGetRequest(Schema s);
    Gson gson = new Gson();
    MarketoRecordResult mkr = new MarketoRecordResult();
    LinkedTreeMap ltm = (LinkedTreeMap) gson.fromJson(reader, Object.class);
    LOG.debug("ltm = {}.", ltm);
    mkr.setRequestId(REST + "::" + ltm.get("requestId"));
    mkr.setSuccess(Boolean.parseBoolean(ltm.get("success").toString()));
    mkr.setStreamPosition((String) ltm.get(FIELD_NEXT_PAGE_TOKEN));
    if (!mkr.isSuccess() && ltm.get(FIELD_ERRORS) != null) {
        List<LinkedTreeMap> errors = (List<LinkedTreeMap>) ltm.get(FIELD_ERRORS);
        for (LinkedTreeMap err : errors) {
            MarketoError error = new MarketoError(REST, (String) err.get("code"), (String) err.get("message"));
            mkr.setErrors(Collections.singletonList(error));
        }
    }
    if (mkr.isSuccess()) {
        List<LinkedTreeMap> tmp = (List<LinkedTreeMap>) ltm.get("result");
        if (tmp != null) {
            mkr.setRecordCount(tmp.size());
            mkr.setRecords(parseRecords(tmp, schema));
        }
        if (mkr.getStreamPosition() != null) {
            mkr.setRemainCount(mkr.getRecordCount());
        }
    }
    return mkr;
}
Also used : InputStreamReader(java.io.InputStreamReader) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) ArrayList(java.util.ArrayList) List(java.util.List) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError)

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