Add support for retrieving series by ID

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

@ -1,6 +1,8 @@
package org.goudham.me;
import org.goudham.me.api.entity.series.Series;
import org.goudham.me.api.entity.waifu.Waifu;
import org.goudham.me.exception.APIMapperException;
import org.goudham.me.exception.APIResponseException;
import org.jetbrains.annotations.NotNull;
@ -50,14 +52,18 @@ public class 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);
}
public Response<Waifu> getWaifu(Integer id) throws APIResponseException {
public Response<Waifu> getWaifu(Integer id) throws APIResponseException, APIMapperException {
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
*

@ -3,7 +3,9 @@ package org.goudham.me;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
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.exception.APIMapperException;
import org.goudham.me.exception.APIResponseException;
import java.net.URI;
@ -33,19 +35,24 @@ public class MyWaifuWrapper {
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 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 {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(host + param))
.version(httpClient.version())
.timeout(Duration.ofSeconds(30))
.header("Content-Type", "application/json")
.header("apikey", apiKey)
.timeout(Duration.ofSeconds(20))
.headers("Content-Type", "application/json", "apikey", apiKey)
.GET()
.build();
@ -63,26 +70,35 @@ public class MyWaifuWrapper {
return new Result(responseCode, responseBody);
}
Response<Waifu> getWaifu(HttpClient httpClient, String param) throws APIResponseException {
Result waifuResult = sendRequest(httpClient, "waifu/" + param);
Integer statusCode = waifuResult.getStatusCode();
String body = waifuResult.getBody();
Waifu waifu = null;
private <T> Response<T> getPopulatedResponse(Result result, Class<T> entity) throws APIMapperException {
Integer statusCode = result.getStatusCode();
String body = result.getBody();
T newEntity = null;
if (statusCode == 200) {
try {
JsonNode parent = objectMapper.readTree(body);
String waifuData = parent.get("data").toString();
waifu = objectMapper.readValue(waifuData, Waifu.class);
String data = parent.get("data").toString();
newEntity = objectMapper.readValue(data, entity);
} 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;
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}";
throw new APIMapperException(exceptionMessage, jpe);
}
}
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