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