// Path: idobata4j-core/src/main/java/com/uphyca/idobata/pusher/PusherBuilder.java
// public class PusherBuilder {
// 
//     private static final String API_KEY = "44ffe67af1c7035be764";
// 
//     private PusherOptions options;
// 
//     /** Options for the Pusher client library to use. */
//     public PusherBuilder setOptions(PusherOptions options) {
//         this.options = options;
//         return this;
//     }
// 
//     public PusherBuilder buildUpon() {
//         return new PusherBuilder().setOptions(options);
//     }
// 
//     /**
//      * Create the {@link com.pusher.client.Pusher} instances.
//      */
//     public Pusher build() {
//         if (options == null) {
//             return new Pusher(API_KEY);
//         }
//         return new Pusher(API_KEY, options);
//     }
// }
// 
// Path: idobata4j-core/src/main/java/com/uphyca/idobata/transform/Converter.java
// public interface Converter {
// 
//     /**
//      * Convert an HTTP response body to a concrete object of the specified type.
//      * 
//      * @param body HTTP response body.
//      * @param type Target object type.
//      * @return Instance of {@code type} which will be cast by the caller.
//      * @throws Exception
//      */
//     <T> T convert(TypedInput body, Type type) throws ConversionException, IOException;
// }


import java.util.List;
import com.pusher.client.AuthorizationFailureException;
import com.pusher.client.Authorizer;
import com.pusher.client.Pusher;
import com.pusher.client.PusherOptions;
import com.uphyca.idobata.http.*;
import com.uphyca.idobata.model.*;
import com.uphyca.idobata.pusher.PusherBuilder;
import com.uphyca.idobata.transform.Converter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
/*
 * Copyright (C) 2014 uPhyca Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.uphyca.idobata;



/**
 * Default implementation of {@link com.uphyca.idobata.Idobata}
 * 
 * @see com.uphyca.idobata.IdobataBuilder
 * @author Sosuke Masui (masui@uphyca.com)
 */
class IdobataImpl implements Idobata {

    private final Client client;
    private final RequestInterceptor requestInterceptor;
    private final Converter converter;
    private final PusherBuilder pusherBuilder;

    IdobataImpl(Client client, RequestInterceptor requestInterceptor, Converter converter, PusherBuilder pusherBuilder) {
        this.client = client;
        this.requestInterceptor = requestInterceptor;
        this.converter = converter;
        this.pusherBuilder = pusherBuilder;
    }

    @Override
    public Seed getSeed() throws IdobataError {
        Endpoint endpoint = new Endpoint("https://idobata.io/api/seed");
        Request request = new Request("GET", endpoint.build(), Collections.<Header> emptyList(), null);
        Response response = requestInterceptor.execute(client, request);
        try {
            return converter.convert(response.getBody(), Seed.class);
        } catch (Exception e) {
            throw new IdobataError(e);
        }
    }

    @Override
    public List<Message> getMessages(List<String> ids) throws IdobataError {
        Endpoint endpoint = new Endpoint("https://idobata.io/api/messages").addQuery("ids[]", ids);
        Request request = new Request("GET", endpoint.build(), Collections.<Header> emptyList(), null);
        Response response = requestInterceptor.execute(client, request);
        try {
// Path: idobata4j-core/src/main/java/com/uphyca/idobata/pusher/PusherBuilder.java
// public class PusherBuilder {
// 
//     private static final String API_KEY = "44ffe67af1c7035be764";
// 
//     private PusherOptions options;
// 
//     /** Options for the Pusher client library to use. */
//     public PusherBuilder setOptions(PusherOptions options) {
//         this.options = options;
//         return this;
//     }
// 
//     public PusherBuilder buildUpon() {
//         return new PusherBuilder().setOptions(options);
//     }
// 
//     /**
//      * Create the {@link com.pusher.client.Pusher} instances.
//      */
//     public Pusher build() {
//         if (options == null) {
//             return new Pusher(API_KEY);
//         }
//         return new Pusher(API_KEY, options);
//     }
// }
// 
// Path: idobata4j-core/src/main/java/com/uphyca/idobata/transform/Converter.java
// public interface Converter {
// 
//     /**
//      * Convert an HTTP response body to a concrete object of the specified type.
//      * 
//      * @param body HTTP response body.
//      * @param type Target object type.
//      * @return Instance of {@code type} which will be cast by the caller.
//      * @throws Exception
//      */
//     <T> T convert(TypedInput body, Type type) throws ConversionException, IOException;
// }


// Path: idobata4j-core/src/main/java/com/uphyca/idobata/IdobataImpl.java
import java.util.List;
import com.pusher.client.AuthorizationFailureException;
import com.pusher.client.Authorizer;
import com.pusher.client.Pusher;
import com.pusher.client.PusherOptions;
import com.uphyca.idobata.http.*;
import com.uphyca.idobata.model.*;
import com.uphyca.idobata.pusher.PusherBuilder;
import com.uphyca.idobata.transform.Converter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;

/*
 * Copyright (C) 2014 uPhyca Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.uphyca.idobata;



/**
 * Default implementation of {@link com.uphyca.idobata.Idobata}
 * 
 * @see com.uphyca.idobata.IdobataBuilder
 * @author Sosuke Masui (masui@uphyca.com)
 */
class IdobataImpl implements Idobata {

    private final Client client;
    private final RequestInterceptor requestInterceptor;
    private final Converter converter;
    private final PusherBuilder pusherBuilder;

    IdobataImpl(Client client, RequestInterceptor requestInterceptor, Converter converter, PusherBuilder pusherBuilder) {
        this.client = client;
        this.requestInterceptor = requestInterceptor;
        this.converter = converter;
        this.pusherBuilder = pusherBuilder;
    }

    @Override
    public Seed getSeed() throws IdobataError {
        Endpoint endpoint = new Endpoint("https://idobata.io/api/seed");
        Request request = new Request("GET", endpoint.build(), Collections.<Header> emptyList(), null);
        Response response = requestInterceptor.execute(client, request);
        try {
            return converter.convert(response.getBody(), Seed.class);
        } catch (Exception e) {
            throw new IdobataError(e);
        }
    }

    @Override
    public List<Message> getMessages(List<String> ids) throws IdobataError {
        Endpoint endpoint = new Endpoint("https://idobata.io/api/messages").addQuery("ids[]", ids);
        Request request = new Request("GET", endpoint.build(), Collections.<Header> emptyList(), null);
        Response response = requestInterceptor.execute(client, request);
        try {
            return converter.convert(response.getBody(), Message[].class);