All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.harrys.hyppo.client.v1.HyppoApiClient Maven / Gradle / Ivy

The newest version!
package com.harrys.hyppo.client.v1;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.harrys.hyppo.client.v1.error.InvalidHyppoRequest;
import com.harrys.hyppo.client.v1.model.CreateIngestionJob;
import com.harrys.hyppo.client.v1.model.IngestionJobCreated;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Created by jpetty on 12/18/15.
 */
public final class HyppoApiClient {
    private static final Logger log = LoggerFactory.getLogger(HyppoApiClient.class);

    private final HyppoHttpClient http;

    private final ObjectMapper mapper;

    public HyppoApiClient(final HyppoClientConfig config){
        this.http   = new HyppoHttpClient(config);
        this.mapper = new ObjectMapper();
    }


    public IngestionJobCreated createIngestionJob(final CreateIngestionJob request) throws IOException {
        try {
            final String body = mapper.writeValueAsString(request);
            return http.executePost("/api/v1/ingestion_jobs", body, responseHandler(IngestionJobCreated.class));
        } catch (JsonProcessingException jpe) {
            throw new IllegalArgumentException("Invalid request body object", jpe);
        }
    }



    private  ResponseHandler responseHandler(final Class klass) {
        return new WrapSuccessHandler<>(response -> mapper.readValue(response.getEntity().getContent(), klass));
    }

    private final class WrapSuccessHandler implements ResponseHandler {

        private final ResponseHandler successHandler;

        public WrapSuccessHandler(final ResponseHandler successHandler){
            this.successHandler = successHandler;
        }

        public final T handleResponse(final CloseableHttpResponse response) throws IOException {
            try {
                return successHandler.handleResponse(response);
            } catch (JsonMappingException jme) {
                if (response.getEntity().isRepeatable()) {
                    InvalidHyppoRequest invalid = tryInvalidRequest(response);
                    if (invalid != null) {
                        throw invalid;
                    }
                }
                throw jme;
            } catch (JsonProcessingException jpe){
                if (response.getEntity().isRepeatable()){
                    InvalidHyppoRequest invalid = tryTextResponse(response);
                    if (invalid != null){
                        throw invalid;
                    }
                }
                throw jpe;
            }
        }

        private InvalidHyppoRequest tryTextResponse(final CloseableHttpResponse response) {
            try {
                return new InvalidHyppoRequest(EntityUtils.toString(response.getEntity()));
            } catch (IOException ioe){
                log.warn("Unexpected IOException while attempting to parse as text", ioe);
                return null;
            }
        }

        private InvalidHyppoRequest tryInvalidRequest(final CloseableHttpResponse response) {
            try {
                return mapper.readValue(response.getEntity().getContent(), InvalidHyppoRequest.class);
            } catch (Exception e){
                log.debug("Failed to parse fallback JSON error response " + InvalidHyppoRequest.class.getName(), e);
                return null;
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy