diff --git a/src/main/java/me/goudham/APIMapper.java b/src/main/java/me/goudham/APIMapper.java index 12d0bdb..dacb078 100644 --- a/src/main/java/me/goudham/APIMapper.java +++ b/src/main/java/me/goudham/APIMapper.java @@ -50,8 +50,8 @@ class APIMapper { * */ Response deserialize(Result result, Class model) throws APIMapperException { - Integer statusCode = result.getStatusCode(); - String body = result.getBody(); + Integer statusCode = result.statusCode(); + String body = result.body(); T newModel = null; if (statusCode == 200) { @@ -78,8 +78,8 @@ class APIMapper { * */ Response> deserializeToList(Result result, JavaType model) throws APIMapperException { - Integer statusCode = result.getStatusCode(); - String body = result.getBody(); + Integer statusCode = result.statusCode(); + String body = result.body(); List listOfModels = null; if (statusCode == 200) { @@ -106,8 +106,8 @@ class APIMapper { * */ Response> deserializeToPaginationData(Result result, JavaType model) throws APIMapperException { - Integer statusCode = result.getStatusCode(); - String body = result.getBody(); + Integer statusCode = result.statusCode(); + String body = result.body(); PaginationData newModel = null; if (statusCode == 200) { diff --git a/src/main/java/me/goudham/Response.java b/src/main/java/me/goudham/Response.java index 906bc76..c4ca930 100644 --- a/src/main/java/me/goudham/Response.java +++ b/src/main/java/me/goudham/Response.java @@ -3,8 +3,6 @@ package me.goudham; import me.goudham.domain.series.Series; import me.goudham.domain.waifu.Waifu; -import java.util.Objects; - /** * This is returned to the User when called by methods in {@link MyWaifuClient}. * E.g {@link MyWaifuClient#getWaifu(Integer)} @@ -15,51 +13,9 @@ import java.util.Objects; * will be populated to ensure the user has all the information for debugging or extra information within * the {@link #body} * + * @param statusCode The status code returned by the API Response + * @param body The body returned by the the API Response * @param The type of model to be returned. E.g {@link Waifu} or {@link Series} * */ -public class Response { - private final T model; - private final Integer statusCode; - private final String body; - - Response(Integer statusCode, String body, T model) { - this.statusCode = statusCode; - this.body = body; - this.model = model; - } - - public T getModel() { - return model; - } - - public Integer getStatusCode() { - return statusCode; - } - - public String getBody() { - return body; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Response response = (Response) o; - return Objects.equals(model, response.model) && Objects.equals(statusCode, response.statusCode) && Objects.equals(body, response.body); - } - - @Override - public int hashCode() { - return Objects.hash(model, statusCode, body); - } - - @Override - public String toString() { - return "Response{" + - "model=" + model + - ", statusCode=" + statusCode + - ", body='" + body + '\'' + - '}'; - } -} +public record Response(Integer statusCode, String body, T model) { } diff --git a/src/main/java/me/goudham/Result.java b/src/main/java/me/goudham/Result.java index 2d8fb5e..c8710d7 100644 --- a/src/main/java/me/goudham/Result.java +++ b/src/main/java/me/goudham/Result.java @@ -1,47 +1,13 @@ package me.goudham; import java.net.http.HttpRequest; -import java.util.Objects; /** * Represents a Result from a {@link HttpRequest} with the resulting * {@code statusCode} and {@code body} + * + * @param statusCode The status code returned by the API Response + * @param body The body returned by the the API Response + * */ -class Result { - private final Integer statusCode; - private final String body; - - Result(Integer statusCode, String body) { - this.statusCode = statusCode; - this.body = body; - } - - Integer getStatusCode() { - return statusCode; - } - - String getBody() { - return body; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Result result = (Result) o; - return Objects.equals(statusCode, result.statusCode) && Objects.equals(body, result.body); - } - - @Override - public int hashCode() { - return Objects.hash(statusCode, body); - } - - @Override - public String toString() { - return "Result{" + - "statusCode=" + statusCode + - ", body='" + body + '\'' + - '}'; - } -} +record Result(Integer statusCode, String body) { } diff --git a/src/main/java/me/goudham/domain/pagination/Links.java b/src/main/java/me/goudham/domain/pagination/Links.java index 5494764..842c7cf 100644 --- a/src/main/java/me/goudham/domain/pagination/Links.java +++ b/src/main/java/me/goudham/domain/pagination/Links.java @@ -1,12 +1,8 @@ package me.goudham.domain.pagination; import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.JsonPropertyDescription; import com.fasterxml.jackson.annotation.JsonPropertyOrder; -import java.util.Objects; - /** * {@link Links} *

Contains gallery API links for {@link Meta}

@@ -27,99 +23,4 @@ import java.util.Objects; "next", "prev" }) -public class Links { - /** - * First page of the gallery - * - */ - @JsonProperty("first") - @JsonPropertyDescription("First page of the gallery") - private String first; - - /** - * Last page of the gallery - * - */ - @JsonProperty("last") - @JsonPropertyDescription("Last page of the gallery") - private String last; - - /** - * Next page of the gallery - * - */ - @JsonProperty("next") - @JsonPropertyDescription("Next page of the gallery") - private String next; - - /** - * Previous page of the gallery - * - */ - @JsonProperty("prev") - @JsonPropertyDescription("Previous page of the gallery") - private String prev; - - @JsonProperty("first") - public String getFirst() { - return first; - } - - @JsonProperty("first") - public void setFirst(String first) { - this.first = first; - } - - @JsonProperty("last") - public String getLast() { - return last; - } - - @JsonProperty("last") - public void setLast(String last) { - this.last = last; - } - - @JsonProperty("next") - public String getNext() { - return next; - } - - @JsonProperty("next") - public void setNext(String next) { - this.next = next; - } - - @JsonProperty("prev") - public String getPrev() { - return prev; - } - - @JsonProperty("prev") - public void setPrev(String prev) { - this.prev = prev; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Links links = (Links) o; - return Objects.equals(first, links.first) && Objects.equals(last, links.last) && Objects.equals(next, links.next) && Objects.equals(prev, links.prev); - } - - @Override - public int hashCode() { - return Objects.hash(first, last, next, prev); - } - - @Override - public String toString() { - return "Links{" + - "first='" + first + '\'' + - ", last='" + last + '\'' + - ", next='" + next + '\'' + - ", prev='" + prev + '\'' + - '}'; - } -} +record Links(String first, String last, String next, String prev) { } diff --git a/src/main/java/me/goudham/domain/pagination/Meta.java b/src/main/java/me/goudham/domain/pagination/Meta.java index 41479b2..dbc0def 100644 --- a/src/main/java/me/goudham/domain/pagination/Meta.java +++ b/src/main/java/me/goudham/domain/pagination/Meta.java @@ -2,11 +2,8 @@ package me.goudham.domain.pagination; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.JsonPropertyDescription; import com.fasterxml.jackson.annotation.JsonPropertyOrder; -import java.util.Objects; - /** * {@link Meta} @@ -34,144 +31,10 @@ import java.util.Objects; "to", "total" }) -public class Meta { - /** - * Current requested page - * - */ - @JsonProperty("current_page") - @JsonPropertyDescription("Current requested page") - private Integer currentPage; - - /** - * Starting gallery image number - * - */ - @JsonProperty("from") - @JsonPropertyDescription("Starting gallery image number") - private Integer from; - - /** - * Last available page - * - */ - @JsonProperty("last_page") - @JsonPropertyDescription("Last available page") - private Integer lastPage; - - /** - * API url for gallery - * - */ - @JsonProperty("path") - @JsonPropertyDescription("API url for gallery") - private String path; - - /** - * Total number of items per page - * - */ - @JsonProperty("per_page") - @JsonPropertyDescription("Total number of items per page") - private Integer perPage; - - /** - * Last gallery image number - * - */ - @JsonProperty("to") - @JsonPropertyDescription("Last gallery image number") - private Integer to; - - /** - * Total number of items within the gallery - * - */ - @JsonProperty("total") - @JsonPropertyDescription("Total number of items") - private Integer total; - - @JsonProperty("current_page") - public Integer getCurrentPage() { - return currentPage; - } - - @JsonProperty("current_page") - public void setCurrentPage(Integer currentPage) { - this.currentPage = currentPage; - } - - @JsonProperty("from") - public Integer getFrom() { return from; } - - @JsonProperty("from") - public void setFrom(Integer from) { this.from = from; } - - @JsonProperty("last_page") - public Integer getLastPage() { - return lastPage; - } - - @JsonProperty("last_page") - public void setLastPage(Integer lastPage) { - this.lastPage = lastPage; - } - - @JsonProperty("path") - public String getPath() { return path; } - - @JsonProperty("path") - public void setPath(String path) { this.path = path; } - - @JsonProperty("per_page") - public Integer getPerPage() { - return perPage; - } - - @JsonProperty("per_page") - public void setPerPage(Integer perPage) { - this.perPage = perPage; - } - - @JsonProperty("to") - public Integer getTo() { return to; } - - @JsonProperty("to") - public void setTo(Integer to) { this.to = to; } - - @JsonProperty("total") - public Integer getTotal() { - return total; - } - - @JsonProperty("total") - public void setTotal(Integer total) { - this.total = total; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Meta meta = (Meta) o; - return Objects.equals(currentPage, meta.currentPage) && Objects.equals(from, meta.from) && Objects.equals(lastPage, meta.lastPage) && Objects.equals(path, meta.path) && Objects.equals(perPage, meta.perPage) && Objects.equals(to, meta.to) && Objects.equals(total, meta.total); - } - - @Override - public int hashCode() { - return Objects.hash(currentPage, from, lastPage, path, perPage, to, total); - } - - @Override - public String toString() { - return "Meta{" + - "currentPage=" + currentPage + - ", from=" + from + - ", lastPage=" + lastPage + - ", path=" + path + - ", perPage=" + perPage + - ", to=" + to + - ", total=" + total + - '}'; - } -} +record Meta(@JsonProperty("current_page") String currentPage, + Integer from, + @JsonProperty("last_page") Integer lastPage, + String path, + @JsonProperty("per_page") Integer perPage, + Integer to, + Integer total) { } diff --git a/src/main/java/me/goudham/domain/pagination/PaginationData.java b/src/main/java/me/goudham/domain/pagination/PaginationData.java index c5ea94a..c497d4d 100644 --- a/src/main/java/me/goudham/domain/pagination/PaginationData.java +++ b/src/main/java/me/goudham/domain/pagination/PaginationData.java @@ -1,12 +1,9 @@ package me.goudham.domain.pagination; import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.JsonPropertyDescription; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import java.util.List; -import java.util.Objects; /** * {@link PaginationData} @@ -26,81 +23,4 @@ import java.util.Objects; "links", "meta" }) -public class PaginationData { - - /** - * Data returned from the gallery - * - */ - @JsonProperty("data") - @JsonPropertyDescription("Data returned from the gallery") - private List data; - - /** - * {@link Links} to other data within the gallery - * - */ - @JsonProperty("links") - @JsonPropertyDescription("Links to other data within the gallery") - private Links links; - - /** - * Extra pagination information - * - */ - @JsonProperty("meta") - @JsonPropertyDescription("Extra pagination information") - private Meta meta; - - @JsonProperty("data") - public List getData() { - return data; - } - - @JsonProperty("data") - public void setData(List data) { - this.data = data; - } - - @JsonProperty("links") - public Links getLinks() { - return links; - } - - @JsonProperty("links") - public void setLinks(Links links) { - this.links = links; - } - - @JsonProperty("meta") - public Meta getMeta() { - return meta; - } - - @JsonProperty("meta") - public void setMeta(Meta meta) { - this.meta = meta; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - PaginationData that = (PaginationData) o; - return Objects.equals(data, that.data) && Objects.equals(links, that.links) && Objects.equals(meta, that.meta); - } - - @Override - public int hashCode() { - return Objects.hash(data, links, meta); - } - - @Override - public String toString() { - return "PaginationData{" + - "data=" + data + - ", links=" + links + - ", meta=" + meta + - '}'; - } -} \ No newline at end of file +public record PaginationData(List data, Links links, Meta meta) { } \ No newline at end of file diff --git a/src/main/java/me/goudham/exception/APIMapperException.java b/src/main/java/me/goudham/exception/APIMapperException.java index 9e5dd04..5c76366 100644 --- a/src/main/java/me/goudham/exception/APIMapperException.java +++ b/src/main/java/me/goudham/exception/APIMapperException.java @@ -4,7 +4,7 @@ import me.goudham.APIWrapper; import me.goudham.Response; /** - * Thrown when {@link APIWrapper} fails to deserialize json into Java POJO's ({@link Response#getModel()}) + * Thrown when {@link APIWrapper} fails to deserialize json into Java POJO's ({@link Response#model()}) * */ public class APIMapperException extends Throwable { diff --git a/src/test/java/me/goudham/APIWrapperTest.java b/src/test/java/me/goudham/APIWrapperTest.java index 22fd00b..de3094d 100644 --- a/src/test/java/me/goudham/APIWrapperTest.java +++ b/src/test/java/me/goudham/APIWrapperTest.java @@ -46,8 +46,8 @@ class APIWrapperTest { sut.setApiKey("InvalidAPIKey"); Result actualResult = sut.sendGetRequest("waifu/1"); - assertThat(actualResult.getStatusCode(), is(expectedStatusCode)); - assertThat(actualResult.getBody(), is(expectedBody)); + assertThat(actualResult.statusCode(), is(expectedStatusCode)); + assertThat(actualResult.body(), is(expectedBody)); verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString()); verifyNoMoreInteractions(httpClient); } @@ -63,8 +63,8 @@ class APIWrapperTest { Result actualResult = sut.sendGetRequest("waifu/1"); - assertThat(actualResult.getStatusCode(), is(expectedStatusCode)); - assertThat(actualResult.getBody(), is(expectedBody)); + assertThat(actualResult.statusCode(), is(expectedStatusCode)); + assertThat(actualResult.body(), is(expectedBody)); verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString()); verifyNoMoreInteractions(httpClient); }