Add/Update documentation

pull/12/head
Hammy 3 years ago
parent eb45813c7f
commit 92002c18e4

@ -22,7 +22,15 @@ class APIMapper {
objectMapper = new ObjectMapper(); objectMapper = new ObjectMapper();
} }
String getValueAsString(Object obj) throws APIMapperException { /**
* Convert any object passed and return as a {@link String}
*
* @param obj {@link Object} to write as {@link String}
* @return {@link String}
* @throws APIMapperException If {@link ObjectMapper} is not able to serialize object into {@link String}
*
*/
String getObjectAsString(Object obj) throws APIMapperException {
try { try {
return objectMapper.writeValueAsString(obj); return objectMapper.writeValueAsString(obj);
} catch (JsonProcessingException jpe) { } catch (JsonProcessingException jpe) {
@ -67,6 +75,7 @@ class APIMapper {
* @param <T> The type of model to be returned. E.g {@link Waifu} or {@link Series} * @param <T> The type of model to be returned. E.g {@link Waifu} or {@link Series}
* @return {@link Response} * @return {@link Response}
* @throws APIMapperException If {@link ObjectMapper} is not able to deserialize JSON to Java POJO properly * @throws APIMapperException If {@link ObjectMapper} is not able to deserialize JSON to Java POJO properly
*
*/ */
<T> Response<List<T>> deserializeToList(Result result, JavaType model) throws APIMapperException { <T> Response<List<T>> deserializeToList(Result result, JavaType model) throws APIMapperException {
Integer statusCode = result.getStatusCode(); Integer statusCode = result.getStatusCode();
@ -94,6 +103,7 @@ class APIMapper {
* @param <T> The type of model to be returned. E.g {@link Waifu} or {@link Series} * @param <T> The type of model to be returned. E.g {@link Waifu} or {@link Series}
* @return {@link Response} * @return {@link Response}
* @throws APIMapperException If {@link ObjectMapper} is not able to deserialize JSON to Java POJO properly * @throws APIMapperException If {@link ObjectMapper} is not able to deserialize JSON to Java POJO properly
*
*/ */
<T> Response<PaginationData<T>> deserializeToPaginationData(Result result, JavaType model) throws APIMapperException { <T> Response<PaginationData<T>> deserializeToPaginationData(Result result, JavaType model) throws APIMapperException {
Integer statusCode = result.getStatusCode(); Integer statusCode = result.getStatusCode();
@ -119,6 +129,7 @@ class APIMapper {
* @return {@link String} The proper json data to deserialize * @return {@link String} The proper json data to deserialize
* @throws JsonProcessingException If {@link ObjectMapper} is not able to * @throws JsonProcessingException If {@link ObjectMapper} is not able to
* read the given {@code jsonBody} * read the given {@code jsonBody}
*
*/ */
private String getData(String jsonBody) throws JsonProcessingException { private String getData(String jsonBody) throws JsonProcessingException {
JsonNode parent = objectMapper.readTree(jsonBody); JsonNode parent = objectMapper.readTree(jsonBody);
@ -132,6 +143,7 @@ class APIMapper {
* *
* @param throwable Type of throwable to throw * @param throwable Type of throwable to throw
* @throws APIMapperException Purpose of the method * @throws APIMapperException Purpose of the method
*
*/ */
private void throwAPIMapperException(Throwable throwable) throws APIMapperException { private void throwAPIMapperException(Throwable throwable) throws APIMapperException {
String customExceptionMessage = "If you are seeing this message, " + String customExceptionMessage = "If you are seeing this message, " +

@ -58,6 +58,13 @@ public class APIWrapper {
apiMapper = new APIMapper(); apiMapper = new APIMapper();
} }
/**
* Create base {@link HttpRequest.Builder} with custom url, default headers and timeout
*
* @param param The end of the endpoint appended onto the host
* @return {@link HttpRequest.Builder}
*
*/
private HttpRequest.Builder getBaseRequest(String param) { private HttpRequest.Builder getBaseRequest(String param) {
return HttpRequest.newBuilder() return HttpRequest.newBuilder()
.uri(URI.create(host + param)) .uri(URI.create(host + param))
@ -65,14 +72,32 @@ public class APIWrapper {
.headers("Content-Type", "application/json", "apikey", apiKey); .headers("Content-Type", "application/json", "apikey", apiKey);
} }
/**
* Separate method for sending GET requests
*
* @param param The end of the endpoint appended onto the host
* @return {@link Result}
* @throws APIResponseException If {@link #sendRequest(HttpRequest)} cannot retrieve the proper data from the API
*
*/
Result sendGetRequest(String param) throws APIResponseException { Result sendGetRequest(String param) throws APIResponseException {
HttpRequest request = getBaseRequest(param).GET().build(); HttpRequest request = getBaseRequest(param).GET().build();
return sendRequest(request); return sendRequest(request);
} }
/**
* Separate method for sending POST requests
*
* @param param The end of the endpoint appended onto the host
* @param headers Headers as Key/Value pairs for POST requests
* @return {@link Result}
* @throws APIResponseException If {@link #sendRequest(HttpRequest)} cannot retrieve the proper data from the API
* @throws APIMapperException If {@link APIMapper#getObjectAsString(Object)} cannot properly serialize object
*
*/
private Result sendPostRequest(String param, Map<String, String> headers) throws APIResponseException, APIMapperException { private Result sendPostRequest(String param, Map<String, String> headers) throws APIResponseException, APIMapperException {
HttpRequest request = getBaseRequest(param) HttpRequest request = getBaseRequest(param)
.POST(HttpRequest.BodyPublishers.ofString(apiMapper.getValueAsString(headers))) .POST(HttpRequest.BodyPublishers.ofString(apiMapper.getObjectAsString(headers)))
.build(); .build();
return sendRequest(request); return sendRequest(request);
} }

Loading…
Cancel
Save