Search in sources :

Example 1 with MarketoError

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

the class MarketoRuntimeTestBase method getFailedSyncResult.

public MarketoSyncResult getFailedSyncResult(String api, String code, String message) {
    MarketoSyncResult mkto = new MarketoSyncResult();
    mkto.setSuccess(false);
    mkto.setRecordCount(0);
    List<MarketoError> errors = new ArrayList<>();
    MarketoError error = new MarketoError(api, code, message);
    errors.add(error);
    mkto.setErrors(errors);
    return mkto;
}
Also used : ArrayList(java.util.ArrayList) MarketoSyncResult(org.talend.components.marketo.runtime.client.type.MarketoSyncResult) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError)

Example 2 with MarketoError

use of org.talend.components.marketo.runtime.client.type.MarketoError 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)

Example 3 with MarketoError

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

the class MarketoBulkExecReaderTest method testRetryOperationFailDieOnError.

@Test
public void testRetryOperationFailDieOnError() throws Exception {
    MarketoRecordResult mkto = new MarketoRecordResult();
    mkto.setErrors(Arrays.asList(new MarketoError("REST", "902", "Invalid operation")));
    when(client.bulkImport(any(TMarketoBulkExecProperties.class))).thenReturn(mkto);
    try {
        reader.start();
        fail("Should not be here");
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("902"));
    }
}
Also used : TMarketoBulkExecProperties(org.talend.components.marketo.tmarketobulkexec.TMarketoBulkExecProperties) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError) Test(org.junit.Test)

Example 4 with MarketoError

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

the class MarketoBulkExecReaderTest method testRetryOperationFailRecoverableErrror.

@Test
public void testRetryOperationFailRecoverableErrror() throws Exception {
    MarketoRecordResult mkto = new MarketoRecordResult();
    mkto.setErrors(Arrays.asList(new MarketoError("REST", "602", "expired header")));
    when(client.bulkImport(any(TMarketoBulkExecProperties.class))).thenReturn(mkto);
    doReturn(true).when(client).isErrorRecoverable(any(List.class));
    reader.properties.dieOnError.setValue(false);
    int minDelay = reader.getRetryAttemps() * reader.getRetryInterval();
    long start = System.currentTimeMillis();
    assertFalse(reader.start());
    long end = System.currentTimeMillis();
    int result = (int) reader.getReturnValues().get(RETURN_NB_CALL);
    assertEquals((long) reader.getRetryAttemps(), result);
    assertTrue(minDelay <= (end - start));
}
Also used : List(java.util.List) TMarketoBulkExecProperties(org.talend.components.marketo.tmarketobulkexec.TMarketoBulkExecProperties) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError) Test(org.junit.Test)

Example 5 with MarketoError

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

the class MarketoBulkExecReaderTest method testStart.

@Test
public void testStart() throws Exception {
    try {
        assertFalse(reader.start());
        fail("Should not be here");
    } catch (Exception e) {
    }
    reader.properties.dieOnError.setValue(false);
    MarketoRecordResult mkto = new MarketoRecordResult();
    mkto.setErrors(Arrays.asList(new MarketoError("REST", "error")));
    when(client.bulkImport(any(TMarketoBulkExecProperties.class))).thenReturn(mkto);
    assertFalse(reader.start());
    IndexedRecord record = new GenericData.Record(MarketoConstants.getEmptySchema());
    mkto.setSuccess(true);
    mkto.setRecords(Arrays.asList(record));
    when(client.bulkImport(any(TMarketoBulkExecProperties.class))).thenReturn(mkto);
    assertTrue(reader.start());
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) IndexedRecord(org.apache.avro.generic.IndexedRecord) TMarketoBulkExecProperties(org.talend.components.marketo.tmarketobulkexec.TMarketoBulkExecProperties) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError) Test(org.junit.Test)

Aggregations

MarketoError (org.talend.components.marketo.runtime.client.type.MarketoError)27 MarketoRecordResult (org.talend.components.marketo.runtime.client.type.MarketoRecordResult)16 Test (org.junit.Test)12 MarketoException (org.talend.components.marketo.runtime.client.type.MarketoException)12 IndexedRecord (org.apache.avro.generic.IndexedRecord)8 MalformedURLException (java.net.MalformedURLException)7 InvalidKeyException (java.security.InvalidKeyException)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 ParseException (java.text.ParseException)7 ArrayList (java.util.ArrayList)7 JAXBException (javax.xml.bind.JAXBException)7 DatatypeConfigurationException (javax.xml.datatype.DatatypeConfigurationException)7 WebServiceException (javax.xml.ws.WebServiceException)7 ComponentException (org.talend.components.api.exception.ComponentException)7 MarketoSyncResult (org.talend.components.marketo.runtime.client.type.MarketoSyncResult)6 ArrayOfString (com.marketo.mktows.ArrayOfString)5 List (java.util.List)4 Schema (org.apache.avro.Schema)4 SyncStatus (org.talend.components.marketo.runtime.client.rest.type.SyncStatus)4 TMarketoBulkExecProperties (org.talend.components.marketo.tmarketobulkexec.TMarketoBulkExecProperties)4