Rename MyWaifuWrapper.java to APIWrapper.java

pull/7/head
Hammy 4 years ago
parent 5d2658f495
commit 1f3ea2fc0c

@ -1,8 +1,6 @@
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.FilteredSeries;
import org.goudham.me.api.entity.series.Series;
import org.goudham.me.api.entity.waifu.Waifu;
import org.goudham.me.exception.APIMapperException;
@ -13,6 +11,7 @@ import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
@ -20,19 +19,24 @@ import java.util.concurrent.ExecutionException;
/**
* Returns API information to {@link MyWaifuClient}
*/
public class MyWaifuWrapper {
public class APIWrapper {
private final String version = "1.0";
private static final String host = "https://mywaifulist.moe/api/v1/";
private final String apiKey;
private final ObjectMapper objectMapper = new ObjectMapper();
private final APIMapper apiMapper;
/**
* Instantiates an instance of {@link MyWaifuWrapper} to retrieve API Information
* Instantiates an instance of {@link APIWrapper} to retrieve API Information.
* An instance of {@link APIMapper} is created to be able to {@link APIMapper#deserialize(Result, Class)} JSON to
* Java objects
*
* @param apiKey API Key to authorise API request
*
*/
MyWaifuWrapper(String apiKey) {
APIWrapper(String apiKey) {
this.apiKey = apiKey;
apiMapper = new APIMapper();
}
@ -70,35 +74,13 @@ public class MyWaifuWrapper {
return new Result(responseCode, responseBody);
}
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 data = parent.get("data").toString();
newEntity = objectMapper.readValue(data, entity);
} catch (JsonProcessingException jpe) {
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();
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 apiMapper.deserialize(waifuResult, Waifu.class);
}
Response<Series> getSeries(HttpClient httpClient, String param) throws APIResponseException, APIMapperException {
Result seriesResult = sendRequest(httpClient, "series/" + param);
return getPopulatedResponse(seriesResult, Series.class);
return apiMapper.deserialize(seriesResult, Series.class);
}
}

@ -1,5 +1,6 @@
package org.goudham.me;
import org.goudham.me.api.entity.series.FilteredSeries;
import org.goudham.me.api.entity.series.Series;
import org.goudham.me.api.entity.waifu.Waifu;
import org.goudham.me.exception.APIMapperException;
@ -12,6 +13,7 @@ import java.net.CookieHandler;
import java.net.ProxySelector;
import java.net.http.HttpClient;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.Executor;
/**
@ -19,20 +21,20 @@ import java.util.concurrent.Executor;
*
*
* <p> Main entry point for retrieving information from MyWaifuList.</p>
* <p> {@link MyWaifuWrapper} is utilised to make the API requests </p>
* <p> {@link APIWrapper} is utilised to make the API requests </p>
*/
public class MyWaifuClient {
private final MyWaifuWrapper myWaifuWrapper;
private final APIWrapper APIWrapper;
private HttpClient httpClient;
/**
* Creates an instance of MyWaifuClient
* Creates an instance of {@link MyWaifuClient}
*
* <p>See <a href="https://mywaifulist.docs.stoplight.io/">MyWaifuList</a> for obtaining an API Key</p>
* @param apiKey API Key to authorise API request
*/
MyWaifuClient(@NotNull String apiKey) {
myWaifuWrapper = new MyWaifuWrapper(apiKey);
APIWrapper = new APIWrapper(apiKey);
}
/**
@ -53,15 +55,15 @@ public class MyWaifuClient {
}
public Response<Waifu> getWaifu(String slug) throws APIResponseException, APIMapperException {
return myWaifuWrapper.getWaifu(httpClient, slug);
return APIWrapper.getWaifu(httpClient, slug);
}
public Response<Waifu> getWaifu(Integer id) throws APIResponseException, APIMapperException {
return myWaifuWrapper.getWaifu(httpClient, String.valueOf(id));
return APIWrapper.getWaifu(httpClient, String.valueOf(id));
}
public Response<Series> getSeries(Integer id) throws APIMapperException, APIResponseException {
return myWaifuWrapper.getSeries(httpClient, String.valueOf(id));
return APIWrapper.getSeries(httpClient, String.valueOf(id));
}
/**
@ -78,12 +80,12 @@ public class MyWaifuClient {
*/
public static class Builder {
private final String apiKey;
private final MyWaifuWrapper myWaifuWrapper;
private final APIWrapper APIWrapper;
private HttpClient.Builder httpClientBuilder;
public Builder(String apiKey) {
this.apiKey = apiKey;
myWaifuWrapper = new MyWaifuWrapper(apiKey);
APIWrapper = new APIWrapper(apiKey);
}
public Builder withCookieHandler(CookieHandler cookieHandler) {

@ -1,11 +1,10 @@
package org.goudham.me.exception;
import org.goudham.me.MyWaifuWrapper;
import org.goudham.me.APIWrapper;
import org.goudham.me.Response;
/**
* Thrown when {@link MyWaifuWrapper} fails to unmarshal json into Java POJO's ({@link Response#getEntity()})
* Thrown when {@link APIWrapper} fails to deserialize json into Java POJO's ({@link Response#getEntity()})
*
*/
public class APIMapperException extends Throwable {

@ -1,10 +1,9 @@
package org.goudham.me.exception;
import org.goudham.me.MyWaifuWrapper;
import org.goudham.me.APIWrapper;
/**
* Thrown when {@link MyWaifuWrapper} fails to return API information
* Thrown when {@link APIWrapper} fails to return API information
*
*/
public class APIResponseException extends Throwable {

Loading…
Cancel
Save