From 65689a7ee5ca91295480435e73e1b485258fd1f7 Mon Sep 17 00:00:00 2001 From: Hammy Date: Sat, 5 Jun 2021 20:12:36 +0100 Subject: [PATCH 1/4] Add another POJO for JSON Entity The getWaifu endpoint has a creator {} block that wasn't documented in the API Reference --- .../goudham/me/api/entity/user/Creator.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/main/java/org/goudham/me/api/entity/user/Creator.java diff --git a/src/main/java/org/goudham/me/api/entity/user/Creator.java b/src/main/java/org/goudham/me/api/entity/user/Creator.java new file mode 100644 index 0000000..5b42b91 --- /dev/null +++ b/src/main/java/org/goudham/me/api/entity/user/Creator.java @@ -0,0 +1,88 @@ +package org.goudham.me.api.entity.user; + +import javax.annotation.processing.Generated; + +import com.fasterxml.jackson.annotation.JsonPropertyDescription; +import org.goudham.me.api.entity.waifu.Waifu; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +import java.util.Objects; + +/** + * {@link Creator} + *

User that submitted the {@link Waifu}

+ * + *

Fields included are:

+ * + * + */ +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ + "id", + "name" +}) +@Generated("jsonschema2pojo") +public class Creator { + + /** + * {@link Creator} ID + * + */ + @JsonProperty("id") + @JsonPropertyDescription("") + private Integer id; + + /** + * {@link Creator} Name + */ + @JsonProperty("name") + @JsonPropertyDescription("") + private String name; + + @JsonProperty("id") + public Integer getId() { + return id; + } + + @JsonProperty("id") + public void setId(Integer id) { + this.id = id; + } + + @JsonProperty("name") + public String getName() { + return name; + } + + @JsonProperty("name") + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Creator creator = (Creator) o; + return Objects.equals(id, creator.id) && Objects.equals(name, creator.name); + } + + @Override + public int hashCode() { + return Objects.hash(id, name); + } + + @Override + public String toString() { + return "Creator{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } +} \ No newline at end of file From 69f4dab38e2afc1bf2d2203e2bf1e93a83404864 Mon Sep 17 00:00:00 2001 From: Hammy Date: Sat, 5 Jun 2021 20:14:45 +0100 Subject: [PATCH 2/4] Update POJO's to better reflect API The getWaifu endpoint disappointingly diverts from the API reference. Therefore changes to field names have been applied --- .../goudham/me/api/entity/series/Series.java | 30 ++++----- .../goudham/me/api/entity/waifu/Waifu.java | 65 ++++++++++++------- 2 files changed, 56 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/goudham/me/api/entity/series/Series.java b/src/main/java/org/goudham/me/api/entity/series/Series.java index 632a182..38dc544 100644 --- a/src/main/java/org/goudham/me/api/entity/series/Series.java +++ b/src/main/java/org/goudham/me/api/entity/series/Series.java @@ -2,10 +2,7 @@ package org.goudham.me.api.entity.series; import javax.annotation.processing.Generated; -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 com.fasterxml.jackson.annotation.*; import org.goudham.me.api.entity.Studio; import org.goudham.me.api.entity.waifu.Waifu; @@ -20,12 +17,13 @@ import java.util.Objects; *
  • {@link String name}
  • *
  • {@link String originalName}
  • *
  • {@link String romajiName}
  • + *
  • {@link String description}
  • *
  • {@link String slug}
  • *
  • {@link String releaseDate}
  • *
  • {@link String airingStart}
  • *
  • {@link String airingEnd}
  • *
  • {@link Integer episodeCount}
  • - *
  • {@link String image}
  • + *
  • {@link String displayPicture}
  • *
  • {@link String url}
  • *
  • {@link Studio}
  • *
  • {@link Integer id}
  • @@ -43,7 +41,7 @@ import java.util.Objects; "airing_start", "airing_end", "episode_count", - "image", + "display_picture", "url", "studio", "id" @@ -95,6 +93,7 @@ public class Series { * */ @JsonProperty("release_date") + @JsonAlias("release") @JsonPropertyDescription("Original works release date (") private String releaseDate; @@ -126,9 +125,10 @@ public class Series { * URL of the display picture * */ - @JsonProperty("image") + @JsonProperty("display_picture") + @JsonAlias("image") @JsonPropertyDescription("URL of the display picture") - private String image; + private String displayPicture; /** * URL of the {@link Series} @@ -243,13 +243,13 @@ public class Series { } @JsonProperty("image") - public String getImage() { - return image; + public String getDisplayPicture() { + return displayPicture; } @JsonProperty("image") - public void setImage(String image) { - this.image = image; + public void setDisplayPicture(String displayPicture) { + this.displayPicture = displayPicture; } @JsonProperty("url") @@ -287,12 +287,12 @@ public class Series { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Series series = (Series) o; - return Objects.equals(name, series.name) && Objects.equals(originalName, series.originalName) && Objects.equals(romajiName, series.romajiName) && Objects.equals(description, series.description) && Objects.equals(slug, series.slug) && Objects.equals(releaseDate, series.releaseDate) && Objects.equals(airingStart, series.airingStart) && Objects.equals(airingEnd, series.airingEnd) && Objects.equals(episodeCount, series.episodeCount) && Objects.equals(image, series.image) && Objects.equals(url, series.url) && Objects.equals(studio, series.studio) && Objects.equals(id, series.id); + return Objects.equals(name, series.name) && Objects.equals(originalName, series.originalName) && Objects.equals(romajiName, series.romajiName) && Objects.equals(description, series.description) && Objects.equals(slug, series.slug) && Objects.equals(releaseDate, series.releaseDate) && Objects.equals(airingStart, series.airingStart) && Objects.equals(airingEnd, series.airingEnd) && Objects.equals(episodeCount, series.episodeCount) && Objects.equals(displayPicture, series.displayPicture) && Objects.equals(url, series.url) && Objects.equals(studio, series.studio) && Objects.equals(id, series.id); } @Override public int hashCode() { - return Objects.hash(name, originalName, romajiName, description, slug, releaseDate, airingStart, airingEnd, episodeCount, image, url, studio, id); + return Objects.hash(name, originalName, romajiName, description, slug, releaseDate, airingStart, airingEnd, episodeCount, displayPicture, url, studio, id); } @Override @@ -307,7 +307,7 @@ public class Series { ", airingStart='" + airingStart + '\'' + ", airingEnd='" + airingEnd + '\'' + ", episodeCount=" + episodeCount + - ", image='" + image + '\'' + + ", image='" + displayPicture + '\'' + ", url='" + url + '\'' + ", studio=" + studio + ", id=" + id + diff --git a/src/main/java/org/goudham/me/api/entity/waifu/Waifu.java b/src/main/java/org/goudham/me/api/entity/waifu/Waifu.java index 716fd07..e80b849 100644 --- a/src/main/java/org/goudham/me/api/entity/waifu/Waifu.java +++ b/src/main/java/org/goudham/me/api/entity/waifu/Waifu.java @@ -6,13 +6,9 @@ import java.util.Map; import java.util.Objects; import javax.annotation.processing.Generated; -import com.fasterxml.jackson.annotation.JsonCreator; -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 com.fasterxml.jackson.annotation.JsonValue; +import com.fasterxml.jackson.annotation.*; import org.goudham.me.api.entity.series.Series; +import org.goudham.me.api.entity.user.Creator; /** @@ -23,9 +19,10 @@ import org.goudham.me.api.entity.series.Series; * * @@ -56,9 +53,10 @@ import org.goudham.me.api.entity.series.Series; @JsonPropertyOrder({ "id", "slug", - "creator_id", + "creator", "name", "original_name", + "romaji_name", "display_picture", "description", "weight", @@ -84,6 +82,7 @@ import org.goudham.me.api.entity.series.Series; "series" }) @Generated("jsonschema2pojo") +@JsonIgnoreProperties(ignoreUnknown = true) public class Waifu { /** * {@link Waifu} ID @@ -105,9 +104,9 @@ public class Waifu { * ID of the user who created this {@link Waifu} * */ - @JsonProperty("creator_id") - @JsonPropertyDescription("ID of the user who created this Waifu") - private Integer creatorId; + @JsonProperty("creator") + @JsonPropertyDescription("User who created this Waifu") + private Creator creator; /** * Full name, in English. @@ -125,6 +124,14 @@ public class Waifu { @JsonPropertyDescription("Name in the original language (\u65e5\u672c\u8a9e)") private String originalName; + /** + * If this character has a romaji name, we'll put it here. + * + */ + @JsonProperty("romaji_name") + @JsonPropertyDescription("if this character has a romaji name, we'll put it here. ") + private String romajiName; + /** * URL of the display picture * @@ -299,7 +306,7 @@ public class Waifu { */ @JsonProperty("appearances") @JsonPropertyDescription("An array of series or works that this character appears in") - private List appearances = null; + private List appearances; /** * {@link Series} @@ -330,14 +337,14 @@ public class Waifu { this.slug = slug; } - @JsonProperty("creator_id") - public Integer getCreatorId() { - return creatorId; + @JsonProperty("creator") + public Creator getCreator() { + return creator; } - @JsonProperty("creator_id") - public void setCreatorId(Integer creatorId) { - this.creatorId = creatorId; + @JsonProperty("creator") + public void setCreator(Creator creator) { + this.creator = creator; } @JsonProperty("name") @@ -360,6 +367,16 @@ public class Waifu { this.originalName = originalName; } + @JsonProperty("romaji_name") + public String getRomajiName() { + return romajiName; + } + + @JsonProperty("romaji_name") + public void setRomajiName(String romajiName) { + this.romajiName = romajiName; + } + @JsonProperty("display_picture") public String getDisplayPicture() { return displayPicture; @@ -571,12 +588,12 @@ public class Waifu { } @JsonProperty("appearances") - public List getAppearances() { + public List getAppearances() { return appearances; } @JsonProperty("appearances") - public void setAppearances(List appearances) { + public void setAppearances(List appearances) { this.appearances = appearances; } @@ -595,12 +612,12 @@ public class Waifu { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Waifu waifu = (Waifu) o; - return Objects.equals(id, waifu.id) && Objects.equals(slug, waifu.slug) && Objects.equals(creatorId, waifu.creatorId) && Objects.equals(name, waifu.name) && Objects.equals(originalName, waifu.originalName) && Objects.equals(displayPicture, waifu.displayPicture) && Objects.equals(description, waifu.description) && Objects.equals(weight, waifu.weight) && Objects.equals(height, waifu.height) && Objects.equals(bust, waifu.bust) && Objects.equals(hip, waifu.hip) && Objects.equals(waist, waifu.waist) && bloodType == waifu.bloodType && Objects.equals(origin, waifu.origin) && Objects.equals(age, waifu.age) && Objects.equals(birthdayMonth, waifu.birthdayMonth) && Objects.equals(birthdayDay, waifu.birthdayDay) && Objects.equals(birthdayYear, waifu.birthdayYear) && Objects.equals(likes, waifu.likes) && Objects.equals(trash, waifu.trash) && Objects.equals(url, waifu.url) && Objects.equals(husbando, waifu.husbando) && Objects.equals(nsfw, waifu.nsfw) && Objects.equals(popularityRank, waifu.popularityRank) && Objects.equals(likeRank, waifu.likeRank) && Objects.equals(trashRank, waifu.trashRank) && Objects.equals(appearances, waifu.appearances) && Objects.equals(series, waifu.series); + return Objects.equals(id, waifu.id) && Objects.equals(slug, waifu.slug) && Objects.equals(creator, waifu.creator) && Objects.equals(name, waifu.name) && Objects.equals(originalName, waifu.originalName) && Objects.equals(displayPicture, waifu.displayPicture) && Objects.equals(description, waifu.description) && Objects.equals(weight, waifu.weight) && Objects.equals(height, waifu.height) && Objects.equals(bust, waifu.bust) && Objects.equals(hip, waifu.hip) && Objects.equals(waist, waifu.waist) && bloodType == waifu.bloodType && Objects.equals(origin, waifu.origin) && Objects.equals(age, waifu.age) && Objects.equals(birthdayMonth, waifu.birthdayMonth) && Objects.equals(birthdayDay, waifu.birthdayDay) && Objects.equals(birthdayYear, waifu.birthdayYear) && Objects.equals(likes, waifu.likes) && Objects.equals(trash, waifu.trash) && Objects.equals(url, waifu.url) && Objects.equals(husbando, waifu.husbando) && Objects.equals(nsfw, waifu.nsfw) && Objects.equals(popularityRank, waifu.popularityRank) && Objects.equals(likeRank, waifu.likeRank) && Objects.equals(trashRank, waifu.trashRank) && Objects.equals(appearances, waifu.appearances) && Objects.equals(series, waifu.series); } @Override public int hashCode() { - return Objects.hash(id, slug, creatorId, name, originalName, displayPicture, description, weight, height, bust, hip, waist, bloodType, origin, age, birthdayMonth, birthdayDay, birthdayYear, likes, trash, url, husbando, nsfw, popularityRank, likeRank, trashRank, appearances, series); + return Objects.hash(id, slug, creator, name, originalName, displayPicture, description, weight, height, bust, hip, waist, bloodType, origin, age, birthdayMonth, birthdayDay, birthdayYear, likes, trash, url, husbando, nsfw, popularityRank, likeRank, trashRank, appearances, series); } @Override @@ -608,7 +625,7 @@ public class Waifu { return "Waifu{" + "id=" + id + ", slug='" + slug + '\'' + - ", creatorId=" + creatorId + + ", creatorId=" + creator + ", name='" + name + '\'' + ", originalName='" + originalName + '\'' + ", displayPicture='" + displayPicture + '\'' + From 696115535b14727908a2b7d4750491f9d9145df8 Mon Sep 17 00:00:00 2001 From: Hammy Date: Sat, 5 Jun 2021 22:23:42 +0100 Subject: [PATCH 3/4] Remove equivalent print() statements --- Jenkinsfile | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 94692d0..f45816b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -36,10 +36,6 @@ pipeline { pom = readMavenPom file: "pom.xml"; filesByGlob = findFiles(glob: "target/*.${pom.packaging}"); - echo "${filesByGlob[0].name} ${filesByGlob[0].path} ${filesByGlob[0].directory} ${filesByGlob[0].length} ${filesByGlob[0].lastModified}" - echo "${filesByGlob[1].name} ${filesByGlob[1].path} ${filesByGlob[1].directory} ${filesByGlob[1].length} ${filesByGlob[1].lastModified}" - echo "${filesByGlob[2].name} ${filesByGlob[2].path} ${filesByGlob[2].directory} ${filesByGlob[2].length} ${filesByGlob[2].lastModified}" - javadocsArtifact = filesByGlob[0].path; jarWithSourcesArtifact = filesByGlob[1].path; jarArtifact = filesByGlob[2].path; From d734e76db85fc5140ad16b6e4f34c79b149dc85d Mon Sep 17 00:00:00 2001 From: Hammy Date: Sat, 5 Jun 2021 23:29:31 +0100 Subject: [PATCH 4/4] (Unfinished) Testing API Retrieval --- .../java/org/goudham/me/MyWaifuClient.java | 10 +++- .../java/org/goudham/me/MyWaifuWrapper.java | 55 ++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/goudham/me/MyWaifuClient.java b/src/main/java/org/goudham/me/MyWaifuClient.java index bc174e3..9fde196 100644 --- a/src/main/java/org/goudham/me/MyWaifuClient.java +++ b/src/main/java/org/goudham/me/MyWaifuClient.java @@ -26,7 +26,7 @@ public class MyWaifuClient { *

    See MyWaifuList for obtaining an API Key

    * @param apiKey API Key to authorise API request */ - public MyWaifuClient(@NotNull String apiKey) { + MyWaifuClient(@NotNull String apiKey) { myWaifuWrapper = new MyWaifuWrapper(apiKey); } @@ -47,6 +47,14 @@ public class MyWaifuClient { return myWaifuClient; } + public void getWaifu(String slug) { + myWaifuWrapper.getWaifu(httpClient, slug); + } + + public void getWaifu(Integer id) { + myWaifuWrapper.getWaifu(httpClient, String.valueOf(id)); + } + /** * Sets an instance of HttpClient * diff --git a/src/main/java/org/goudham/me/MyWaifuWrapper.java b/src/main/java/org/goudham/me/MyWaifuWrapper.java index f116755..15dfa4c 100644 --- a/src/main/java/org/goudham/me/MyWaifuWrapper.java +++ b/src/main/java/org/goudham/me/MyWaifuWrapper.java @@ -1,13 +1,25 @@ package org.goudham.me; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Duration; +import java.util.concurrent.CompletableFuture; + + /** * Returns API information to {@link MyWaifuClient} */ class MyWaifuWrapper { private final String version = "1.0"; - private final String host = "https://mywaifulist.moe/api/v1/"; + private static final String host = "https://mywaifulist.moe/api/v1/"; private final String apiKey; + private final ObjectMapper objectMapper = new ObjectMapper(); + /** * Instantiates an instance of {@link MyWaifuWrapper} to retrieve API Information * @param apiKey API Key to authorise API request @@ -15,4 +27,45 @@ class MyWaifuWrapper { MyWaifuWrapper(String apiKey) { this.apiKey = apiKey; } + + private void sendRequest(HttpClient httpClient, String param) { + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(host + param)) + .version(httpClient.version()) + .timeout(Duration.ofSeconds(30)) + .header("Content-Type", "application/json") + .header("apikey", apiKey) + .GET() + .build(); + + CompletableFuture> response = httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString()); + + System.out.println(response); + +// String resultBody = ""; +// int resultStatusCode = 0; +// try { +// resultBody = response.thenApply(HttpResponse::body).get(); +// resultStatusCode = response.thenApply(HttpResponse::statusCode).get(); +// +// } catch (InterruptedException | ExecutionException e) { +// e.printStackTrace(); +// } +// +// System.out.println(resultStatusCode); +// System.out.println(resultBody); +// +// try { +// JsonNode parent = objectMapper.readTree(resultBody); +// String waifuData = parent.get("data").toString(); +// Waifu waifu = objectMapper.readValue(waifuData, Waifu.class); +// System.out.println(waifu); +// } catch (JsonProcessingException e) { +// e.printStackTrace(); +// } + } + + void getWaifu(HttpClient httpClient, String param) { + sendRequest(httpClient, "waifu/" + param); + } }