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;
}
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;
}
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());
}
}
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;
}
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());
}
Aggregations