
package org.apache.camel.component.google.drive;

import java.util.Map;
import java.util.Map.Entry;

import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;

import org.apache.camel.Processor;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.TypeConverter;
import org.apache.camel.component.google.drive.internal.GoogleDriveApiName;
import org.apache.camel.util.IntrospectionSupport;
import org.apache.camel.util.component.AbstractApiConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The GoogleDrive consumer.
 */
public class GoogleDriveConsumer extends AbstractApiConsumer<GoogleDriveApiName, GoogleDriveConfiguration> {

    private static final Logger LOG = LoggerFactory.getLogger(BatchGoogleDriveClientFactory.class);
    
    public GoogleDriveConsumer(GoogleDriveEndpoint endpoint, Processor processor) {
        super(endpoint, processor);
    } 
    
    @Override
    protected Object doInvokeMethod(Map<String, Object> properties) throws RuntimeCamelException {
        AbstractGoogleClientRequest request = (AbstractGoogleClientRequest) super.doInvokeMethod(properties);
        try {
            TypeConverter typeConverter = getEndpoint().getCamelContext().getTypeConverter();
            for (Entry<String, Object> p : properties.entrySet()) {
                IntrospectionSupport.setProperty(typeConverter, request, p.getKey(), p.getValue());
            }
            return request.execute();
        } catch (Exception e) {
            throw new RuntimeCamelException(e);
        }
    }      

}
