From 92002c18e457be197c09c20de4a9e0ba7c83d877 Mon Sep 17 00:00:00 2001 From: Hammy Date: Tue, 15 Jun 2021 01:45:58 +0100 Subject: [PATCH] Add/Update documentation --- src/main/java/me/goudham/APIMapper.java | 14 +++++++++++- src/main/java/me/goudham/APIWrapper.java | 27 +++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/main/java/me/goudham/APIMapper.java b/src/main/java/me/goudham/APIMapper.java index a78ade4..12d0bdb 100644 --- a/src/main/java/me/goudham/APIMapper.java +++ b/src/main/java/me/goudham/APIMapper.java @@ -22,7 +22,15 @@ class APIMapper { 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 { return objectMapper.writeValueAsString(obj); } catch (JsonProcessingException jpe) { @@ -67,6 +75,7 @@ class APIMapper { * @param The type of model to be returned. E.g {@link Waifu} or {@link Series} * @return {@link Response} * @throws APIMapperException If {@link ObjectMapper} is not able to deserialize JSON to Java POJO properly + * */ Response> deserializeToList(Result result, JavaType model) throws APIMapperException { Integer statusCode = result.getStatusCode(); @@ -94,6 +103,7 @@ class APIMapper { * @param The type of model to be returned. E.g {@link Waifu} or {@link Series} * @return {@link Response} * @throws APIMapperException If {@link ObjectMapper} is not able to deserialize JSON to Java POJO properly + * */ Response> deserializeToPaginationData(Result result, JavaType model) throws APIMapperException { Integer statusCode = result.getStatusCode(); @@ -119,6 +129,7 @@ class APIMapper { * @return {@link String} — The proper json data to deserialize * @throws JsonProcessingException If {@link ObjectMapper} is not able to * read the given {@code jsonBody} + * */ private String getData(String jsonBody) throws JsonProcessingException { JsonNode parent = objectMapper.readTree(jsonBody); @@ -132,6 +143,7 @@ class APIMapper { * * @param throwable Type of throwable to throw * @throws APIMapperException Purpose of the method + * */ private void throwAPIMapperException(Throwable throwable) throws APIMapperException { String customExceptionMessage = "If you are seeing this message, " + diff --git a/src/main/java/me/goudham/APIWrapper.java b/src/main/java/me/goudham/APIWrapper.java index c55b9ba..6ef4eae 100644 --- a/src/main/java/me/goudham/APIWrapper.java +++ b/src/main/java/me/goudham/APIWrapper.java @@ -58,6 +58,13 @@ public class APIWrapper { 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) { return HttpRequest.newBuilder() .uri(URI.create(host + param)) @@ -65,14 +72,32 @@ public class APIWrapper { .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 { HttpRequest request = getBaseRequest(param).GET().build(); 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 headers) throws APIResponseException, APIMapperException { HttpRequest request = getBaseRequest(param) - .POST(HttpRequest.BodyPublishers.ofString(apiMapper.getValueAsString(headers))) + .POST(HttpRequest.BodyPublishers.ofString(apiMapper.getObjectAsString(headers))) .build(); return sendRequest(request); }