SeecoderBokClient.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package cn.seecoder.bok.client;
  2. import cn.seecoder.bok.common.dto.CollectDTO;
  3. import cn.seecoder.bok.common.dto.RateDTO;
  4. import cn.seecoder.bok.common.vo.*;
  5. import com.fasterxml.jackson.core.JsonProcessingException;
  6. import com.fasterxml.jackson.core.type.TypeReference;
  7. import com.fasterxml.jackson.databind.ObjectMapper;
  8. import com.fasterxml.jackson.databind.json.JsonMapper;
  9. import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
  10. import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
  11. import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
  12. import okhttp3.*;
  13. import okio.BufferedSource;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.data.domain.Page;
  17. import org.springframework.data.domain.Pageable;
  18. import javax.validation.constraints.NotBlank;
  19. import javax.validation.constraints.NotNull;
  20. import java.io.IOException;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Objects;
  24. import java.util.Optional;
  25. import java.util.function.Consumer;
  26. import java.util.function.Function;
  27. import java.util.function.Supplier;
  28. /**
  29. * @author kunduin
  30. */
  31. public class SeecoderBokClient implements SeecoderBokQuestionApi, SeecoderBokQuestionQueryApi,
  32. SeecoderBokRecordApi, SeecoderBokUserInteractiveApi, SeecoderBokKnowledgeApi {
  33. private final Logger logger = LoggerFactory.getLogger(getClass());
  34. private final OkHttpClient client;
  35. private final ObjectMapper mapper;
  36. private final int CONFIG_PAGE_START;
  37. private final String CONFIG_HOST;
  38. public SeecoderBokClient(String host) {
  39. this(host, 0);
  40. }
  41. public SeecoderBokClient(String host, int pageStart) {
  42. this.CONFIG_HOST = host;
  43. this.CONFIG_PAGE_START = pageStart;
  44. client = new OkHttpClient();
  45. mapper = JsonMapper.builder()
  46. .addModule(new ParameterNamesModule())
  47. .addModule(new Jdk8Module())
  48. .addModule(new JavaTimeModule())
  49. .build();
  50. }
  51. @Override
  52. public List<KnowledgeNodeVO> getKnowledgeNode(String name) {
  53. HttpUrl httpUrl = createUrlBuilder("/api/knowledge")
  54. .addQueryParameter("name", name)
  55. .build();
  56. Response response = get(httpUrl);
  57. return getResponseBody(response,
  58. new TypeReference<List<KnowledgeNodeVO>>() {
  59. }).orElse(new ArrayList<>());
  60. }
  61. @Override
  62. public Page<QuestionVO> getQuestionList(String stem, String tag, String knowledgeId, Pageable pageable) {
  63. return null;
  64. }
  65. @Override
  66. public QuestionVO createQuestion(@NotNull QuestionVO question) {
  67. return null;
  68. }
  69. @Override
  70. public Optional<QuestionVO> getQuestionById(@NotBlank String questionId) {
  71. return Optional.empty();
  72. }
  73. @Override
  74. public QuestionVO updateQuestion(@NotNull QuestionVO question) {
  75. return null;
  76. }
  77. @Override
  78. public void deleteQuestionById(@NotBlank String questionId) {
  79. }
  80. @Override
  81. public List<QuestionVO> getQuestionListByIds(@NotNull List<String> ids) {
  82. return null;
  83. }
  84. @Override
  85. public List<RecordVO> createUserRecord(@NotNull List<RecordVO> records) {
  86. return null;
  87. }
  88. @Override
  89. public Page<RecordVO> getUserRecord(List<String> userIdList, List<String> questionIdList, List<String> recordIdList, Pageable pageable) {
  90. return null;
  91. }
  92. @Override
  93. public Page<RecordQuestionVO> getUserRecordQuestions(@NotNull String userId, Pageable pageable) {
  94. return null;
  95. }
  96. @Override
  97. public void collectQuestion(@NotBlank String userId, @NotBlank String questionId, @NotNull CollectDTO collectDTO) {
  98. }
  99. @Override
  100. public void rateQuestion(@NotBlank String userId, @NotBlank String questionId, @NotNull RateDTO rateDTO) {
  101. }
  102. @Override
  103. public Page<QuestionVO> getUserInteractiveQuestions(@NotBlank String userId, Boolean collect, Boolean rate, Pageable pageable) {
  104. return null;
  105. }
  106. @Override
  107. public InteractiveVO getUserInteractiveQuestion(@NotBlank String userId, @NotBlank String questionId) {
  108. return null;
  109. }
  110. @Override
  111. public List<InteractiveVO> getUserInteractiveQuestionIn(@NotBlank String userId, List<String> questionIdList) {
  112. return null;
  113. }
  114. private Response get(HttpUrl httpUrl) {
  115. return requestWithMethod("GET", httpUrl, null);
  116. }
  117. private Response post(HttpUrl httpUrl, RequestBody requestBody) {
  118. return requestWithMethod("POST", httpUrl, requestBody);
  119. }
  120. private Response put(HttpUrl httpUrl, RequestBody requestBody) {
  121. return requestWithMethod("PUT", httpUrl, requestBody);
  122. }
  123. private Response requestWithMethod(String method, HttpUrl httpUrl, RequestBody requestBody) {
  124. Request request = new Request.Builder().url(httpUrl).method(method, requestBody).build();
  125. try {
  126. return client.newCall(request).execute();
  127. } catch (IOException e) {
  128. logger.error("IOException.", e);
  129. throw SeecoderBokException.IO_EXCEPTION;
  130. }
  131. }
  132. private HttpUrl.Builder createUrlBuilder(final String path) {
  133. return Objects.requireNonNull(HttpUrl.parse(CONFIG_HOST + path)).newBuilder();
  134. }
  135. private <T> Optional<T> getResponseBody(Response response, Class<T> tClass) {
  136. return getResponseBody(response, bodyString -> {
  137. try {
  138. return mapper.readValue(bodyString, tClass);
  139. } catch (JsonProcessingException e) {
  140. logger.error("JsonProcessingException.", e);
  141. throw SeecoderBokException.JSON_EXCEPTION;
  142. }
  143. });
  144. }
  145. private <T> Optional<T> getResponseBody(Response response, TypeReference<T> typeReference) {
  146. return getResponseBody(response, bodyString -> {
  147. try {
  148. return mapper.readValue(bodyString, typeReference);
  149. } catch (JsonProcessingException e) {
  150. logger.error("JsonProcessingException.", e);
  151. throw SeecoderBokException.JSON_EXCEPTION;
  152. }
  153. });
  154. }
  155. private <T> Optional<T> getResponseBody(Response response, Function<String, T> mapType) {
  156. return Optional.ofNullable(response.body()).map(body -> {
  157. try {
  158. String bodyString = body.string();
  159. return mapType.apply(bodyString);
  160. } catch (IOException e) {
  161. logger.error("IOException.", e);
  162. throw SeecoderBokException.IO_EXCEPTION;
  163. }
  164. });
  165. }
  166. }