Search in sources :

Example 6 with MarketoException

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());
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) Record(org.apache.avro.generic.GenericData.Record) IndexedRecord(org.apache.avro.generic.IndexedRecord) Date(java.util.Date) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult) Test(org.junit.Test)

Example 7 with MarketoException

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());
}
Also used : MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) Schema(org.apache.avro.Schema) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult) Test(org.junit.Test)

Example 8 with MarketoException

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());
}
Also used : MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) Schema(org.apache.avro.Schema) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult) Test(org.junit.Test)

Example 9 with MarketoException

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());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 10 with MarketoException

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());
    }
}
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) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) IOException(java.io.IOException) URL(java.net.URL) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError) JsonObject(com.google.gson.JsonObject) ArrayList(java.util.ArrayList) List(java.util.List) 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