package org.ado.biblio.update.kimono;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import org.ado.biblio.update.BiblioUpdateException;
import org.ado.biblio.update.kimono.json.KimonoRelease;
import org.ado.biblio.update.kimono.json.KimonoResponse;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.net.URL;

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Andoni del Olmo
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/**
 * @author Andoni del Olmo,
 * @since 25.01.15
 */
public class KimonoBiblioApi {

    private static final String KIMONO_ENDPOINT_TEMPLATE = "https://www.kimonolabs.com/api/%s?apikey=%s&kimmodify=%d";
    private static final String API_ID = "2fwithpm";
    private static final String API_KEY = "a506e75ca96092b0f73b0ff59c15abe6";
    private static final String KIMONO_URL = String.format(KIMONO_ENDPOINT_TEMPLATE, API_ID, API_KEY, 1);

    public KimonoRelease getLatestRelease() throws BiblioUpdateException {
        return getKimonoResponse().getKimonoRelease();
    }

    private KimonoResponse getKimonoResponse() throws BiblioUpdateException {
        String jsonResponse = null;
        try {
            final Gson gson = new GsonBuilder().create();
            jsonResponse = IOUtils.toString(new URL(KIMONO_URL).openStream());
            return gson.fromJson(jsonResponse, KimonoResponse.class);

        } catch (IOException e) {
            throw new BiblioUpdateException("Cannot establish connection to Kimonolabs server", e);
        } catch (JsonSyntaxException e) {
            throw new BiblioUpdateException(String.format("Unable to parse latest release details from Kimonolabs response: %s", jsonResponse), e);
        }
    }
}
@@ -1722,16 +1722,25 @@
 com/api/
+ondemand/
 %25s?apike

update kiminolab endpoint.