Add support for retrieving series by ID

pull/6/head
Hammy 4 years ago
parent 2aaba3527f
commit 256341a88d

@ -1,6 +1,8 @@
package org.goudham.me; package org.goudham.me;
import org.goudham.me.api.entity.series.Series;
import org.goudham.me.api.entity.waifu.Waifu; import org.goudham.me.api.entity.waifu.Waifu;
import org.goudham.me.exception.APIMapperException;
import org.goudham.me.exception.APIResponseException; import org.goudham.me.exception.APIResponseException;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -50,14 +52,18 @@ public class MyWaifuClient {
return myWaifuClient; return myWaifuClient;
} }
public Response<Waifu> getWaifu(String slug) throws APIResponseException { public Response<Waifu> getWaifu(String slug) throws APIResponseException, APIMapperException {
return myWaifuWrapper.getWaifu(httpClient, slug); return myWaifuWrapper.getWaifu(httpClient, slug);
} }
public Response<Waifu> getWaifu(Integer id) throws APIResponseException { public Response<Waifu> getWaifu(Integer id) throws APIResponseException, APIMapperException {
return myWaifuWrapper.getWaifu(httpClient, String.valueOf(id)); return myWaifuWrapper.getWaifu(httpClient, String.valueOf(id));
} }
public Response<Series> getSeries(Integer id) throws APIMapperException, APIResponseException {
return myWaifuWrapper.getSeries(httpClient, String.valueOf(id));
}
/** /**
* Sets an instance of HttpClient * Sets an instance of HttpClient
* *

@ -3,7 +3,9 @@ package org.goudham.me;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.goudham.me.api.entity.series.Series;
import org.goudham.me.api.entity.waifu.Waifu; import org.goudham.me.api.entity.waifu.Waifu;
import org.goudham.me.exception.APIMapperException;
import org.goudham.me.exception.APIResponseException; import org.goudham.me.exception.APIResponseException;
import java.net.URI; import java.net.URI;
@ -33,19 +35,24 @@ public class MyWaifuWrapper {
this.apiKey = apiKey; this.apiKey = apiKey;
} }
/** /**
* * Handles sending a request to the API asynchronously using {@link HttpRequest}
* and the underlying {@link HttpClient}
* *
* @param httpClient The {@link HttpClient} to use for sending {@link HttpRequest}'s * @param httpClient The {@link HttpClient} to use for sending {@link HttpRequest}'s
* @param param The end of the endpoint appended onto the host * @param param The end of the endpoint appended onto the host
* @return {@link Result}
* @throws APIResponseException If the {@link CompletableFuture Response}
* cannot be decoded or the thread was interrupted while waiting to receive the data
*
*/ */
private Result sendRequest(HttpClient httpClient, String param) throws APIResponseException { private Result sendRequest(HttpClient httpClient, String param) throws APIResponseException {
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(host + param)) .uri(URI.create(host + param))
.version(httpClient.version()) .version(httpClient.version())
.timeout(Duration.ofSeconds(30)) .timeout(Duration.ofSeconds(20))
.header("Content-Type", "application/json") .headers("Content-Type", "application/json", "apikey", apiKey)
.header("apikey", apiKey)
.GET() .GET()
.build(); .build();
@ -63,26 +70,35 @@ public class MyWaifuWrapper {
return new Result(responseCode, responseBody); return new Result(responseCode, responseBody);
} }
Response<Waifu> getWaifu(HttpClient httpClient, String param) throws APIResponseException { private <T> Response<T> getPopulatedResponse(Result result, Class<T> entity) throws APIMapperException {
Result waifuResult = sendRequest(httpClient, "waifu/" + param); Integer statusCode = result.getStatusCode();
Integer statusCode = waifuResult.getStatusCode(); String body = result.getBody();
String body = waifuResult.getBody(); T newEntity = null;
Waifu waifu = null;
if (statusCode == 200) { if (statusCode == 200) {
try { try {
JsonNode parent = objectMapper.readTree(body); JsonNode parent = objectMapper.readTree(body);
String waifuData = parent.get("data").toString(); String data = parent.get("data").toString();
waifu = objectMapper.readValue(waifuData, Waifu.class); newEntity = objectMapper.readValue(data, entity);
} catch (JsonProcessingException jpe) { } catch (JsonProcessingException jpe) {
jpe.printStackTrace(); String customExceptionMessage = "If you are seeing this message, this is more than likely a fault in my logic. " +
"Please raise an issue including the printed stacktrace :D";
String exceptionMessage = "\n\n" + customExceptionMessage + "\n\n" + jpe.getMessage();
statusCode = 100; throw new APIMapperException(exceptionMessage, jpe);
body = "{\"message\":\"If you are seeing this message, this is more than likely a fault in my logic. " + }
"Please raise an issue with the printed stacktrace :D\",\"code\":Custom 100}";
} }
return new Response<>(statusCode, body, newEntity);
}
Response<Waifu> getWaifu(HttpClient httpClient, String param) throws APIResponseException, APIMapperException {
Result waifuResult = sendRequest(httpClient, "waifu/" + param);
return getPopulatedResponse(waifuResult, Waifu.class);
} }
return new Response<>(statusCode, body, waifu); Response<Series> getSeries(HttpClient httpClient, String param) throws APIResponseException, APIMapperException {
Result seriesResult = sendRequest(httpClient, "series/" + param);
return getPopulatedResponse(seriesResult, Series.class);
} }
} }

Loading…
Cancel
Save