|
|
@@ -1,8 +1,200 @@
|
|
|
package cn.seecoder.bok.client;
|
|
|
|
|
|
+import cn.seecoder.bok.common.dto.CollectDTO;
|
|
|
+import cn.seecoder.bok.common.dto.RateDTO;
|
|
|
+import cn.seecoder.bok.common.vo.*;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.json.JsonMapper;
|
|
|
+import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
|
|
|
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
|
|
+import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
|
|
|
+import okhttp3.*;
|
|
|
+import okio.BufferedSource;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
+
|
|
|
+import javax.validation.constraints.NotBlank;
|
|
|
+import javax.validation.constraints.NotNull;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.function.Consumer;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.function.Supplier;
|
|
|
+
|
|
|
/**
|
|
|
* @author kunduin
|
|
|
*/
|
|
|
-public class SeecoderBokClient {
|
|
|
-
|
|
|
+public class SeecoderBokClient implements SeecoderBokQuestionApi, SeecoderBokQuestionQueryApi,
|
|
|
+ SeecoderBokRecordApi, SeecoderBokUserInteractiveApi, SeecoderBokKnowledgeApi {
|
|
|
+ private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
+
|
|
|
+ private final OkHttpClient client;
|
|
|
+ private final ObjectMapper mapper;
|
|
|
+
|
|
|
+ private final int CONFIG_PAGE_START;
|
|
|
+ private final String CONFIG_HOST;
|
|
|
+
|
|
|
+ public SeecoderBokClient(String host) {
|
|
|
+ this(host, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ public SeecoderBokClient(String host, int pageStart) {
|
|
|
+ this.CONFIG_HOST = host;
|
|
|
+ this.CONFIG_PAGE_START = pageStart;
|
|
|
+ client = new OkHttpClient();
|
|
|
+ mapper = JsonMapper.builder()
|
|
|
+ .addModule(new ParameterNamesModule())
|
|
|
+ .addModule(new Jdk8Module())
|
|
|
+ .addModule(new JavaTimeModule())
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<KnowledgeNodeVO> getKnowledgeNode(String name) {
|
|
|
+ HttpUrl httpUrl = createUrlBuilder("/api/knowledge")
|
|
|
+ .addQueryParameter("name", name)
|
|
|
+ .build();
|
|
|
+ Response response = get(httpUrl);
|
|
|
+ return getResponseBody(response,
|
|
|
+ new TypeReference<List<KnowledgeNodeVO>>() {
|
|
|
+ }).orElse(new ArrayList<>());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<QuestionVO> getQuestionList(String stem, String tag, String knowledgeId, Pageable pageable) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public QuestionVO createQuestion(@NotNull QuestionVO question) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Optional<QuestionVO> getQuestionById(@NotBlank String questionId) {
|
|
|
+ return Optional.empty();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public QuestionVO updateQuestion(@NotNull QuestionVO question) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteQuestionById(@NotBlank String questionId) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<QuestionVO> getQuestionListByIds(@NotNull List<String> ids) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<RecordVO> createUserRecord(@NotNull List<RecordVO> records) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<RecordVO> getUserRecord(List<String> userIdList, List<String> questionIdList, List<String> recordIdList, Pageable pageable) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<RecordQuestionVO> getUserRecordQuestions(@NotNull String userId, Pageable pageable) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void collectQuestion(@NotBlank String userId, @NotBlank String questionId, @NotNull CollectDTO collectDTO) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void rateQuestion(@NotBlank String userId, @NotBlank String questionId, @NotNull RateDTO rateDTO) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<QuestionVO> getUserInteractiveQuestions(@NotBlank String userId, Boolean collect, Boolean rate, Pageable pageable) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public InteractiveVO getUserInteractiveQuestion(@NotBlank String userId, @NotBlank String questionId) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<InteractiveVO> getUserInteractiveQuestionIn(@NotBlank String userId, List<String> questionIdList) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Response get(HttpUrl httpUrl) {
|
|
|
+ return requestWithMethod("GET", httpUrl, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Response post(HttpUrl httpUrl, RequestBody requestBody) {
|
|
|
+ return requestWithMethod("POST", httpUrl, requestBody);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Response put(HttpUrl httpUrl, RequestBody requestBody) {
|
|
|
+ return requestWithMethod("PUT", httpUrl, requestBody);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private Response requestWithMethod(String method, HttpUrl httpUrl, RequestBody requestBody) {
|
|
|
+ Request request = new Request.Builder().url(httpUrl).method(method, requestBody).build();
|
|
|
+ try {
|
|
|
+ return client.newCall(request).execute();
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("IOException.", e);
|
|
|
+ throw SeecoderBokException.IO_EXCEPTION;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private HttpUrl.Builder createUrlBuilder(final String path) {
|
|
|
+ return Objects.requireNonNull(HttpUrl.parse(CONFIG_HOST + path)).newBuilder();
|
|
|
+ }
|
|
|
+
|
|
|
+ private <T> Optional<T> getResponseBody(Response response, Class<T> tClass) {
|
|
|
+ return getResponseBody(response, bodyString -> {
|
|
|
+ try {
|
|
|
+ return mapper.readValue(bodyString, tClass);
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ logger.error("JsonProcessingException.", e);
|
|
|
+ throw SeecoderBokException.JSON_EXCEPTION;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private <T> Optional<T> getResponseBody(Response response, TypeReference<T> typeReference) {
|
|
|
+ return getResponseBody(response, bodyString -> {
|
|
|
+ try {
|
|
|
+ return mapper.readValue(bodyString, typeReference);
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ logger.error("JsonProcessingException.", e);
|
|
|
+ throw SeecoderBokException.JSON_EXCEPTION;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private <T> Optional<T> getResponseBody(Response response, Function<String, T> mapType) {
|
|
|
+ return Optional.ofNullable(response.body()).map(body -> {
|
|
|
+ try {
|
|
|
+ String bodyString = body.string();
|
|
|
+ return mapType.apply(bodyString);
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("IOException.", e);
|
|
|
+ throw SeecoderBokException.IO_EXCEPTION;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|