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

com.treasuredata.client.TDHttpRequestHandlers Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package com.treasuredata.client;

import com.google.common.base.Function;
import okhttp3.Response;
import okhttp3.ResponseBody;

import java.io.InputStream;

/**
 * Request handler implementations
 */
public class TDHttpRequestHandlers
{
    private TDHttpRequestHandlers()
    {
    }

    public static final TDHttpRequestHandler stringContentHandler = new TDHttpRequestHandler()
    {
        @Override
        public String onSuccess(Response response)
                throws Exception
        {
            return response.body().string();
        }
    };

    public static final TDHttpRequestHandler byteArrayContentHandler = new TDHttpRequestHandler()
    {
        @Override
        public byte[] onSuccess(Response response)
                throws Exception
        {
            return response.body().bytes();
        }
    };

    public static final  TDHttpRequestHandler newByteStreamHandler(final Function handler)
    {
        return new TDHttpRequestHandler()
        {
            @Override
            public Result onSuccess(Response response)
                    throws Exception
            {
                try (ResponseBody body = response.body()) {
                    return handler.apply(body.byteStream());
                }
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy