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

com.manywho.sdk.client.mappers.ResponseMapper Maven / Gradle / Ivy

package com.manywho.sdk.client.mappers;

import com.fasterxml.jackson.databind.type.CollectionType;
import com.manywho.sdk.exceptions.ManyWhoException;
import org.apache.http.HttpResponse;

import java.io.IOException;
import java.util.List;

import static com.manywho.sdk.services.providers.ObjectMapperProvider.getObjectMapper;

public class ResponseMapper {
    public static  T parseResponse(HttpResponse response, Class responseClass) throws IOException {
        if (!isSuccessStatusCode(response.getStatusLine().getStatusCode())) {
            throw new ManyWhoException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
        }

        return mapResponse(response, responseClass);
    }

    public static  List parseListResponse(HttpResponse response, Class responseClass) throws IOException {
        if (!isSuccessStatusCode(response.getStatusLine().getStatusCode())) {
            throw new ManyWhoException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
        }

        return mapListResponse(response, responseClass);
    }

    protected static  T mapResponse(HttpResponse response, Class responseClass) throws IOException {
        return getObjectMapper().readValue(response.getEntity().getContent(), responseClass);
    }

    protected static  List mapListResponse(HttpResponse response, Class responseClass) throws IOException {
        CollectionType type = getObjectMapper().getTypeFactory().constructCollectionType(List.class, responseClass);

        return getObjectMapper().readValue(response.getEntity().getContent(), type);
    }

    private static boolean isSuccessStatusCode(int statusCode) {
        return statusCode >= 200 && statusCode <= 299;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy