use of org.talend.components.marketo.runtime.client.type.MarketoException in project components by Talend.
the class MarketoCustomObjectClientTest method testGetCustomObjects.
@Test
public void testGetCustomObjects() throws Exception {
iprops.customObjectAction.setValue(CustomObjectAction.get);
doThrow(new MarketoException("REST", "error")).when(client).executeGetRequest(any(Schema.class));
mktoRR = client.getCustomObjects(iprops, null);
assertFalse(mktoRR.isSuccess());
assertFalse(mktoRR.getErrorsString().isEmpty());
//
doReturn(new MarketoRecordResult()).when(client).executeGetRequest(any(Schema.class));
mktoRR = client.getCustomObjects(iprops, null);
assertFalse(mktoRR.isSuccess());
assertTrue(mktoRR.getErrorsString().isEmpty());
//
MarketoRecordResult mrr = new MarketoRecordResult();
mrr.setSuccess(true);
mrr.setRemainCount(0);
mrr.setRecordCount(1);
List<IndexedRecord> records = new ArrayList<>();
IndexedRecord record = new Record(MarketoConstants.getCustomObjectRecordSchema());
record.put(0, "mkto-123456");
record.put(1, 0);
record.put(2, new Date());
record.put(3, new Date());
records.add(record);
mrr.setRecords(records);
doReturn(mrr).when(client).executeGetRequest(any(Schema.class));
mktoRR = client.getCustomObjects(iprops, null);
assertTrue(mktoRR.isSuccess());
assertTrue(mktoRR.getErrorsString().isEmpty());
}
use of org.talend.components.marketo.runtime.client.type.MarketoException in project components by Talend.
the class MarketoCustomObjectClientTest method testDescribeCustomObject.
@Test
public void testDescribeCustomObject() throws Exception {
iprops.customObjectAction.setValue(CustomObjectAction.describe);
//
doThrow(new MarketoException("REST", "error")).when(client).executeGetRequest(any(Schema.class));
mktoRR = client.describeCustomObject(iprops);
assertFalse(mktoRR.isSuccess());
assertFalse(mktoRR.getErrorsString().isEmpty());
//
doReturn(new MarketoRecordResult()).when(client).executeGetRequest(any(Schema.class));
mktoRR = client.describeCustomObject(iprops);
assertFalse(mktoRR.isSuccess());
assertTrue(mktoRR.getErrorsString().isEmpty());
//
doReturn(getCustomObjectResult()).when(client).executeGetRequest(any(Schema.class));
mktoRR = client.describeCustomObject(iprops);
assertTrue(mktoRR.isSuccess());
assertTrue(mktoRR.getErrorsString().isEmpty());
}
use of org.talend.components.marketo.runtime.client.type.MarketoException in project components by Talend.
the class MarketoCustomObjectClientTest method testListCustomObjects.
@Test
public void testListCustomObjects() throws Exception {
iprops.customObjectAction.setValue(CustomObjectAction.list);
doThrow(new MarketoException("REST", "error")).when(client).executeGetRequest(any(Schema.class));
mktoRR = client.listCustomObjects(iprops);
assertFalse(mktoRR.isSuccess());
assertFalse(mktoRR.getErrorsString().isEmpty());
//
doReturn(new MarketoRecordResult()).when(client).executeGetRequest(any(Schema.class));
mktoRR = client.listCustomObjects(iprops);
assertFalse(mktoRR.isSuccess());
assertTrue(mktoRR.getErrorsString().isEmpty());
//
doReturn(getCustomObjectResult()).when(client).executeGetRequest(any(Schema.class));
mktoRR = client.listCustomObjects(iprops);
assertTrue(mktoRR.isSuccess());
assertTrue(mktoRR.getErrorsString().isEmpty());
}
use of org.talend.components.marketo.runtime.client.type.MarketoException in project components by Talend.
the class MarketoBaseRESTClient method httpFakeGet.
public InputStreamReader httpFakeGet(String content, boolean isForLead) throws MarketoException {
try {
current_uri.append(fmtParams(QUERY_METHOD, QUERY_METHOD_GET));
URL url = new URL(current_uri.toString());
HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
urlConn.setRequestMethod(QUERY_METHOD_POST);
if (isForLead) {
urlConn.setRequestProperty(REQUEST_PROPERTY_CONTENT_TYPE, REQUEST_VALUE_APPLICATION_X_WWW_FORM_URLENCODED);
} else {
urlConn.setRequestProperty(REQUEST_PROPERTY_CONTENT_TYPE, REQUEST_VALUE_APPLICATION_JSON);
}
urlConn.setRequestProperty(REQUEST_PROPERTY_ACCEPT, REQUEST_VALUE_TEXT_JSON);
urlConn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(urlConn.getOutputStream());
wr.write(content);
wr.flush();
wr.close();
int responseCode = urlConn.getResponseCode();
if (responseCode == 200) {
InputStream inStream = urlConn.getInputStream();
InputStreamReader reader = new InputStreamReader(inStream);
return reader;
} 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 MarketoBaseRESTClient method executeGetRequest.
public MarketoRecordResult executeGetRequest(Schema schema) throws MarketoException {
try {
URL url = new URL(current_uri.toString());
HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
urlConn.setRequestMethod("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();
MarketoRecordResult mkr = new MarketoRecordResult();
LinkedTreeMap ltm = (LinkedTreeMap) gson.fromJson(reader, Object.class);
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(Arrays.asList(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;
} 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());
}
}
Aggregations