Search in sources :

Example 1 with BulkImportResult

use of org.talend.components.marketo.runtime.client.rest.response.BulkImportResult in project components by Talend.

the class MarketoBulkExecClient method bulkImport.

/**
 * Imports a spreadsheet of leads or custom objects into the target instance
 *
 * POST /bulk/v1/leads/import.json
 *
 * POST /bulk/v1/customobjects/{apiName}/import.json
 *
 * @param parameters
 * @return
 */
public MarketoRecordResult bulkImport(TMarketoBulkExecProperties parameters) {
    String importFilename = parameters.bulkFilePath.getValue();
    String format = parameters.bulkFileFormat.getValue().name();
    Integer pollWaitTime = parameters.pollWaitTime.getValue();
    String logDownloadPath = parameters.logDownloadPath.getValue();
    String lookupField;
    Integer listId;
    String partitionName;
    String customObjectName;
    current_uri = new StringBuilder(bulkPath);
    Boolean isImportingLeads = parameters.bulkImportTo.getValue().equals(BulkImportTo.Leads);
    if (isImportingLeads) {
        lookupField = parameters.lookupField.getValue().name();
        listId = parameters.listId.getValue();
        partitionName = parameters.partitionName.getValue();
        // 
        current_uri.append(API_PATH_BULK_LEADS).append(// 
        fmtParams(FIELD_ACCESS_TOKEN, accessToken, true)).append(fmtParams(FIELD_LOOKUP_FIELD, lookupField));
        if (listId != null) {
            current_uri.append(fmtParams(FIELD_LIST_ID, listId));
        }
        if (!StringUtils.isEmpty(partitionName)) {
            current_uri.append(fmtParams(FIELD_PARTITION_NAME, partitionName));
        }
    } else {
        customObjectName = parameters.customObjectName.getValue();
        // 
        current_uri.append(String.format(API_PATH_BULK_CUSTOMOBJECTS, customObjectName)).append(fmtParams(FIELD_ACCESS_TOKEN, accessToken, true));
    }
    current_uri.append(fmtParams(FIELD_FORMAT, format));
    // 
    MarketoRecordResult mkto = new MarketoRecordResult();
    try {
        LOG.debug("bulkImport {}.", current_uri);
        BulkImportResult rs = executePostFileRequest(BulkImportResult.class, importFilename);
        LOG.debug("rs = {}.", rs);
        mkto.setRequestId(REST + "::" + rs.getRequestId());
        mkto.setSuccess(rs.isSuccess());
        if (!mkto.isSuccess()) {
            mkto.setRecordCount(0);
            mkto.setErrors(Arrays.asList(new MarketoError(REST, rs.getErrors().get(0).getCode(), messages.getMessage("bulkimport.error.import", rs.getErrors().get(0).getMessage()))));
            return mkto;
        }
        BulkImport bulkResult = rs.getResult().get(0);
        if (bulkResult.getStatus().equals(BULK_STATUS_FAILED)) {
            // request Failed
            String err = messages.getMessage("bulkimport.status.failed", bulkResult.getMessage());
            LOG.error("{}.", err);
            mkto.setSuccess(false);
            mkto.setErrors(Arrays.asList(new MarketoError(REST, err)));
        } else if (bulkResult.getStatus().equals(BULK_STATUS_COMPLETE)) {
            // already Complete
            bulkResult = getStatusesForBatch(bulkResult, logDownloadPath);
        } else {
            // Importing, Queued
            while (true) {
                try {
                    LOG.warn(messages.getMessage("bulkimport.status.waiting", pollWaitTime));
                    Thread.sleep(pollWaitTime * 1000L);
                    current_uri = new StringBuilder(bulkPath);
                    if (bulkResult.isBulkLeadsImport()) {
                        current_uri.append(String.format(API_PATH_BULK_LEADS_RESULT_STATUS, bulkResult.getBatchId()));
                    } else {
                        current_uri.append(String.format(API_PATH_BULK_CUSTOMOBJECTS_RESULT, bulkResult.getObjectApiName(), bulkResult.getBatchId(), "status"));
                    }
                    current_uri.append(fmtParams(FIELD_ACCESS_TOKEN, accessToken, true));
                    LOG.debug("status = {}.", current_uri);
                    rs = (BulkImportResult) executeGetRequest(BulkImportResult.class);
                    if (rs.isSuccess() && rs.getResult().get(0).getStatus().equals(BULK_STATUS_COMPLETE)) {
                        bulkResult = getStatusesForBatch(rs.getResult().get(0), logDownloadPath);
                        break;
                    } else {
                        LOG.warn(messages.getMessage("bulkimport.status.current", rs.getResult().get(0).getStatus()));
                    }
                } catch (InterruptedException e) {
                    LOG.error("Sleep interrupted : {}", e);
                    throw new MarketoException(REST, "Sleep interrupted : " + e.getLocalizedMessage());
                }
            }
        }
        mkto.setRecords(Arrays.asList(bulkResult.toIndexedRecord()));
        mkto.setRecordCount(1);
        mkto.setRemainCount(0);
    } catch (MarketoException e) {
        mkto.setSuccess(false);
        mkto.setErrors(Arrays.asList(e.toMarketoError()));
    }
    return mkto;
}
Also used : BulkImport(org.talend.components.marketo.runtime.client.rest.type.BulkImport) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) BulkImportResult(org.talend.components.marketo.runtime.client.rest.response.BulkImportResult) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError)

Example 2 with BulkImportResult

use of org.talend.components.marketo.runtime.client.rest.response.BulkImportResult 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());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) Gson(com.google.gson.Gson) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BulkImportResult(org.talend.components.marketo.runtime.client.rest.response.BulkImportResult) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) PrintWriter(java.io.PrintWriter)

Aggregations

BulkImportResult (org.talend.components.marketo.runtime.client.rest.response.BulkImportResult)2 MarketoException (org.talend.components.marketo.runtime.client.type.MarketoException)2 Gson (com.google.gson.Gson)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 URL (java.net.URL)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1 BulkImport (org.talend.components.marketo.runtime.client.rest.type.BulkImport)1 MarketoError (org.talend.components.marketo.runtime.client.type.MarketoError)1 MarketoRecordResult (org.talend.components.marketo.runtime.client.type.MarketoRecordResult)1