From e74da4ed8aca02cffa8ad72143515f9390ac8f87 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Mon, 2 Oct 2023 01:03:33 -0400 Subject: [PATCH] Update the Post Object for use to get called --- .../discourse/client/DiscourseClient.java | 3 +- .../client/service/PostsService.java | 58 ++++ .../{TopicService.java => TopicsService.java} | 6 +- .../client/test/PostsServiceTest.java | 37 +++ .../ossez/discourse/client/test/TestBase.java | 10 + .../client/test/TopicsServiceTest.java | 6 +- .../discourse/common/model/dto/Post.java | 299 ++++++++++++++++++ 7 files changed, 412 insertions(+), 7 deletions(-) create mode 100644 client/src/main/java/com/ossez/discourse/client/service/PostsService.java rename client/src/main/java/com/ossez/discourse/client/service/{TopicService.java => TopicsService.java} (88%) create mode 100644 client/src/test/java/com/ossez/discourse/client/test/PostsServiceTest.java create mode 100644 common/src/main/java/com/ossez/discourse/common/model/dto/Post.java diff --git a/client/src/main/java/com/ossez/discourse/client/DiscourseClient.java b/client/src/main/java/com/ossez/discourse/client/DiscourseClient.java index 497b60b..2da05ab 100644 --- a/client/src/main/java/com/ossez/discourse/client/DiscourseClient.java +++ b/client/src/main/java/com/ossez/discourse/client/DiscourseClient.java @@ -1,6 +1,7 @@ package com.ossez.discourse.client; +import com.fasterxml.jackson.databind.ObjectMapper; import okhttp3.HttpUrl; import okhttp3.Request; @@ -11,7 +12,7 @@ public abstract class DiscourseClient { public static String site_url = "https://www.zchub.net/";// base url. e.g. http://your_discourse_domain.com public static String api_key = ""; public static String api_username = ""; - + public ObjectMapper objectMapper = new ObjectMapper(); public Request getRequest(String path) { HttpUrl.Builder urlBuilder = HttpUrl.parse(site_url + path).newBuilder(); Request request = new Request.Builder().url(urlBuilder.build().toString()) diff --git a/client/src/main/java/com/ossez/discourse/client/service/PostsService.java b/client/src/main/java/com/ossez/discourse/client/service/PostsService.java new file mode 100644 index 0000000..8a82e09 --- /dev/null +++ b/client/src/main/java/com/ossez/discourse/client/service/PostsService.java @@ -0,0 +1,58 @@ +package com.ossez.discourse.client.service; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.ossez.discourse.client.DiscourseClient; +import com.ossez.discourse.common.model.dto.Post; +import com.ossez.discourse.common.model.dto.Topics; +import okhttp3.OkHttpClient; +import okhttp3.Response; +import org.apache.http.HttpStatus; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.Optional; + +/** + * WeChat Official Account Platform related Service + * + * @author YuCheng + */ +public class PostsService extends DiscourseClient { + private final Logger log = LoggerFactory.getLogger(PostsService.class); + + private WeChatOfficialAccountApi weChatOfficialAccountApi; + + private OkHttpClient client = new OkHttpClient(); + + public PostsService(String siteUrl, String apiName, String apiKey) { + DiscourseClient.site_url = siteUrl; + DiscourseClient.api_username = apiName; + DiscourseClient.api_key = apiKey; + } + + /** + * Get Post Object by search postId + * + * @param postId + * @return + */ + public Optional getPost(Long postId) { + String path = "posts/" + String.valueOf(postId) + ".json"; + + Optional post = Optional.ofNullable(new Post()); + try { + Response response = client.newCall(getRequest(path)).execute(); + String responseStr = response.body().string(); + + if (response.code() == HttpStatus.SC_OK) { + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + post = Optional.of(objectMapper.readValue(responseStr, Post.class)); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + return post; + } +} diff --git a/client/src/main/java/com/ossez/discourse/client/service/TopicService.java b/client/src/main/java/com/ossez/discourse/client/service/TopicsService.java similarity index 88% rename from client/src/main/java/com/ossez/discourse/client/service/TopicService.java rename to client/src/main/java/com/ossez/discourse/client/service/TopicsService.java index 7679cb6..99e955a 100644 --- a/client/src/main/java/com/ossez/discourse/client/service/TopicService.java +++ b/client/src/main/java/com/ossez/discourse/client/service/TopicsService.java @@ -17,14 +17,14 @@ import java.util.Optional; * * @author YuCheng */ -public class TopicService extends DiscourseClient { - private final Logger log = LoggerFactory.getLogger(TopicService.class); +public class TopicsService extends DiscourseClient { + private final Logger log = LoggerFactory.getLogger(TopicsService.class); private WeChatOfficialAccountApi weChatOfficialAccountApi; private OkHttpClient client = new OkHttpClient(); - public TopicService(String siteUrl, String apiName, String apiKey) { + public TopicsService(String siteUrl, String apiName, String apiKey) { DiscourseClient.site_url = siteUrl; DiscourseClient.api_username = apiName; DiscourseClient.api_key = apiKey; diff --git a/client/src/test/java/com/ossez/discourse/client/test/PostsServiceTest.java b/client/src/test/java/com/ossez/discourse/client/test/PostsServiceTest.java new file mode 100644 index 0000000..554fe6e --- /dev/null +++ b/client/src/test/java/com/ossez/discourse/client/test/PostsServiceTest.java @@ -0,0 +1,37 @@ +package com.ossez.discourse.client.test; + +import com.google.inject.Inject; +import com.ossez.discourse.client.service.PostsService; +import com.ossez.discourse.client.service.TopicsService; +import com.ossez.discourse.common.exception.WxErrorException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Test for datacube API + * + * @author YuCheng + */ +@TestInstance(Lifecycle.PER_CLASS) +public class PostsServiceTest extends TestBase { + private static final Logger log = LoggerFactory.getLogger(PostsServiceTest.class); + @Inject + protected PostsService postsService; + + /** + * Test Create Menu + * + * @throws WxErrorException + */ + @Test + public void testCreate() throws WxErrorException { + log.debug("Create WeChat Offical Account Menun Test"); + log.debug("{}", postsService.getPost(Long.valueOf("1245")).get().getRaw()); + + + } + +} diff --git a/client/src/test/java/com/ossez/discourse/client/test/TestBase.java b/client/src/test/java/com/ossez/discourse/client/test/TestBase.java index 10c5234..63c0ec3 100644 --- a/client/src/test/java/com/ossez/discourse/client/test/TestBase.java +++ b/client/src/test/java/com/ossez/discourse/client/test/TestBase.java @@ -4,6 +4,8 @@ import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Injector; import com.ossez.discourse.client.DiscourseClient; +import com.ossez.discourse.client.service.PostsService; +import com.ossez.discourse.client.service.TopicsService; import org.apache.commons.lang3.ObjectUtils; import org.junit.jupiter.api.BeforeAll; import org.slf4j.Logger; @@ -43,6 +45,14 @@ public class TestBase { DiscourseClient.api_username = prop.getProperty("api.username"); DiscourseClient.api_key = prop.getProperty("api.key"); + // TopicsService + TopicsService topicsService = new TopicsService(DiscourseClient.site_url, DiscourseClient.api_username, DiscourseClient.api_key); + bind(TopicsService.class).toInstance(topicsService); + + // TopicsService + PostsService postsService = new PostsService(DiscourseClient.site_url, DiscourseClient.api_username, DiscourseClient.api_key); + bind(PostsService.class).toInstance(postsService); + // Init WeChat config for testing // Document document = new SAXReader().read(inputStream); // TestConfigStorage config = new TestConfigStorage(); diff --git a/client/src/test/java/com/ossez/discourse/client/test/TopicsServiceTest.java b/client/src/test/java/com/ossez/discourse/client/test/TopicsServiceTest.java index ce0c8b9..82304cd 100644 --- a/client/src/test/java/com/ossez/discourse/client/test/TopicsServiceTest.java +++ b/client/src/test/java/com/ossez/discourse/client/test/TopicsServiceTest.java @@ -1,7 +1,7 @@ package com.ossez.discourse.client.test; import com.google.inject.Inject; -import com.ossez.discourse.client.service.TopicService; +import com.ossez.discourse.client.service.TopicsService; import com.ossez.discourse.common.exception.WxErrorException; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; @@ -18,7 +18,7 @@ import org.slf4j.LoggerFactory; public class TopicsServiceTest extends TestBase { private static final Logger log = LoggerFactory.getLogger(TopicsServiceTest.class); @Inject - protected TopicService topicService; + protected TopicsService topicsService; /** * Test Create Menu @@ -28,7 +28,7 @@ public class TopicsServiceTest extends TestBase { @Test public void testCreate() throws WxErrorException { log.debug("Create WeChat Offical Account Menun Test"); - log.debug("{}", topicService.getTopic(Long.valueOf("1245")).get().getTitle()); + log.debug("{}", topicsService.getTopic(Long.valueOf("1245")).get().getTitle()); } diff --git a/common/src/main/java/com/ossez/discourse/common/model/dto/Post.java b/common/src/main/java/com/ossez/discourse/common/model/dto/Post.java new file mode 100644 index 0000000..261c19e --- /dev/null +++ b/common/src/main/java/com/ossez/discourse/common/model/dto/Post.java @@ -0,0 +1,299 @@ +package com.ossez.discourse.common.model.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class Post { + + @JsonProperty(value = "id", required = true) + private Long id; + @JsonProperty(value = "username") + private String userName; + private String avatarTemplate; + private String createdAt; + private String cooked; + private String postNumber; + private String postType; + private String updatedAt; + private Integer replyCount; + private String replyToPostNumber; + private Integer quoteCount; + private Integer incomingLinkCount; + private Integer reads; + private Integer readersCount; + private String score; + private Boolean yours; + private Long topicId; + private String topicSlug; + private String primaryGroupName; + private String flairName; + private String flairUrl; + private String flairBgColor; + private String flairGroupId; + private Integer version; + private Boolean canEdit; + private Boolean canDelete; + private Boolean canRecover; + private Boolean canSeeHiddenPost; + private Boolean canWiki; + private String userTitle; + private Boolean bookmarked; + private String raw; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getAvatarTemplate() { + return avatarTemplate; + } + + public void setAvatarTemplate(String avatarTemplate) { + this.avatarTemplate = avatarTemplate; + } + + public String getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(String createdAt) { + this.createdAt = createdAt; + } + + public String getCooked() { + return cooked; + } + + public void setCooked(String cooked) { + this.cooked = cooked; + } + + public String getPostNumber() { + return postNumber; + } + + public void setPostNumber(String postNumber) { + this.postNumber = postNumber; + } + + public String getPostType() { + return postType; + } + + public void setPostType(String postType) { + this.postType = postType; + } + + public String getUpdatedAt() { + return updatedAt; + } + + public void setUpdatedAt(String updatedAt) { + this.updatedAt = updatedAt; + } + + public Integer getReplyCount() { + return replyCount; + } + + public void setReplyCount(Integer replyCount) { + this.replyCount = replyCount; + } + + public String getReplyToPostNumber() { + return replyToPostNumber; + } + + public void setReplyToPostNumber(String replyToPostNumber) { + this.replyToPostNumber = replyToPostNumber; + } + + public Integer getQuoteCount() { + return quoteCount; + } + + public void setQuoteCount(Integer quoteCount) { + this.quoteCount = quoteCount; + } + + public Integer getIncomingLinkCount() { + return incomingLinkCount; + } + + public void setIncomingLinkCount(Integer incomingLinkCount) { + this.incomingLinkCount = incomingLinkCount; + } + + public Integer getReads() { + return reads; + } + + public void setReads(Integer reads) { + this.reads = reads; + } + + public Integer getReadersCount() { + return readersCount; + } + + public void setReadersCount(Integer readersCount) { + this.readersCount = readersCount; + } + + public String getScore() { + return score; + } + + public void setScore(String score) { + this.score = score; + } + + public Boolean getYours() { + return yours; + } + + public void setYours(Boolean yours) { + this.yours = yours; + } + + public Long getTopicId() { + return topicId; + } + + public void setTopicId(Long topicId) { + this.topicId = topicId; + } + + public String getTopicSlug() { + return topicSlug; + } + + public void setTopicSlug(String topicSlug) { + this.topicSlug = topicSlug; + } + + public String getPrimaryGroupName() { + return primaryGroupName; + } + + public void setPrimaryGroupName(String primaryGroupName) { + this.primaryGroupName = primaryGroupName; + } + + public String getFlairName() { + return flairName; + } + + public void setFlairName(String flairName) { + this.flairName = flairName; + } + + public String getFlairUrl() { + return flairUrl; + } + + public void setFlairUrl(String flairUrl) { + this.flairUrl = flairUrl; + } + + public String getFlairBgColor() { + return flairBgColor; + } + + public void setFlairBgColor(String flairBgColor) { + this.flairBgColor = flairBgColor; + } + + public String getFlairGroupId() { + return flairGroupId; + } + + public void setFlairGroupId(String flairGroupId) { + this.flairGroupId = flairGroupId; + } + + public Integer getVersion() { + return version; + } + + public void setVersion(Integer version) { + this.version = version; + } + + public Boolean getCanEdit() { + return canEdit; + } + + public void setCanEdit(Boolean canEdit) { + this.canEdit = canEdit; + } + + public Boolean getCanDelete() { + return canDelete; + } + + public void setCanDelete(Boolean canDelete) { + this.canDelete = canDelete; + } + + public Boolean getCanRecover() { + return canRecover; + } + + public void setCanRecover(Boolean canRecover) { + this.canRecover = canRecover; + } + + public Boolean getCanSeeHiddenPost() { + return canSeeHiddenPost; + } + + public void setCanSeeHiddenPost(Boolean canSeeHiddenPost) { + this.canSeeHiddenPost = canSeeHiddenPost; + } + + public Boolean getCanWiki() { + return canWiki; + } + + public void setCanWiki(Boolean canWiki) { + this.canWiki = canWiki; + } + + public String getUserTitle() { + return userTitle; + } + + public void setUserTitle(String userTitle) { + this.userTitle = userTitle; + } + + public Boolean getBookmarked() { + return bookmarked; + } + + public void setBookmarked(Boolean bookmarked) { + this.bookmarked = bookmarked; + } + + public String getRaw() { + return raw; + } + + public void setRaw(String raw) { + this.raw = raw; + } +} + +