Search in sources :

Example 1 with MarketoRESTClient

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

the class MarketoSourceOrSink method getEndpointSchema.

@Override
public Schema getEndpointSchema(RuntimeContainer container, String schemaName) throws IOException {
    MarketoRESTClient client = (MarketoRESTClient) getClientService(null);
    TMarketoInputProperties ip = new TMarketoInputProperties("retrieveSchema");
    Schema describeSchema = MarketoConstants.getCustomObjectDescribeSchema();
    ip.connection = properties.getConnectionProperties();
    ip.inputOperation.setValue(InputOperation.CustomObject);
    ip.customObjectAction.setValue(CustomObjectAction.describe);
    ip.customObjectName.setValue(schemaName);
    ip.schemaInput.schema.setValue(describeSchema);
    MarketoRecordResult r = client.describeCustomObject(ip);
    if (!r.isSuccess()) {
        return null;
    }
    List<IndexedRecord> records = r.getRecords();
    if (records == null || records.isEmpty()) {
        return null;
    }
    IndexedRecord record = records.get(0);
    return FieldDescription.getSchemaFromJson(schemaName, record.get(describeSchema.getField("fields").pos()).toString(), record.get(describeSchema.getField("dedupeFields").pos()).toString());
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) TMarketoInputProperties(org.talend.components.marketo.tmarketoinput.TMarketoInputProperties) MarketoRESTClient(org.talend.components.marketo.runtime.client.MarketoRESTClient) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult)

Example 2 with MarketoRESTClient

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

the class MarketoSourceOrSink method getDynamicSchema.

/**
 * Retrieve schema for Leads or CustomObjects.
 *
 * @param the ObjectName to get schema. If blank, assumes it's for Lead fields. Otherwise ObjectName should be the
 * CustomObject API's name.
 *
 * @return the schema for the given CustomObject or all Lead fields.
 */
public Schema getDynamicSchema(String objectName, Schema design) throws IOException {
    List<Field> designFields = new ArrayList<>();
    List<String> existingFieldNames = new ArrayList<>();
    for (Field f : design.getFields()) {
        existingFieldNames.add(f.name());
        Field nf = new Field(f.name(), f.schema(), f.doc(), f.defaultVal());
        nf.getObjectProps().putAll(f.getObjectProps());
        for (Map.Entry<String, Object> entry : f.getObjectProps().entrySet()) {
            nf.addProp(entry.getKey(), entry.getValue());
        }
        designFields.add(nf);
    }
    List<Field> objectFields;
    List<Field> resultFields = new ArrayList<>();
    resultFields.addAll(designFields);
    // will fetch fields...
    MarketoRESTClient client = (MarketoRESTClient) getClientService(null);
    if (StringUtils.isEmpty(objectName)) {
        objectFields = client.getAllLeadFields();
    } else {
        objectFields = getSchemaFieldsList(getEndpointSchema(null, objectName));
    }
    for (Field f : objectFields) {
        // test if field isn't already in the schema
        if (!existingFieldNames.contains(f.name())) {
            resultFields.add(f);
        }
    }
    Schema resultSchema = Schema.createRecord(design.getName(), design.getDoc(), design.getNamespace(), design.isError());
    resultSchema.getObjectProps().putAll(design.getObjectProps());
    resultSchema.setFields(resultFields);
    return resultSchema;
}
Also used : Field(org.apache.avro.Schema.Field) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) Map(java.util.Map) MarketoRESTClient(org.talend.components.marketo.runtime.client.MarketoRESTClient)

Example 3 with MarketoRESTClient

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

the class MarketoSourceOrSink method getCompoundKeyFields.

@Override
public List<String> getCompoundKeyFields(String resource) throws IOException {
    MarketoRESTClient client = (MarketoRESTClient) getClientService(null);
    TMarketoInputProperties ip = new TMarketoInputProperties("retrieveSchema");
    Schema describeSchema = MarketoConstants.getCustomObjectDescribeSchema();
    ip.connection = properties.getConnectionProperties();
    ip.inputOperation.setValue(InputOperation.CustomObject);
    ip.customObjectAction.setValue(CustomObjectAction.describe);
    ip.customObjectName.setValue(resource);
    ip.schemaInput.schema.setValue(describeSchema);
    MarketoRecordResult r = client.describeCustomObject(ip);
    if (!r.isSuccess()) {
        return null;
    }
    List<IndexedRecord> records = r.getRecords();
    if (records == null || records.isEmpty()) {
        return null;
    }
    IndexedRecord record = records.get(0);
    String[] keys = new Gson().fromJson(record.get(describeSchema.getField("dedupeFields").pos()).toString(), String[].class);
    return Arrays.asList(keys);
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) Gson(com.google.gson.Gson) TMarketoInputProperties(org.talend.components.marketo.tmarketoinput.TMarketoInputProperties) MarketoRESTClient(org.talend.components.marketo.runtime.client.MarketoRESTClient) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult)

Aggregations

Schema (org.apache.avro.Schema)3 MarketoRESTClient (org.talend.components.marketo.runtime.client.MarketoRESTClient)3 IndexedRecord (org.apache.avro.generic.IndexedRecord)2 MarketoRecordResult (org.talend.components.marketo.runtime.client.type.MarketoRecordResult)2 TMarketoInputProperties (org.talend.components.marketo.tmarketoinput.TMarketoInputProperties)2 Gson (com.google.gson.Gson)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Field (org.apache.avro.Schema.Field)1