Search in sources :

Example 16 with MarketoException

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

the class MarketoSOAPClient method convertToLeadRecord.

/*
     * 
     * SyncLeads operations
     * 
     */
public LeadRecord convertToLeadRecord(IndexedRecord record, Map<String, String> mappings) throws MarketoException {
    // first, check if a mandatory field is in the schema
    Boolean ok = Boolean.FALSE;
    for (Entry<String, String> e : mappings.entrySet()) {
        ok |= (e.getKey().equals(FIELD_ID) || e.getKey().equals(FIELD_EMAIL) || e.getKey().equals(FIELD_FOREIGN_SYS_PERSON_ID) || e.getValue().equals(FIELD_ID) || e.getValue().equals(FIELD_EMAIL) || e.getValue().equals(FIELD_FOREIGN_SYS_PERSON_ID)) && record.get(record.getSchema().getField(e.getKey()).pos()) != null;
    }
    if (!ok) {
        MarketoException err = new MarketoException("SOAP", "syncLead error: Missing mandatory field for operation.");
        LOG.error(err.toString());
        throw err;
    }
    // 
    LeadRecord lead = new LeadRecord();
    ArrayOfAttribute aoa = new ArrayOfAttribute();
    for (Field f : record.getSchema().getFields()) {
        // find matching marketo column name
        String col = mappings.get(f.name());
        if (col.equals(FIELD_ID)) {
            final Integer id = (Integer) record.get(f.pos());
            if (id != null) {
                lead.setId(objectFactory.createLeadRecordId(id));
            }
        } else if (col.equals(FIELD_EMAIL)) {
            final String email = (String) record.get(f.pos());
            if (email != null) {
                lead.setEmail(objectFactory.createLeadRecordEmail(email));
            }
        } else if (col.equals(FIELD_FOREIGN_SYS_PERSON_ID)) {
            final String fspid = (String) record.get(f.pos());
            if (fspid != null) {
                lead.setForeignSysPersonId(objectFactory.createLeadRecordForeignSysPersonId(fspid));
            }
        } else if (col.equals(FIELD_FOREIGN_SYS_TYPE)) {
            final String fst = (String) record.get(f.pos());
            if (fst != null) {
                lead.setForeignSysType(objectFactory.createLeadRecordForeignSysType(ForeignSysType.valueOf(fst)));
            }
        } else {
            // skip status & error fields
            if (FIELD_STATUS.equals(col) || FIELD_ERROR_MSG.equals(col)) {
                continue;
            }
            Attribute attr = new Attribute();
            Object value = record.get(f.pos());
            attr.setAttrName(col);
            if (MarketoClientUtils.isDateTypeField(f) && value != null) {
                attr.setAttrValue(MarketoClientUtils.formatLongToDateString(Long.valueOf(String.valueOf(value))));
            } else {
                attr.setAttrValue(String.valueOf(value));
            }
            aoa.getAttributes().add(attr);
        }
    }
    QName qname = new QName("http://www.marketo.com/mktows/", "leadAttributeList");
    JAXBElement<ArrayOfAttribute> attrList = new JAXBElement(qname, ArrayOfAttribute.class, aoa);
    lead.setLeadAttributeList(attrList);
    return lead;
}
Also used : ArrayOfAttribute(com.marketo.mktows.ArrayOfAttribute) ArrayOfAttribute(com.marketo.mktows.ArrayOfAttribute) Attribute(com.marketo.mktows.Attribute) QName(javax.xml.namespace.QName) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) ArrayOfString(com.marketo.mktows.ArrayOfString) JAXBElement(javax.xml.bind.JAXBElement) Field(org.apache.avro.Schema.Field) ArrayOfLeadRecord(com.marketo.mktows.ArrayOfLeadRecord) LeadRecord(com.marketo.mktows.LeadRecord)

Example 17 with MarketoException

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

the class MarketoSOAPClient method connect.

public MarketoSOAPClient connect() throws MarketoException {
    try {
        port = getMktowsApiSoapPort();
        LOG.debug("Marketo SOAP Client :: port.");
        header = getAuthentificationHeader();
        LOG.debug("Marketo SOAP Client initialization :: AuthHeader.");
        // bug/TDI-38439_MarketoWizardConnection : make a dummy call to check auth and not just URL.
        getPort().listMObjects(new ParamsListMObjects(), header);
    } catch (MalformedURLException | NoSuchAlgorithmException | InvalidKeyException | WebServiceException e) {
        throw new MarketoException(SOAP, e.getMessage());
    }
    return this;
}
Also used : MalformedURLException(java.net.MalformedURLException) WebServiceException(javax.xml.ws.WebServiceException) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ParamsListMObjects(com.marketo.mktows.ParamsListMObjects) InvalidKeyException(java.security.InvalidKeyException)

Example 18 with MarketoException

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

the class MarketoBulkExecClient method executePostFileRequest.

public BulkImportResult executePostFileRequest(Class<?> resultClass, String filePath) throws MarketoException {
    String boundary = "Talend_tMarketoBulkExec_" + String.valueOf(System.currentTimeMillis());
    try {
        URL url = new URL(current_uri.toString());
        HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
        urlConn.setRequestMethod("POST");
        urlConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        urlConn.setRequestProperty("accept", "text/json");
        urlConn.setDoOutput(true);
        // build the request body
        String requestBody = buildRequest(filePath);
        PrintWriter wr = new PrintWriter(new OutputStreamWriter(urlConn.getOutputStream()));
        wr.append("--" + boundary + "\r\n");
        wr.append("Content-Disposition: form-data; name=\"file\";filename=\"" + filePath + "\";\r\n");
        wr.append("Content-type: text/plain; charset=\"utf-8\"\r\n");
        wr.append("Content-Transfer-Encoding: text/plain\r\n");
        wr.append("MIME-Version: 1.0\r\n");
        wr.append("\r\n");
        wr.append(requestBody);
        wr.append("\r\n");
        wr.append("--" + boundary);
        wr.flush();
        wr.close();
        int responseCode = urlConn.getResponseCode();
        if (responseCode == 200) {
            InputStream inStream = urlConn.getInputStream();
            InputStreamReader reader = new InputStreamReader(inStream);
            Gson gson = new Gson();
            return (BulkImportResult) gson.fromJson(reader, resultClass);
        } else {
            LOG.error("POST request failed: {}", responseCode);
            throw new MarketoException(REST, responseCode, "Request failed! Please check your request setting!");
        }
    } catch (IOException e) {
        LOG.error("POST request failed: {}", e.getMessage());
        throw new MarketoException(REST, e.getMessage());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) Gson(com.google.gson.Gson) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BulkImportResult(org.talend.components.marketo.runtime.client.rest.response.BulkImportResult) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) PrintWriter(java.io.PrintWriter)

Example 19 with MarketoException

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

the class MarketoLeadClient method getAllLeadFields.

/*
     *
     * management func
     *
     */
public List<Schema.Field> getAllLeadFields() {
    current_uri = // 
    new StringBuilder(basicPath).append(// 
    "/v1/leads").append(// 
    API_PATH_URI_DESCRIBE).append(fmtParams(FIELD_ACCESS_TOKEN, accessToken, true));
    List<Schema.Field> fields = new ArrayList<>();
    try {
        LOG.debug("describeLead {}.", current_uri);
        DescribeFieldsResult rs = (DescribeFieldsResult) executeGetRequest(DescribeFieldsResult.class);
        if (!rs.isSuccess()) {
            return fields;
        }
        // 
        for (FieldDescription d : rs.getResult()) {
            fields.add(d.toAvroField());
        }
    } catch (MarketoException e) {
        LOG.error("describeLeadFields error: {}.", e.toString());
    }
    return fields;
}
Also used : Field(org.apache.avro.Schema.Field) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) ArrayList(java.util.ArrayList) DescribeFieldsResult(org.talend.components.marketo.runtime.client.rest.response.DescribeFieldsResult) FieldDescription(org.talend.components.marketo.runtime.client.rest.type.FieldDescription)

Example 20 with MarketoException

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

the class MarketoLeadClient method getLeadActivity.

@Override
public MarketoRecordResult getLeadActivity(TMarketoInputProperties parameters, String offset) {
    String sinceDateTime = parameters.sinceDateTime.getValue();
    List<String> incs = parameters.includeTypes.type.getValue();
    List<String> excs = parameters.setExcludeTypes.getValue() ? parameters.excludeTypes.type.getValue() : new ArrayList<String>();
    int batchLimit = parameters.batchSize.getValue() > REST_API_BATCH_LIMIT ? REST_API_BATCH_LIMIT : parameters.batchSize.getValue();
    String pgOffset = offset;
    List<Integer> activityTypeIds = new ArrayList<>();
    // no activity provided, we take all
    if (incs.isEmpty()) {
        int limit = 0;
        LOG.warn("No ActivityTypeId provided! Getting 10 first availables (API limit).");
        for (Object s : parameters.includeTypes.type.getPossibleValues()) {
            incs.add(s.toString());
            limit++;
            if (limit == REST_API_ACTIVITY_TYPE_IDS_LIMIT) {
                break;
            }
        }
    }
    // translate into ids
    for (String i : incs) {
        activityTypeIds.add(IncludeExcludeFieldsREST.valueOf(i).fieldVal);
    }
    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;
        }
    }
    // Marketo API in SOAP and REST return a false estimation of remainCount. Watch out !!!
    current_uri = // 
    new StringBuilder(basicPath).append(// 
    API_PATH_ACTIVITIES).append(fmtParams(FIELD_ACCESS_TOKEN, accessToken, true));
    if (!StringUtils.isEmpty(pgOffset)) {
        current_uri.append(fmtParams(FIELD_NEXT_PAGE_TOKEN, pgOffset));
    }
    if (activityTypeIds != null) {
        current_uri.append(fmtParams(FIELD_ACTIVITY_TYPE_IDS, csvString(activityTypeIds.toArray())));
    }
    current_uri.append(fmtParams(FIELD_BATCH_SIZE, batchLimit));
    LOG.debug("Activities: {}.", 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) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) 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