ZCH-20 更新 Post 的方法

This commit is contained in:
YuCheng Hu 2023-10-04 12:28:31 -04:00
parent 223d951d62
commit 259050ba8d
9 changed files with 123 additions and 31 deletions

View File

@ -14,6 +14,7 @@ public abstract class DiscourseClient {
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())
@ -34,4 +35,14 @@ public abstract class DiscourseClient {
return request;
}
public Request putRequest(String path, RequestBody body) {
HttpUrl.Builder urlBuilder = HttpUrl.parse(site_url + path).newBuilder();
Request request = new Request.Builder().url(urlBuilder.build().toString())
.addHeader("api-username", api_username)
.addHeader("api-key", api_key)
.put(body)
.build();
return request;
}
}

View File

@ -1,11 +1,17 @@
package com.ossez.discourse.client.service;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.ossez.discourse.client.DiscourseClient;
import com.ossez.discourse.common.model.dto.Post;
import com.ossez.discourse.common.model.dto.PostUpdate;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -55,4 +61,34 @@ public class PostsService extends DiscourseClient {
}
return post;
}
public Optional<Post> updatePost(PostUpdate postUpdate) {
if (ObjectUtils.isEmpty(postUpdate.getId()))
throw new RuntimeException("Post Id is empty, not able to update a post.");
String path = "posts/" + String.valueOf(postUpdate.getId()) + ".json";
Optional<Post> post = Optional.ofNullable(new Post());
try {
RequestBody body = RequestBody.create(
MediaType.parse("application/json"), objectMapper.writeValueAsString(postUpdate));
Response response = client.newCall(putRequest(path, body)).execute();
String responseStr = response.body().string();
JsonNode jsonNode = objectMapper.readTree(responseStr);
if (response.code() == HttpStatus.SC_OK) {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
if (ObjectUtils.isNotEmpty(jsonNode.get("post"))) {
post = Optional.of(objectMapper.treeToValue(jsonNode.get("post"), Post.class));
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return post;
}
}

View File

@ -5,7 +5,7 @@ import com.ossez.discourse.common.model.WeChatStatus;
import com.ossez.discourse.common.model.req.CustomMessage;
import com.ossez.discourse.common.model.req.DataCubeRequest;
import com.ossez.discourse.common.model.req.NetworkCheck;
import com.ossez.discourse.common.model.req.QueryQuota;
import com.ossez.discourse.common.model.dto.PostUpdate;
import com.ossez.discourse.common.model.res.DataCubeArticle;
import com.ossez.discourse.common.model.res.DataCubeUser;
import com.ossez.discourse.common.model.res.NetworkCheckResponse;
@ -27,7 +27,7 @@ public interface WeChatOfficialAccountApi {
Single<NetworkCheckResponse> clearQuota(@Body NetworkCheck request);
@POST("/cgi-bin/openapi/quota/get")
Single<QueryQuotaResponse> queryQuota(@Body QueryQuota request);
Single<QueryQuotaResponse> queryQuota(@Body PostUpdate request);
@POST("/cgi-bin/message/custom/send")
Single<WeChatStatus> sendMessage(@Body CustomMessage customMessage);

View File

@ -1,14 +1,19 @@
package com.ossez.discourse.client.test;
import org.apache.commons.io.StandardLineSeparator;
import com.google.inject.Inject;
import com.ossez.discourse.client.service.PostsService;
import com.ossez.discourse.common.exception.DiscourseErrorException;
import com.ossez.discourse.common.model.dto.PostUpdate;
import org.apache.commons.lang3.StringUtils;
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;
import java.util.UUID;
/**
* Test for datacube API
*
@ -26,11 +31,24 @@ public class PostsServiceTest extends TestBase {
* @throws DiscourseErrorException
*/
@Test
public void testCreate() throws DiscourseErrorException {
public void testCreatePost() throws DiscourseErrorException {
log.debug("Create WeChat Offical Account Menun Test");
log.debug("{}", postsService.getPost(Long.valueOf("1245")).get().getRaw());
}
@Test
public void testUpdatePost() throws DiscourseErrorException {
log.debug("Discourse Update Post Test");
PostUpdate postUpdate = new PostUpdate();
postUpdate.setId(DISCOURSE_POST_ID);
StringBuilder rawBuilder = new StringBuilder().append(DISCOURSE_TOPIC_CONTENT);
rawBuilder.append(StandardLineSeparator.CRLF.getString());
rawBuilder.append(UUID.randomUUID().toString());
postUpdate.setRaw(rawBuilder.toString());
log.debug("{}", postsService.updatePost(postUpdate).get().getRaw());
}
}

View File

@ -26,6 +26,7 @@ public class TestBase {
public static final Long DISCOURSE_POST_ID = 594L;
public static final Long DISCOURSE_TOPIC_ID = 570L;
public static final String DISCOURSE_TOPIC_TITLE = "ZCHub Discourse API Test";
public static final String DISCOURSE_TOPIC_CONTENT = "ZCHub Discourse API Test";
public static final String DISCOURSE_TOPIC_TITLE_CREATE = "ZCHub Discourse API Test - CREATE";
@BeforeAll

View File

@ -77,18 +77,21 @@
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>

View File

@ -0,0 +1,43 @@
package com.ossez.discourse.common.model.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;
/**
* PostUpdate Data Object
*
* @author YuCheng Hu
*/
public class PostUpdate implements Serializable {
private static final long serialVersionUID = -227183546475863573L;
private Long id;
private String raw;
private String editReason;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRaw() {
return raw;
}
public void setRaw(String raw) {
this.raw = raw;
}
public String getEditReason() {
return editReason;
}
public void setEditReason(String editReason) {
this.editReason = editReason;
}
}

View File

@ -1,22 +0,0 @@
package com.ossez.discourse.common.model.req;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.StringUtils;
/**
* WeChatAccessToken Response Object
*
* @author YuCheng Hu
*/
public class QueryQuota {
@JsonProperty("cgi_path")
private String cgiPath = "/cgi-bin/message/custom/send";
public String getCgiPath() {
return cgiPath;
}
public void setCgiPath(String cgiPath) {
this.cgiPath = cgiPath;
}
}

View File

@ -96,6 +96,8 @@
<artifactId>httpmime</artifactId>
<version>${httpclient.version}</version>
</dependency>
<!-- APACHE COMMONS -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
@ -104,7 +106,7 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>