|
|
@@ -6,29 +6,21 @@ import cn.seecoder.bok.common.dto.CollectDTO;
|
|
|
import cn.seecoder.bok.common.dto.RateDTO;
|
|
|
import cn.seecoder.bok.common.utility.RestPage;
|
|
|
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 org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.data.domain.*;
|
|
|
+import okhttp3.HttpUrl;
|
|
|
+import okhttp3.Response;
|
|
|
+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.io.UnsupportedEncodingException;
|
|
|
-import java.net.URLEncoder;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
-import java.text.MessageFormat;
|
|
|
-import java.util.*;
|
|
|
-import java.util.function.Function;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
+import static cn.seecoder.bok.client.ParamUtil.*;
|
|
|
+
|
|
|
/**
|
|
|
* @author kunduin
|
|
|
*/
|
|
|
@@ -37,12 +29,7 @@ public class SeecoderBokClient
|
|
|
SeecoderBokRecordApi, SeecoderBokUserInteractiveApi,
|
|
|
SeecoderBokKnowledgeApi {
|
|
|
|
|
|
- private final Logger logger = LoggerFactory.getLogger(SeecoderBokClient.class);
|
|
|
-
|
|
|
- private final OkHttpClient client;
|
|
|
- private final ObjectMapper mapper;
|
|
|
-
|
|
|
- private final String CONFIG_HOST;
|
|
|
+ private final OkRequest req;
|
|
|
|
|
|
/**
|
|
|
* 创建client
|
|
|
@@ -50,21 +37,16 @@ public class SeecoderBokClient
|
|
|
* @param host bok地址,示例:{@code "http://bok.seecoder.cn"}
|
|
|
*/
|
|
|
public SeecoderBokClient(String host) {
|
|
|
- CONFIG_HOST = host;
|
|
|
- client = new OkHttpClient();
|
|
|
- mapper = JsonMapper.builder()
|
|
|
- .addModule(new ParameterNamesModule())
|
|
|
- .addModule(new Jdk8Module())
|
|
|
- .addModule(new JavaTimeModule())
|
|
|
- .build();
|
|
|
+ req = new OkRequest(host);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @Deprecated
|
|
|
public List<KnowledgeNodeVO> getKnowledgeNode(String name) {
|
|
|
- HttpUrl httpUrl = createUrlBuilder("/api/knowledge")
|
|
|
+ HttpUrl httpUrl = req.createUrlBuilder("/api/knowledge")
|
|
|
.addQueryParameter("name", name).build();
|
|
|
- Response response = get(httpUrl);
|
|
|
- return getResponseBody(response, new TypeReference<List<KnowledgeNodeVO>>() {})
|
|
|
+ Response response = req.get(httpUrl);
|
|
|
+ return req.getResponseBody(response, new TypeReference<List<KnowledgeNodeVO>>() {})
|
|
|
.orElse(Collections.emptyList());
|
|
|
}
|
|
|
|
|
|
@@ -72,7 +54,7 @@ public class SeecoderBokClient
|
|
|
public Page<QuestionVO> getQuestionList(String stem, List<String> tags,
|
|
|
List<QuestionType> types, List<Integer> weights,
|
|
|
List<String> knowledgeIds, Pageable pageable) {
|
|
|
- HttpUrl.Builder urlBuilder = createUrlBuilder("/api/question")
|
|
|
+ HttpUrl.Builder urlBuilder = req.createUrlBuilder("/api/question")
|
|
|
.addQueryParameter("stem", stem)
|
|
|
.addQueryParameter("tag", joinComma(tags))
|
|
|
.addQueryParameter("knowledgeId", joinComma(knowledgeIds))
|
|
|
@@ -83,54 +65,54 @@ public class SeecoderBokClient
|
|
|
|
|
|
@Override
|
|
|
public QuestionVO createQuestion(@NotNull QuestionVO question) {
|
|
|
- HttpUrl httpUrl = createUrlBuilder("/api/question").build();
|
|
|
- Response response = post(httpUrl, writeRequestBody(question));
|
|
|
- return getResponseBody(response, QuestionVO.class).orElseThrow(() -> SeecoderBokException.UNWRAP_EXCEPTION);
|
|
|
+ HttpUrl httpUrl = req.createUrlBuilder("/api/question").build();
|
|
|
+ Response response = req.post(httpUrl, req.writeRequestBody(question));
|
|
|
+ return req.getResponseBody(response, QuestionVO.class).orElseThrow(() -> SeecoderBokException.UNWRAP_EXCEPTION);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<QuestionVO> createQuestions(@NotNull List<QuestionVO> questionVOS) {
|
|
|
- HttpUrl httpUrl = createUrlBuilder("/api/questions").build();
|
|
|
- Response response = post(httpUrl, writeRequestBody(questionVOS));
|
|
|
- return getResponseBody(response, new TypeReference<List<QuestionVO>>() {})
|
|
|
+ HttpUrl httpUrl = req.createUrlBuilder("/api/questions").build();
|
|
|
+ Response response = req.post(httpUrl, req.writeRequestBody(questionVOS));
|
|
|
+ return req.getResponseBody(response, new TypeReference<List<QuestionVO>>() {})
|
|
|
.orElseThrow(() -> SeecoderBokException.UNWRAP_EXCEPTION);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Optional<QuestionVO> getQuestionById(@NotBlank String questionId) {
|
|
|
- HttpUrl httpUrl = createUrlBuilder(formatUrl("/api/question/{0}", encodeValue(questionId))).build();
|
|
|
- Response response = get(httpUrl);
|
|
|
- return getResponseBody(response, QuestionVO.class);
|
|
|
+ HttpUrl httpUrl = req.createUrlBuilder(formatUrl("/api/question/{0}", encodeValue(questionId))).build();
|
|
|
+ Response response = req.get(httpUrl);
|
|
|
+ return req.getResponseBody(response, QuestionVO.class);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public QuestionVO updateQuestion(@NotNull QuestionVO question) {
|
|
|
- HttpUrl httpUrl = createUrlBuilder(formatUrl("/api/question/{0}", encodeValue(question.getId()))).build();
|
|
|
- Response response = put(httpUrl, writeRequestBody(question));
|
|
|
- return getResponseBody(response, QuestionVO.class).orElseThrow(() -> SeecoderBokException.UNWRAP_EXCEPTION);
|
|
|
+ HttpUrl httpUrl = req.createUrlBuilder(formatUrl("/api/question/{0}", encodeValue(question.getId()))).build();
|
|
|
+ Response response = req.put(httpUrl, req.writeRequestBody(question));
|
|
|
+ return req.getResponseBody(response, QuestionVO.class).orElseThrow(() -> SeecoderBokException.UNWRAP_EXCEPTION);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void deleteQuestionById(@NotBlank String questionId) {
|
|
|
- HttpUrl httpUrl = createUrlBuilder(formatUrl("/api/question/{0}", questionId)).build();
|
|
|
- delete(httpUrl);
|
|
|
+ HttpUrl httpUrl = req.createUrlBuilder(formatUrl("/api/question/{0}", questionId)).build();
|
|
|
+ req.delete(httpUrl);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<QuestionVO> getQuestionListByIds(@NotNull List<String> ids) {
|
|
|
- HttpUrl httpUrl = createUrlBuilder("/api/question-query/id")
|
|
|
+ HttpUrl httpUrl = req.createUrlBuilder("/api/question-query/id")
|
|
|
.addQueryParameter("id", joinComma(ids))
|
|
|
.build();
|
|
|
- Response response = get(httpUrl);
|
|
|
- return getResponseBody(response, new TypeReference<List<QuestionVO>>() {})
|
|
|
+ Response response = req.get(httpUrl);
|
|
|
+ return req.getResponseBody(response, new TypeReference<List<QuestionVO>>() {})
|
|
|
.orElse(Collections.emptyList());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<RecordVO> createUserRecord(@NotNull List<RecordVO> records) {
|
|
|
- HttpUrl httpUrl = createUrlBuilder("/api/record").build();
|
|
|
- Response response = post(httpUrl, writeRequestBody(records));
|
|
|
- return getResponseBody(response, new TypeReference<List<RecordVO>>() {})
|
|
|
+ HttpUrl httpUrl = req.createUrlBuilder("/api/record").build();
|
|
|
+ Response response = req.post(httpUrl, req.writeRequestBody(records));
|
|
|
+ return req.getResponseBody(response, new TypeReference<List<RecordVO>>() {})
|
|
|
.orElse(Collections.emptyList());
|
|
|
}
|
|
|
|
|
|
@@ -140,46 +122,46 @@ public class SeecoderBokClient
|
|
|
List<String> questionIdList,
|
|
|
List<String> recordIdList,
|
|
|
Pageable pageable) {
|
|
|
- HttpUrl.Builder urlBuilder = createUrlBuilder("/api/record")
|
|
|
+ HttpUrl.Builder urlBuilder = req.createUrlBuilder("/api/record")
|
|
|
.addQueryParameter("userId", joinComma(userIdList))
|
|
|
.addQueryParameter("questionId", joinComma(questionIdList))
|
|
|
.addQueryParameter("recordId", joinComma(recordIdList));
|
|
|
HttpUrl httpUrl = addPageableToParams(urlBuilder, pageable).build();
|
|
|
- Response response = get(httpUrl);
|
|
|
- return getResponseBody(response, new TypeReference<RestPage<RecordVO>>() {})
|
|
|
+ Response response = req.get(httpUrl);
|
|
|
+ return req.getResponseBody(response, new TypeReference<RestPage<RecordVO>>() {})
|
|
|
.orElse(new RestPage<>());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public RecordQuestionVO getUserRecordQuestions(
|
|
|
@NotNull String userId, Pageable pageable) {
|
|
|
- HttpUrl.Builder urlBuilder = createUrlBuilder(formatUrl("/api/record/{0}/question", userId));
|
|
|
+ HttpUrl.Builder urlBuilder = req.createUrlBuilder(formatUrl("/api/record/{0}/question", userId));
|
|
|
HttpUrl httpUrl = addPageableToParams(urlBuilder, pageable).build();
|
|
|
- Response response = get(httpUrl);
|
|
|
- return getResponseBody(response, RecordQuestionVO.class)
|
|
|
+ Response response = req.get(httpUrl);
|
|
|
+ return req.getResponseBody(response, RecordQuestionVO.class)
|
|
|
.orElse(RecordQuestionVO.empty());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void collectQuestion(
|
|
|
@NotBlank String userId, @NotBlank String questionId, @NotNull CollectDTO collectDTO) {
|
|
|
- HttpUrl httpUrl = createUrlBuilder(
|
|
|
+ HttpUrl httpUrl = req.createUrlBuilder(
|
|
|
formatUrl("/api/user/{0}/question/{1}/collection", userId, questionId)).build();
|
|
|
- post(httpUrl, writeRequestBody(collectDTO));
|
|
|
+ req.post(httpUrl, req.writeRequestBody(collectDTO));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void rateQuestion(
|
|
|
@NotBlank String userId, @NotBlank String questionId, @NotNull RateDTO rateDTO) {
|
|
|
- HttpUrl httpUrl = createUrlBuilder(
|
|
|
+ HttpUrl httpUrl = req.createUrlBuilder(
|
|
|
formatUrl("/api/user/{0}/question/{1}/rate", userId, questionId)).build();
|
|
|
- post(httpUrl, writeRequestBody(rateDTO));
|
|
|
+ req.post(httpUrl, req.writeRequestBody(rateDTO));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Page<QuestionVO> getUserInteractiveQuestions(
|
|
|
@NotBlank String userId, Boolean collect, Boolean rate, Pageable pageable) {
|
|
|
- HttpUrl.Builder urlBuilder = createUrlBuilder(formatUrl("/api/user/{0}/question", userId))
|
|
|
+ HttpUrl.Builder urlBuilder = req.createUrlBuilder(formatUrl("/api/user/{0}/question", userId))
|
|
|
.addQueryParameter("collection", String.valueOf(collect))
|
|
|
.addQueryParameter("rate", String.valueOf(rate));
|
|
|
return getQuestionVOS(pageable, urlBuilder);
|
|
|
@@ -188,111 +170,29 @@ public class SeecoderBokClient
|
|
|
@Override
|
|
|
public Optional<InteractionVO> getUserInteractiveQuestion(
|
|
|
@NotBlank String userId, @NotBlank String questionId) {
|
|
|
- HttpUrl httpUrl = createUrlBuilder(
|
|
|
+ HttpUrl httpUrl = req.createUrlBuilder(
|
|
|
formatUrl("/api/user/{0}/question/{1}", userId, questionId)).build();
|
|
|
- Response response = get(httpUrl);
|
|
|
- return getResponseBody(response, InteractionVO.class);
|
|
|
+ Response response = req.get(httpUrl);
|
|
|
+ return req.getResponseBody(response, InteractionVO.class);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<InteractionVO> getUserInteractiveQuestionIn(
|
|
|
@NotBlank String userId, List<String> questionIdList) {
|
|
|
- HttpUrl httpUrl = createUrlBuilder(
|
|
|
+ HttpUrl httpUrl = req.createUrlBuilder(
|
|
|
formatUrl("/api/user/{0}/questionInList", userId)).build();
|
|
|
- Response response = get(httpUrl);
|
|
|
- return getResponseBody(response, new TypeReference<List<InteractionVO>>() {})
|
|
|
+ Response response = req.get(httpUrl);
|
|
|
+ return req.getResponseBody(response, new TypeReference<List<InteractionVO>>() {})
|
|
|
.orElse(Collections.emptyList());
|
|
|
}
|
|
|
|
|
|
private Page<QuestionVO> getQuestionVOS(Pageable pageable, HttpUrl.Builder urlBuilder) {
|
|
|
HttpUrl httpUrl = addPageableToParams(urlBuilder, pageable).build();
|
|
|
- Response response = get(httpUrl);
|
|
|
- return getResponseBody(response, new TypeReference<RestPage<QuestionVO>>() {})
|
|
|
+ Response response = req.get(httpUrl);
|
|
|
+ return req.getResponseBody(response, new TypeReference<RestPage<QuestionVO>>() {})
|
|
|
.orElse(new RestPage<>());
|
|
|
}
|
|
|
|
|
|
- private Response get(HttpUrl httpUrl) {
|
|
|
- return requestWithMethod("GET", httpUrl, null);
|
|
|
- }
|
|
|
-
|
|
|
- private void delete(HttpUrl httpUrl) {
|
|
|
- requestWithMethod("DELETE", 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;
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- private <T> RequestBody writeRequestBody(T obj) {
|
|
|
- if (obj == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- return RequestBody.create(mapper.writeValueAsString(obj), SeecoderBokConstants.JSON);
|
|
|
- } catch (JsonProcessingException e) {
|
|
|
- logger.error("JsonProcessingException.", e);
|
|
|
- throw SeecoderBokException.JSON_EXCEPTION;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
private HttpUrl.Builder addPageableToParams(HttpUrl.Builder builder, Pageable pageable) {
|
|
|
return Optional.ofNullable(pageable)
|
|
|
.map(notNullPageable ->
|
|
|
@@ -303,28 +203,4 @@ public class SeecoderBokClient
|
|
|
.orElse(builder);
|
|
|
}
|
|
|
|
|
|
- private String joinComma(Iterable<String> list) {
|
|
|
- return Optional.ofNullable(list)
|
|
|
- .map(notNullList -> String.join(",", notNullList))
|
|
|
- .orElse("");
|
|
|
- }
|
|
|
-
|
|
|
- private String parseSort(Sort sort) {
|
|
|
- return String.join("|",
|
|
|
- sort.map(order -> order.getProperty() + "," + order.getDirection()));
|
|
|
- }
|
|
|
-
|
|
|
- private static String encodeValue(String value) {
|
|
|
- try {
|
|
|
- return URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return value;
|
|
|
- }
|
|
|
-
|
|
|
- public static String formatUrl(String pattern, String... arguments) {
|
|
|
- MessageFormat temp = new MessageFormat(pattern);
|
|
|
- return temp.format(Arrays.stream(arguments).map(SeecoderBokClient::encodeValue).toArray());
|
|
|
- }
|
|
|
}
|