Merge pull request #5 from akaritakai/master

Improved failure behavior and spelling corrections.
This commit is contained in:
Christine Dodrill 2015-08-19 15:35:55 -07:00
commit 6aa9ec5b18
6 changed files with 76 additions and 22 deletions

View File

@ -35,7 +35,7 @@ Clients
- [Go](https://godoc.org/github.com/Xe/PonyAPI/client/go) - [Go](https://godoc.org/github.com/Xe/PonyAPI/client/go)
- [Nim](https://github.com/Xe/PonyAPI/blob/master/client/nim/ponyapi.nim) [Docs](http://ponyapi.apps.xeserv.us/static/nim.html) - [Nim](https://github.com/Xe/PonyAPI/blob/master/client/nim/ponyapi.nim) [Docs](http://ponyapi.apps.xeserv.us/static/nim.html)
- [Python](https://github.com/Xe/PonyAPI/blob/master/client/python/ponyapi.py) - [Python](https://github.com/Xe/PonyAPI/blob/master/client/python/ponyapi.py)
- [Java](https://github.com/Xe/PonyAPI/client/java) - [Java](https://github.com/Xe/PonyAPI/tree/master/client/java)
Routes Routes
------ ------

View File

@ -22,7 +22,7 @@ public class PonyApiExample {
// Initialize a client with a custom API host // Initialize a client with a custom API host
PonyApiClient client = new PonyApiClient("some.fqdn.here"); // defaults to port 80 PonyApiClient client = new PonyApiClient("some.fqdn.here"); // defaults to port 80
client = new PonyApiClient("some.fqdn.here", 8080); // with a custom port number client = new PonyApiClient("some.fqdn.here", 8080); // with a custom port number
// Initialize a client using http://ponyapi.apps.xeserv.us/ as the API ohst // Initialize a client using http://ponyapi.apps.xeserv.us/ as the API host
client = new PonyApiClient(); client = new PonyApiClient();
// Get a list of all the episodes // Get a list of all the episodes

View File

@ -1,5 +1,6 @@
package us.xeserv.ponyapi; package us.xeserv.ponyapi;
import com.google.common.base.Strings;
import com.google.gson.*; import com.google.gson.*;
import java.util.ArrayList; import java.util.ArrayList;
@ -24,18 +25,36 @@ class JsonDecoder {
} }
protected static Episode fromJson(String json) { protected static Episode fromJson(String json) {
if (Strings.isNullOrEmpty(json)) {
return null;
}
try {
JsonObject wrapper = (JsonObject) new JsonParser().parse(json); JsonObject wrapper = (JsonObject) new JsonParser().parse(json);
JsonObject payload = wrapper.get("episode").getAsJsonObject(); JsonObject payload = wrapper.get("episode").getAsJsonObject();
return gson.fromJson(payload, Episode.class); return gson.fromJson(payload, Episode.class);
} catch (Exception ignored) {
// TODO: Logging for parse errors or passing a general parse exception
}
return null;
} }
protected static List<Episode> listFromJson(String json) { protected static List<Episode> listFromJson(String json) {
if (Strings.isNullOrEmpty(json)) {
return null;
}
List<Episode> list = new ArrayList<>(); List<Episode> list = new ArrayList<>();
try {
JsonObject wrapper = (JsonObject) new JsonParser().parse(json); JsonObject wrapper = (JsonObject) new JsonParser().parse(json);
JsonArray payload = wrapper.get("episodes").getAsJsonArray(); JsonArray payload = wrapper.get("episodes").getAsJsonArray();
for (JsonElement episode : payload) { for (JsonElement episode : payload) {
list.add(gson.fromJson(episode, Episode.class)); list.add(gson.fromJson(episode, Episode.class));
} }
} catch (Exception ignored) {
// TODO: Logging for parse errors or passing a general parse exception
}
if (list.isEmpty()) {
return null;
}
return list; return list;
} }

View File

@ -29,7 +29,16 @@ public class PonyApiClient {
} }
/** /**
* Accepts a FQDN (and optionally port) running the PonyAPI service. * Accepts a FQDN and port running the PonyAPI service on a specified port.
* @param host a FQDN running the PonyAPI service
* @param port the port the PonyAPI service is running on
*/
public PonyApiClient(String host, int port) {
this.host = host + ":" + port;
}
/**
* Accepts a FQDN running the PonyAPI service on port 80.
* @param host a FQDN running the PonyAPI service * @param host a FQDN running the PonyAPI service
*/ */
public PonyApiClient(String host) { public PonyApiClient(String host) {
@ -136,8 +145,11 @@ public class PonyApiClient {
if (Strings.isNullOrEmpty(query)) { if (Strings.isNullOrEmpty(query)) {
throw new IllegalArgumentException("A search query is required."); throw new IllegalArgumentException("A search query is required.");
} }
String json = asJson(get("/search?q=" + URLEncoder.encode(query, "UTF-8"))); HttpResponse response = get("/search?q=" + URLEncoder.encode(query, "UTF-8"));
return JsonDecoder.listFromJson(json); if (statusCode(response) == 404) {
return null;
}
return JsonDecoder.listFromJson(asJson(response));
} }
private HttpResponse get(String path) throws IOException { private HttpResponse get(String path) throws IOException {

View File

@ -6,9 +6,18 @@ import java.time.Instant;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
public class JsonDecoderTest { public class JsonDecoderTest {
@Test
public void malformedInputTest() {
assertNull(JsonDecoder.fromJson(null));
assertNull(JsonDecoder.fromJson(""));
assertNull(JsonDecoder.fromJson("{ \"error\": { \"msg\": \"some error\", \"code\": 404 }"));
}
@Test @Test
public void episodeTest() { public void episodeTest() {
String json = "{\n" String json = "{\n"
@ -51,6 +60,13 @@ public class JsonDecoderTest {
assertEquals(true, episode.isMovie); assertEquals(true, episode.isMovie);
} }
@Test
public void multipleMalformedInputTest() {
assertNull(JsonDecoder.listFromJson(null));
assertNull(JsonDecoder.listFromJson(""));
assertNull(JsonDecoder.listFromJson("{ \"error\": { \"msg\": \"some error\", \"code\": 404 }"));
}
@Test @Test
public void multipleTest() { public void multipleTest() {
String json = "{\n" String json = "{\n"
@ -73,6 +89,7 @@ public class JsonDecoderTest {
+ "}"; + "}";
List<Episode> episodes = JsonDecoder.listFromJson(json); List<Episode> episodes = JsonDecoder.listFromJson(json);
assertNotNull(episodes);
assertEquals(2, episodes.size()); assertEquals(2, episodes.size());
Episode episode1 = episodes.get(0); Episode episode1 = episodes.get(0);

View File

@ -33,7 +33,7 @@ public class PonyApiClientTest {
@Test @Test
public void getSeasonTest() throws IOException { public void getSeasonTest() throws IOException {
List<Episode> list = client.getSeason(98); List<Episode> list = client.getSeason(98);
assertEquals(null, list); assertNull(list);
list = client.getSeason(1); list = client.getSeason(1);
assertEquals(26, list.size()); assertEquals(26, list.size());
} }
@ -46,22 +46,26 @@ public class PonyApiClientTest {
@Test @Test
public void getEpisodeTest() throws IOException { public void getEpisodeTest() throws IOException {
Episode episode = client.getEpisode(1, 1); Episode episode = client.getEpisode(98, 1);
assertNull(episode);
episode = client.getEpisode(1, 1);
assertEquals("Friendship is Magic Part 1", episode.name); assertEquals("Friendship is Magic Part 1", episode.name);
assertEquals(Instant.ofEpochSecond(1286735400), episode.airDate); assertEquals(Instant.ofEpochSecond(1286735400), episode.airDate);
assertEquals(1, episode.season); assertEquals(1, episode.season);
assertEquals(1, episode.episode); assertEquals(1, episode.episode);
assertEquals(false, episode.isMovie); assertFalse(episode.isMovie);
} }
@Test @Test
public void getMovieTest() throws IOException { public void getMovieTest() throws IOException {
Episode episode = client.getMovie(1); Episode movie = client.getMovie(98);
assertEquals("Equestria Girls", episode.name); assertNull(movie);
assertEquals(Instant.ofEpochSecond(1371340800), episode.airDate); movie = client.getMovie(1);
assertEquals(99, episode.season); assertEquals("Equestria Girls", movie.name);
assertEquals(1, episode.episode); assertEquals(Instant.ofEpochSecond(1371340800), movie.airDate);
assertEquals(true, episode.isMovie); assertEquals(99, movie.season);
assertEquals(1, movie.episode);
assertTrue(movie.isMovie);
} }
@Test @Test
@ -72,7 +76,9 @@ public class PonyApiClientTest {
@Test @Test
public void searchTest() throws IOException { public void searchTest() throws IOException {
List<Episode> episodes = client.search("Owl's Well"); List<Episode> episodes = client.search("No Results");
assertNull(episodes);
episodes = client.search("Owl's Well");
assertEquals(1, episodes.size()); assertEquals(1, episodes.size());
Episode episode = episodes.get(0); Episode episode = episodes.get(0);
assertEquals("Owl's Well That Ends Well", episode.name); assertEquals("Owl's Well That Ends Well", episode.name);