|
|
@@ -11,28 +11,25 @@ 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 org.springframework.data.domain.*;
|
|
|
|
|
|
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.text.MessageFormat;
|
|
|
+import java.util.*;
|
|
|
import java.util.function.Function;
|
|
|
-import java.util.function.Supplier;
|
|
|
|
|
|
/**
|
|
|
* @author kunduin
|
|
|
*/
|
|
|
-public class SeecoderBokClient implements SeecoderBokQuestionApi, SeecoderBokQuestionQueryApi,
|
|
|
- SeecoderBokRecordApi, SeecoderBokUserInteractiveApi, SeecoderBokKnowledgeApi {
|
|
|
+public class SeecoderBokClient
|
|
|
+ implements SeecoderBokQuestionApi, SeecoderBokQuestionQueryApi,
|
|
|
+ SeecoderBokRecordApi, SeecoderBokUserInteractiveApi,
|
|
|
+ SeecoderBokKnowledgeApi {
|
|
|
+
|
|
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
private final OkHttpClient client;
|
|
|
@@ -59,88 +56,167 @@ public class SeecoderBokClient implements SeecoderBokQuestionApi, SeecoderBokQue
|
|
|
@Override
|
|
|
public List<KnowledgeNodeVO> getKnowledgeNode(String name) {
|
|
|
HttpUrl httpUrl = createUrlBuilder("/api/knowledge")
|
|
|
- .addQueryParameter("name", name)
|
|
|
- .build();
|
|
|
+ .addQueryParameter("name", name).build();
|
|
|
Response response = get(httpUrl);
|
|
|
- return getResponseBody(response,
|
|
|
- new TypeReference<List<KnowledgeNodeVO>>() {
|
|
|
- }).orElse(new ArrayList<>());
|
|
|
+ return getResponseBody(response, new TypeReference<List<KnowledgeNodeVO>>() {})
|
|
|
+ .orElse(Collections.emptyList());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Page<QuestionVO> getQuestionList(String stem, String tag, String knowledgeId, Pageable pageable) {
|
|
|
- return null;
|
|
|
+ public Page<QuestionVO> getQuestionList(
|
|
|
+ String stem, String tag, String knowledgeId, Pageable pageable) {
|
|
|
+ HttpUrl.Builder urlBuilder = createUrlBuilder("/api/question")
|
|
|
+ .addQueryParameter("stem", stem)
|
|
|
+ .addQueryParameter("tag", tag)
|
|
|
+ .addQueryParameter("knowledgeId", knowledgeId);
|
|
|
+ return getQuestionVOS(pageable, urlBuilder);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public QuestionVO createQuestion(@NotNull QuestionVO question) {
|
|
|
- return null;
|
|
|
+ HttpUrl httpUrl = createUrlBuilder("/api/question").build();
|
|
|
+ Response response = post(httpUrl, writeRequestBody(question));
|
|
|
+ return getResponseBody(response, QuestionVO.class).orElseThrow(() -> SeecoderBokException.UNWRAP_EXCEPTION);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Optional<QuestionVO> getQuestionById(@NotBlank String questionId) {
|
|
|
- return Optional.empty();
|
|
|
+ HttpUrl httpUrl = createUrlBuilder(MessageFormat.format("/api/question/{0}", questionId)).build();
|
|
|
+ Response response = get(httpUrl);
|
|
|
+ return getResponseBody(response, QuestionVO.class);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public QuestionVO updateQuestion(@NotNull QuestionVO question) {
|
|
|
- return null;
|
|
|
+ HttpUrl httpUrl = createUrlBuilder(MessageFormat.format("/api/question/{0}", question.getId())).build();
|
|
|
+ Response response = put(httpUrl, writeRequestBody(question));
|
|
|
+ return getResponseBody(response, QuestionVO.class).orElseThrow(() -> SeecoderBokException.UNWRAP_EXCEPTION);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void deleteQuestionById(@NotBlank String questionId) {
|
|
|
-
|
|
|
+ HttpUrl httpUrl = createUrlBuilder(MessageFormat.format("/api/question/{0}", questionId)).build();
|
|
|
+ delete(httpUrl);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<QuestionVO> getQuestionListByIds(@NotNull List<String> ids) {
|
|
|
- return null;
|
|
|
+ HttpUrl httpUrl = createUrlBuilder("/api/question-query/id")
|
|
|
+ .addQueryParameter("id", joinComma(ids))
|
|
|
+ .build();
|
|
|
+ Response response = get(httpUrl);
|
|
|
+ return getResponseBody(response, new TypeReference<List<QuestionVO>>() {})
|
|
|
+ .orElse(Collections.emptyList());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<RecordVO> createUserRecord(@NotNull List<RecordVO> records) {
|
|
|
- return null;
|
|
|
+ HttpUrl httpUrl = createUrlBuilder("/api/record").build();
|
|
|
+ Response response = post(httpUrl, writeRequestBody(records));
|
|
|
+ return getResponseBody(response, new TypeReference<List<RecordVO>>() {})
|
|
|
+ .orElse(Collections.emptyList());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Page<RecordVO> getUserRecord(List<String> userIdList, List<String> questionIdList, List<String> recordIdList, Pageable pageable) {
|
|
|
- return null;
|
|
|
+ public Page<RecordVO> getUserRecord(
|
|
|
+ List<String> userIdList,
|
|
|
+ List<String> questionIdList,
|
|
|
+ List<String> recordIdList,
|
|
|
+ Pageable pageable) {
|
|
|
+ HttpUrl.Builder urlBuilder = 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<Page<RecordVO>>() {})
|
|
|
+ .map(recordVOS -> new PageImpl<>(
|
|
|
+ recordVOS.getContent(),
|
|
|
+ parseBokToPageable(recordVOS.getNumber(), recordVOS.getSize(), pageable),
|
|
|
+ recordVOS.getTotalElements()))
|
|
|
+ .orElse(new PageImpl<>(Collections.emptyList()));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Page<RecordQuestionVO> getUserRecordQuestions(@NotNull String userId, Pageable pageable) {
|
|
|
- return null;
|
|
|
+ public PageableRecordQuestionVO getUserRecordQuestions(
|
|
|
+ @NotNull String userId, Pageable pageable) {
|
|
|
+ HttpUrl.Builder urlBuilder = createUrlBuilder(MessageFormat.format("/api/record/{0}/question", userId));
|
|
|
+ HttpUrl httpUrl = addPageableToParams(urlBuilder, pageable).build();
|
|
|
+ Response response = get(httpUrl);
|
|
|
+ return getResponseBody(response, PageableRecordQuestionVO.class)
|
|
|
+ .map(pageableRecordQuestionVO -> {
|
|
|
+ PageVO page = pageableRecordQuestionVO.getPage();
|
|
|
+ page.setNumber(parseBokToPageableNumber(page.getNumber()));
|
|
|
+ pageableRecordQuestionVO.setPage(page);
|
|
|
+ return pageableRecordQuestionVO;
|
|
|
+ })
|
|
|
+ .orElse(PageableRecordQuestionVO.ofEmpty());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void collectQuestion(@NotBlank String userId, @NotBlank String questionId, @NotNull CollectDTO collectDTO) {
|
|
|
-
|
|
|
+ public void collectQuestion(
|
|
|
+ @NotBlank String userId, @NotBlank String questionId, @NotNull CollectDTO collectDTO) {
|
|
|
+ HttpUrl httpUrl = createUrlBuilder(
|
|
|
+ MessageFormat.format("/api/user/{0}/question/{1}/collection", userId, questionId)).build();
|
|
|
+ post(httpUrl, writeRequestBody(collectDTO));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void rateQuestion(@NotBlank String userId, @NotBlank String questionId, @NotNull RateDTO rateDTO) {
|
|
|
-
|
|
|
+ public void rateQuestion(
|
|
|
+ @NotBlank String userId, @NotBlank String questionId, @NotNull RateDTO rateDTO) {
|
|
|
+ HttpUrl httpUrl = createUrlBuilder(
|
|
|
+ MessageFormat.format("/api/user/{0}/question/{1}/rate", userId, questionId)).build();
|
|
|
+ post(httpUrl, writeRequestBody(rateDTO));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Page<QuestionVO> getUserInteractiveQuestions(@NotBlank String userId, Boolean collect, Boolean rate, Pageable pageable) {
|
|
|
- return null;
|
|
|
+ public Page<QuestionVO> getUserInteractiveQuestions(
|
|
|
+ @NotBlank String userId, Boolean collect, Boolean rate, Pageable pageable) {
|
|
|
+ HttpUrl.Builder urlBuilder = createUrlBuilder(MessageFormat.format("/api/user/{0}/question", userId))
|
|
|
+ .addQueryParameter("collection", String.valueOf(collect))
|
|
|
+ .addQueryParameter("rate", String.valueOf(rate));
|
|
|
+ return getQuestionVOS(pageable, urlBuilder);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Page<QuestionVO> getQuestionVOS(Pageable pageable, HttpUrl.Builder urlBuilder) {
|
|
|
+ HttpUrl httpUrl = addPageableToParams(urlBuilder, pageable).build();
|
|
|
+ Response response = get(httpUrl);
|
|
|
+ return getResponseBody(response, PageableQuestionListVO.class)
|
|
|
+ .map(pageableQuestionListVO ->
|
|
|
+ new PageImpl<>(
|
|
|
+ pageableQuestionListVO.getQuestions(),
|
|
|
+ parseBokToPageable(pageableQuestionListVO.getPage(), pageable),
|
|
|
+ pageableQuestionListVO.getPage().getTotalElements()))
|
|
|
+ .orElse(new PageImpl<>(Collections.emptyList()));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public InteractiveVO getUserInteractiveQuestion(@NotBlank String userId, @NotBlank String questionId) {
|
|
|
- return null;
|
|
|
+ public Optional<InteractiveVO> getUserInteractiveQuestion(
|
|
|
+ @NotBlank String userId, @NotBlank String questionId) {
|
|
|
+ HttpUrl httpUrl = createUrlBuilder(
|
|
|
+ MessageFormat.format("/api/user/{0}/question/{1}", userId, questionId)).build();
|
|
|
+ Response response = get(httpUrl);
|
|
|
+ return getResponseBody(response, InteractiveVO.class);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<InteractiveVO> getUserInteractiveQuestionIn(@NotBlank String userId, List<String> questionIdList) {
|
|
|
- return null;
|
|
|
+ public List<InteractiveVO> getUserInteractiveQuestionIn(
|
|
|
+ @NotBlank String userId, List<String> questionIdList) {
|
|
|
+ HttpUrl httpUrl = createUrlBuilder(
|
|
|
+ MessageFormat.format("/api/user/{0}/questionInList", userId)).build();
|
|
|
+ Response response = get(httpUrl);
|
|
|
+ return getResponseBody(response, new TypeReference<List<InteractiveVO>>() {})
|
|
|
+ .orElse(Collections.emptyList());
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
@@ -149,7 +225,6 @@ public class SeecoderBokClient implements SeecoderBokQuestionApi, SeecoderBokQue
|
|
|
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 {
|
|
|
@@ -165,36 +240,89 @@ public class SeecoderBokClient implements SeecoderBokQuestionApi, SeecoderBokQue
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
- }
|
|
|
- });
|
|
|
+ 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;
|
|
|
- }
|
|
|
- });
|
|
|
+ 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;
|
|
|
- }
|
|
|
- });
|
|
|
+ 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 int parsePageableNumberToBok(int pageNumber) {
|
|
|
+ return pageNumber - CONFIG_PAGE_START + SeecoderBokConstants.PAGE_START;
|
|
|
+ }
|
|
|
+
|
|
|
+ private int parseBokToPageableNumber(int bokPageNumber) {
|
|
|
+ return bokPageNumber - SeecoderBokConstants.PAGE_START + CONFIG_PAGE_START;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Pageable parseBokToPageable(PageVO pageVO, Pageable pageable) {
|
|
|
+ return parseBokToPageable(pageVO.getNumber(), pageVO.getSize(), pageable);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Pageable parseBokToPageable(int page, int size, Pageable pageable) {
|
|
|
+ if (pageable == null) {
|
|
|
+ return PageRequest.of(parseBokToPageableNumber(page), size);
|
|
|
+ }
|
|
|
+ return pageable;
|
|
|
+ }
|
|
|
+
|
|
|
+ private HttpUrl.Builder addPageableToParams(HttpUrl.Builder builder, Pageable pageable) {
|
|
|
+ return Optional.ofNullable(pageable)
|
|
|
+ .map(notNullPageable ->
|
|
|
+ builder.addQueryParameter("page", String.valueOf(parsePageableNumberToBok(notNullPageable.getPageNumber())))
|
|
|
+ .addQueryParameter("total", notNullPageable.getSort().toString())
|
|
|
+ .addQueryParameter("size", String.valueOf(notNullPageable.getPageSize()))
|
|
|
+ )
|
|
|
+ .orElse(builder);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String joinComma(List<String> list) {
|
|
|
+ return Optional.ofNullable(list)
|
|
|
+ .map(notNullList -> String.join(",", notNullList))
|
|
|
+ .orElse("");
|
|
|
}
|
|
|
}
|