net.joningi.icndb.APIService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of icndb-java-api Show documentation
Show all versions of icndb-java-api Show documentation
Java API client for The Internet Chuck Norris Database (http://www.icndb.com/)
The newest version!
package net.joningi.icndb;
import java.util.List;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import com.google.gson.GsonBuilder;
public class APIService {
public static final String BASE_URL = "http://api.icndb.com/";
protected final Client client;
public APIService() {
this.client = javax.ws.rs.client.ClientBuilder.newClient();
}
private String request(String url) {
try {
final WebTarget myResource = client.target(url);
return myResource.request(MediaType.TEXT_PLAIN).get(String.class);
} catch (NotFoundException e) {
return null;
}
}
public Container get(final String value) {
return new GsonBuilder().create().fromJson(request(BASE_URL + value), Container.class);
}
public MultiContainer getMulti(final String value) {
return new GsonBuilder().create().fromJson(request(BASE_URL + value), MultiContainer.class);
}
public int getCount() {
return new GsonBuilder().create().fromJson(request(BASE_URL + "jokes/count"),
CountContainer.class).getCount();
}
public List getCategories() {
return new GsonBuilder().create().fromJson(request(BASE_URL + "/categories"),
CategoriesContainer.class).getCategories();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy