Update the Post Object for use to get called

This commit is contained in:
YuCheng Hu 2023-10-02 01:03:33 -04:00
parent d4b35c4400
commit e74da4ed8a
7 changed files with 412 additions and 7 deletions

View File

@ -1,6 +1,7 @@
package com.ossez.discourse.client; package com.ossez.discourse.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.Request; 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 site_url = "https://www.zchub.net/";// base url. e.g. http://your_discourse_domain.com
public static String api_key = ""; public static String api_key = "";
public static String api_username = ""; public static String api_username = "";
public ObjectMapper objectMapper = new ObjectMapper();
public Request getRequest(String path) { public Request getRequest(String path) {
HttpUrl.Builder urlBuilder = HttpUrl.parse(site_url + path).newBuilder(); HttpUrl.Builder urlBuilder = HttpUrl.parse(site_url + path).newBuilder();
Request request = new Request.Builder().url(urlBuilder.build().toString()) Request request = new Request.Builder().url(urlBuilder.build().toString())

View File

@ -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<Post> getPost(Long postId) {
String path = "posts/" + String.valueOf(postId) + ".json";
Optional<Post> 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;
}
}

View File

@ -17,14 +17,14 @@ import java.util.Optional;
* *
* @author YuCheng * @author YuCheng
*/ */
public class TopicService extends DiscourseClient { public class TopicsService extends DiscourseClient {
private final Logger log = LoggerFactory.getLogger(TopicService.class); private final Logger log = LoggerFactory.getLogger(TopicsService.class);
private WeChatOfficialAccountApi weChatOfficialAccountApi; private WeChatOfficialAccountApi weChatOfficialAccountApi;
private OkHttpClient client = new OkHttpClient(); 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.site_url = siteUrl;
DiscourseClient.api_username = apiName; DiscourseClient.api_username = apiName;
DiscourseClient.api_key = apiKey; DiscourseClient.api_key = apiKey;

View File

@ -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());
}
}

View File

@ -4,6 +4,8 @@ import com.google.inject.AbstractModule;
import com.google.inject.Guice; import com.google.inject.Guice;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.ossez.discourse.client.DiscourseClient; 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.apache.commons.lang3.ObjectUtils;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -43,6 +45,14 @@ public class TestBase {
DiscourseClient.api_username = prop.getProperty("api.username"); DiscourseClient.api_username = prop.getProperty("api.username");
DiscourseClient.api_key = prop.getProperty("api.key"); 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 // Init WeChat config for testing
// Document document = new SAXReader().read(inputStream); // Document document = new SAXReader().read(inputStream);
// TestConfigStorage config = new TestConfigStorage(); // TestConfigStorage config = new TestConfigStorage();

View File

@ -1,7 +1,7 @@
package com.ossez.discourse.client.test; package com.ossez.discourse.client.test;
import com.google.inject.Inject; 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 com.ossez.discourse.common.exception.WxErrorException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestInstance;
@ -18,7 +18,7 @@ import org.slf4j.LoggerFactory;
public class TopicsServiceTest extends TestBase { public class TopicsServiceTest extends TestBase {
private static final Logger log = LoggerFactory.getLogger(TopicsServiceTest.class); private static final Logger log = LoggerFactory.getLogger(TopicsServiceTest.class);
@Inject @Inject
protected TopicService topicService; protected TopicsService topicsService;
/** /**
* Test Create Menu * Test Create Menu
@ -28,7 +28,7 @@ public class TopicsServiceTest extends TestBase {
@Test @Test
public void testCreate() throws WxErrorException { public void testCreate() throws WxErrorException {
log.debug("Create WeChat Offical Account Menun Test"); 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());
} }

View File

@ -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;
}
}