Java tutorial
/* * Copyright (C) 2013 Edouard Murat. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301 USA */ package com.eddy.malupdater; import com.eddy.malupdater.progress.ProgressListener; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientRequest; import com.sun.jersey.api.client.UniformInterfaceException; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.filter.ConnectionListenerFilter; import com.sun.jersey.api.client.filter.ContainerListener; import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; import com.sun.jersey.api.client.filter.OnStartConnectionListener; import com.sun.jersey.core.util.MultivaluedMapImpl; import javax.ws.rs.core.MultivaluedMap; /** * * @author eddy */ public class MalAPI { private static JsonParser jsonParser = new JsonParser(); private static Gson gson = new Gson(); public static AnimeList getAnimeList(String username) { try { String jsonResponse = GET("http://mal-api.com/animelist/" + username); JsonObject object = jsonParser.parse(jsonResponse).getAsJsonObject(); return jsonToAnimeList(object.getAsJsonArray("anime")); } catch (UniformInterfaceException e) { return new AnimeList(); } } public static void updateAnime(Anime anime) { MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl(); queryParams.putSingle("status", anime.getWatched_status().name().toLowerCase()); queryParams.putSingle("episodes", String.valueOf(anime.getWatched_episodes())); queryParams.putSingle("score", String.valueOf(anime.getScore())); String request = String.format("http://mal-api.com/animelist/anime/%d", anime.getId()); PUT(request, queryParams, new HTTPBasicAuthFilter(MyProperties.getUsername(), String.valueOf(MyProperties.getPassword()))); } private static AnimeList jsonToAnimeList(JsonArray array) { AnimeList list = new AnimeList(); for (JsonElement element : array) { Anime anime = jsonToAnime(element.getAsJsonObject()); list.add(anime); } return list; } private static Anime jsonToAnime(JsonObject object) { int episodes = gson.fromJson(object.get("episodes"), int.class); int id = gson.fromJson(object.get("id"), int.class); int score = gson.fromJson(object.get("score"), int.class); String title = gson.fromJson(object.get("title"), String.class); String type = gson.fromJson(object.get("type"), String.class); int watched_episodes = gson.fromJson(object.get("watched_episodes"), int.class); String watched_status = gson.fromJson(object.get("watched_status"), String.class); return new Anime(episodes, id, score, title, type, watched_episodes, watched_status); } private static String GET(String request) throws UniformInterfaceException { Client client = Client.create(); client.addFilter(new ConnectionListenerFilter(new OnStartConnectionListener() { @Override public ContainerListener onStart(ClientRequest cr) { return ProgressListener.getInstance(); } })); WebResource webResource = client.resource(request); return webResource.get(String.class); } private static String PUT(String request, MultivaluedMap<String, String> params, HTTPBasicAuthFilter authFilter) { Client client = Client.create(); client.addFilter(authFilter); WebResource webResource = client.resource(request).queryParams(params); return webResource.put(String.class); } }