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)
- [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)
- [Java](https://github.com/Xe/PonyAPI/client/java)
- [Java](https://github.com/Xe/PonyAPI/tree/master/client/java)
Routes
------

View File

@ -22,7 +22,7 @@ public class PonyApiExample {
// Initialize a client with a custom API host
PonyApiClient client = new PonyApiClient("some.fqdn.here"); // defaults to port 80
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();
// Get a list of all the episodes

View File

@ -1,5 +1,6 @@
package us.xeserv.ponyapi;
import com.google.common.base.Strings;
import com.google.gson.*;
import java.util.ArrayList;
@ -24,17 +25,35 @@ class JsonDecoder {
}
protected static Episode fromJson(String json) {
JsonObject wrapper = (JsonObject) new JsonParser().parse(json);
JsonObject payload = wrapper.get("episode").getAsJsonObject();
return gson.fromJson(payload, Episode.class);
if (Strings.isNullOrEmpty(json)) {
return null;
}
try {
JsonObject wrapper = (JsonObject) new JsonParser().parse(json);
JsonObject payload = wrapper.get("episode").getAsJsonObject();
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) {
if (Strings.isNullOrEmpty(json)) {
return null;
}
List<Episode> list = new ArrayList<>();
JsonObject wrapper = (JsonObject) new JsonParser().parse(json);
JsonArray payload = wrapper.get("episodes").getAsJsonArray();
for (JsonElement episode : payload) {
list.add(gson.fromJson(episode, Episode.class));
try {
JsonObject wrapper = (JsonObject) new JsonParser().parse(json);
JsonArray payload = wrapper.get("episodes").getAsJsonArray();
for (JsonElement episode : payload) {
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;
}

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
*/
public PonyApiClient(String host) {
@ -136,8 +145,11 @@ public class PonyApiClient {
if (Strings.isNullOrEmpty(query)) {
throw new IllegalArgumentException("A search query is required.");
}
String json = asJson(get("/search?q=" + URLEncoder.encode(query, "UTF-8")));
return JsonDecoder.listFromJson(json);
HttpResponse response = get("/search?q=" + URLEncoder.encode(query, "UTF-8"));
if (statusCode(response) == 404) {
return null;
}
return JsonDecoder.listFromJson(asJson(response));
}
private HttpResponse get(String path) throws IOException {

View File

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

View File

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