Search in sources :

Example 21 with MarketoException

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

the class MarketoLeadClient method getLeadChanges.

@Override
public MarketoRecordResult getLeadChanges(TMarketoInputProperties parameters, String offset) {
    String sinceDateTime = parameters.sinceDateTime.getValue();
    int batchLimit = parameters.batchSize.getValue() > REST_API_BATCH_LIMIT ? REST_API_BATCH_LIMIT : parameters.batchSize.getValue();
    String[] fields = parameters.fieldList.getValue().split(",");
    String pgOffset = offset;
    if (pgOffset == null) {
        try {
            pgOffset = getPageToken(sinceDateTime);
        } catch (MarketoException e) {
            LOG.error("getPageToken Error: `{}`.", e.toString());
            MarketoRecordResult mkto = new MarketoRecordResult();
            mkto.setSuccess(false);
            mkto.setErrors(Collections.singletonList(e.toMarketoError()));
            return mkto;
        }
    }
    current_uri = // 
    new StringBuilder(basicPath).append(// 
    API_PATH_ACTIVITIES_LEADCHANGES).append(fmtParams(FIELD_ACCESS_TOKEN, accessToken, true));
    if (!StringUtils.isEmpty(pgOffset)) {
        current_uri.append(fmtParams(FIELD_NEXT_PAGE_TOKEN, pgOffset));
    }
    current_uri.append(fmtParams(FIELD_BATCH_SIZE, batchLimit));
    if (fields != null && fields.length > 0) {
        current_uri.append(fmtParams(FIELD_FIELDS, csvString(fields)));
    }
    LOG.debug("Changes: {}.", current_uri);
    return getRecordResultForLead(parameters.inputOperation.getValue(), null, batchLimit, parameters.schemaInput.schema.getValue(), parameters.mappingInput.getNameMappingsForMarketo());
}
Also used : MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult)

Example 22 with MarketoException

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

the class MarketoSOAPClientTestIT method testConvertToLeadRecord.

/*
     * 
     * SyncLead Operations
     * 
     * 
     */
@Test
public void testConvertToLeadRecord() throws Exception {
    outProperties.outputOperation.setValue(OutputOperation.syncLead);
    outProperties.updateSchemaRelated();
    outProperties.afterOutputOperation();
    outProperties.beforeMappingInput();
    MarketoSOAPClient client = new MarketoSOAPClient(outProperties.connection).connect();
    IndexedRecord record = new GenericData.Record(outProperties.schemaInput.schema.getValue());
    record.put(0, 10);
    record.put(1, "undx@undx.net");
    LeadRecord lr = client.convertToLeadRecord(record, outProperties.mappingInput.getNameMappingsForMarketo());
    assertNotNull(lr);
    assertEquals(new Integer(10), lr.getId().getValue());
    assertEquals("undx@undx.net", lr.getEmail().getValue());
    // test attributes
    List<Field> fields = new ArrayList<>();
    Field field = new Schema.Field("FirstName", Schema.create(Schema.Type.STRING), null, (Object) null);
    fields.add(field);
    field = new Schema.Field("LastName", Schema.create(Schema.Type.STRING), null, (Object) null);
    fields.add(field);
    Schema s = MarketoUtils.newSchema(outProperties.schemaInput.schema.getValue(), "leadAttribute", fields);
    record = new GenericData.Record(s);
    record.put(0, 10);
    record.put(1, "undx@undx.net");
    record.put(2, "ForeignPersonSysId");
    // CUSTOM, SFDC, NETSUITE;
    record.put(3, "SFDC");
    record.put(5, "My firstName");
    record.put(6, "My lastName");
    outProperties.schemaInput.schema.setValue(s);
    outProperties.beforeMappingInput();
    lr = client.convertToLeadRecord(record, outProperties.mappingInput.getNameMappingsForMarketo());
    assertNotNull(lr);
    assertEquals(new Integer(10), lr.getId().getValue());
    assertEquals("undx@undx.net", lr.getEmail().getValue());
    assertEquals("ForeignPersonSysId", lr.getForeignSysPersonId().getValue());
    assertEquals("SFDC", lr.getForeignSysType().getValue().toString());
    assertEquals("My firstName", lr.getLeadAttributeList().getValue().getAttributes().get(0).getAttrValue().toString());
    assertEquals("My lastName", lr.getLeadAttributeList().getValue().getAttributes().get(1).getAttrValue().toString());
    // test mandatory field
    record.put(0, 10);
    record.put(1, null);
    record.put(2, null);
    lr = client.convertToLeadRecord(record, outProperties.mappingInput.getNameMappingsForMarketo());
    assertNotNull(lr);
    record.put(0, null);
    record.put(1, "toto@toto.org");
    record.put(2, null);
    lr = client.convertToLeadRecord(record, outProperties.mappingInput.getNameMappingsForMarketo());
    assertNotNull(lr);
    assertNotNull(lr);
    record.put(0, null);
    record.put(1, null);
    record.put(2, "SSDDSSDSD");
    lr = client.convertToLeadRecord(record, outProperties.mappingInput.getNameMappingsForMarketo());
    assertNotNull(lr);
    record.put(0, null);
    record.put(1, null);
    record.put(2, null);
    try {
        lr = client.convertToLeadRecord(record, outProperties.mappingInput.getNameMappingsForMarketo());
        assertNotNull(lr);
    } catch (MarketoException e) {
        assertEquals("syncLead error: Missing mandatory field for operation.", e.getMessage());
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) ArrayList(java.util.ArrayList) Field(org.apache.avro.Schema.Field) GenericData(org.apache.avro.generic.GenericData) Field(org.apache.avro.Schema.Field) LeadRecord(com.marketo.mktows.LeadRecord) IndexedRecord(org.apache.avro.generic.IndexedRecord) LeadRecord(com.marketo.mktows.LeadRecord) Test(org.junit.Test)

Example 23 with MarketoException

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

the class MarketoBaseRESTClient method executeGetRequest.

public RequestResult executeGetRequest(Class<?> resultClass) throws MarketoException {
    try {
        URL url = new URL(current_uri.toString());
        HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
        urlConn.setRequestMethod(QUERY_METHOD_GET);
        urlConn.setDoOutput(true);
        urlConn.setRequestProperty(REQUEST_PROPERTY_ACCEPT, REQUEST_VALUE_TEXT_JSON);
        int responseCode = urlConn.getResponseCode();
        if (responseCode == 200) {
            InputStream inStream = urlConn.getInputStream();
            Reader reader = new InputStreamReader(inStream);
            Gson gson = new Gson();
            return (RequestResult) gson.fromJson(reader, resultClass);
        } else {
            LOG.error("GET request failed: {}.", responseCode);
            throw new MarketoException(REST, responseCode, "Request failed! Please check your request setting!");
        }
    } catch (IOException e) {
        LOG.error("GET request failed: {}", e.getMessage());
        throw new MarketoException(REST, e.getMessage());
    }
}
Also used : RequestResult(org.talend.components.marketo.runtime.client.rest.response.RequestResult) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 24 with MarketoException

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

the class MarketoBaseRESTClient method getSyncResultFromRequest.

/**
 * Execute POST or GET request and feed the MarketoSyncResult to return
 *
 * @param paramPOSTJson
 * @return
 */
public MarketoSyncResult getSyncResultFromRequest(boolean sendPOST, JsonObject paramPOSTJson) {
    MarketoSyncResult mkto = new MarketoSyncResult();
    try {
        SyncResult rs;
        if (sendPOST) {
            rs = (SyncResult) executePostRequest(SyncResult.class, paramPOSTJson);
        } else {
            rs = (SyncResult) executeGetRequest(SyncResult.class);
        }
        // 
        mkto.setRequestId(REST + "::" + rs.getRequestId());
        mkto.setStreamPosition(rs.getNextPageToken());
        mkto.setSuccess(rs.isSuccess());
        if (mkto.isSuccess()) {
            mkto.setRecordCount(rs.getResult().size());
            mkto.setRemainCount(0);
            mkto.setRecords(rs.getResult());
            if (rs.isMoreResult()) {
                // cannot know how many remain...
                mkto.setRemainCount(mkto.getRecordCount());
                mkto.setStreamPosition(rs.getNextPageToken());
            }
        // 
        } else {
            mkto.setRecordCount(0);
            mkto.setErrors(rs.getErrors());
        }
        LOG.debug("rs = {}.", rs);
    } catch (MarketoException e) {
        mkto.setSuccess(false);
        mkto.setErrors(Collections.singletonList(e.toMarketoError()));
    }
    return mkto;
}
Also used : MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) SyncResult(org.talend.components.marketo.runtime.client.rest.response.SyncResult) MarketoSyncResult(org.talend.components.marketo.runtime.client.type.MarketoSyncResult) MarketoSyncResult(org.talend.components.marketo.runtime.client.type.MarketoSyncResult)

Example 25 with MarketoException

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

the class MarketoBaseRESTClient method executePostRequest.

public MarketoRecordResult executePostRequest(JsonObject inputJson) throws MarketoException {
    try {
        URL url = new URL(current_uri.toString());
        HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
        urlConn.setRequestMethod("POST");
        // "application/json"
        urlConn.setRequestProperty(REQUEST_PROPERTY_CONTENT_TYPE, REQUEST_VALUE_APPLICATION_JSON);
        // content-type is
        // required.
        urlConn.setRequestProperty(REQUEST_PROPERTY_ACCEPT, REQUEST_VALUE_TEXT_JSON);
        urlConn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(urlConn.getOutputStream());
        wr.write(inputJson.toString());
        wr.flush();
        wr.close();
        int responseCode = urlConn.getResponseCode();
        if (responseCode == 200) {
            InputStream inStream = urlConn.getInputStream();
            InputStreamReader reader = new InputStreamReader(inStream);
            Gson gson = new Gson();
            LinkedTreeMap ltm = (LinkedTreeMap) gson.fromJson(reader, Object.class);
            MarketoRecordResult mkr = new MarketoRecordResult();
            mkr.setRequestId(REST + "::" + ltm.get("requestId"));
            mkr.setSuccess(Boolean.parseBoolean(ltm.get("success").toString()));
            return mkr;
        } else {
            LOG.error("POST request failed: {}", responseCode);
            throw new MarketoException(REST, responseCode, "Request failed! Please check your request setting!");
        }
    } catch (IOException e) {
        LOG.error("GET request failed: {}", e.getMessage());
        throw new MarketoException(REST, e.getMessage());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) InputStream(java.io.InputStream) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) Gson(com.google.gson.Gson) OutputStreamWriter(java.io.OutputStreamWriter) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult)

Aggregations

MarketoException (org.talend.components.marketo.runtime.client.type.MarketoException)37 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)12 JsonObject (com.google.gson.JsonObject)11 MarketoRecordResult (org.talend.components.marketo.runtime.client.type.MarketoRecordResult)10 URL (java.net.URL)9 IndexedRecord (org.apache.avro.generic.IndexedRecord)9 IOException (java.io.IOException)8 InputStream (java.io.InputStream)8 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)8 InputStreamReader (java.io.InputStreamReader)7 Schema (org.apache.avro.Schema)7 Field (org.apache.avro.Schema.Field)7 Gson (com.google.gson.Gson)6 SyncResult (org.talend.components.marketo.runtime.client.rest.response.SyncResult)6 Record (org.apache.avro.generic.GenericData.Record)5 MarketoError (org.talend.components.marketo.runtime.client.type.MarketoError)5 OutputStreamWriter (java.io.OutputStreamWriter)4 LinkedTreeMap (com.google.gson.internal.LinkedTreeMap)3 Reader (java.io.Reader)3