JudgementClient.java 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package cn.seecoder.judgement;
  2. import cn.seecoder.judgement.dto.StudentAnswerDTO;
  3. import cn.seecoder.judgement.vo.JudgedAnswersVO;
  4. import com.fasterxml.jackson.core.JsonProcessingException;
  5. import com.fasterxml.jackson.databind.JsonNode;
  6. import com.fasterxml.jackson.databind.ObjectMapper;
  7. import okhttp3.*;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import java.io.IOException;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.concurrent.TimeUnit;
  15. import static cn.seecoder.judgement.JudgementException.BIZ_ERROR_CODE;
  16. public class JudgementClient implements JudgementApi {
  17. private final Logger logger = LoggerFactory.getLogger(getClass());
  18. private OkHttpClient client;
  19. private String host;
  20. private String token;
  21. public JudgementClient(String host,String token){
  22. this.token=token;
  23. this.host=host;
  24. this.client=new OkHttpClient.Builder()
  25. .connectTimeout(60, TimeUnit.SECONDS)
  26. .writeTimeout(60,TimeUnit.SECONDS)
  27. .retryOnConnectionFailure(true)
  28. .build();
  29. }
  30. private <T> T processResponse(ObjectMapper objectMapper, Response response, Class<T> clazz) throws JudgementException{
  31. try {
  32. validateResponseStatus(response);
  33. String body = response.body().string();
  34. JsonNode node = objectMapper.readTree(body);
  35. Integer code = Integer.parseInt(node.get("code").asText());
  36. String msg = node.get("msg").asText();
  37. if (code != 0) {
  38. throw new JudgementException(BIZ_ERROR_CODE, msg);
  39. }
  40. if (clazz == null) {
  41. return null;
  42. }
  43. return objectMapper.treeToValue(node.get("result"), clazz);
  44. } catch (IOException e) {
  45. logger.error("IOException", e);
  46. throw JudgementException.IO_ERROR;
  47. }
  48. }
  49. private void validateResponseStatus(Response response) throws JudgementException{
  50. if (response.code() == 403) {
  51. throw JudgementException.UNAHTORIZATION_ERROR;
  52. } else if (response.code() == 500) {
  53. throw JudgementException.INTERNAL_SERVER_ERROR;
  54. } else if (response.code() != 200) {
  55. throw JudgementException.UNKNOWN_ERROR;
  56. }
  57. }
  58. @Override
  59. public JudgedAnswersVO submitQuestionStudentAnswer(List<String> questionsIds, List<Object> answers) throws JudgementException{
  60. try {
  61. ObjectMapper objectMapper = new ObjectMapper();
  62. Request.Builder builder = new Request.Builder()
  63. .addHeader("Authorization", token);
  64. Map<String, StudentAnswerDTO.AnswerRecordDTO> records = new HashMap<>();
  65. for (int i = 0; i < questionsIds.size(); i++){
  66. StudentAnswerDTO.AnswerRecordDTO answerRecordDTO = new StudentAnswerDTO.AnswerRecordDTO();
  67. answerRecordDTO.setAnswer(answers.get(i));
  68. records.put(questionsIds.get(i), answerRecordDTO);
  69. }
  70. StudentAnswerDTO studentAnswerDTO=StudentAnswerDTO.builder()
  71. .records(records)
  72. .build();
  73. builder = builder
  74. .url(host + "/api/judgement/submit")
  75. .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(studentAnswerDTO)));
  76. Request request = builder.build();
  77. final Call call = client.newCall(request);
  78. Response response = call.execute();
  79. JudgedAnswersVO judgedAnswersVO = processResponse(objectMapper, response, JudgedAnswersVO.class);
  80. return judgedAnswersVO;
  81. }catch (JsonProcessingException e) {
  82. logger.error("JSON writeValueAsString error", e);
  83. throw JudgementException.JSON_ERROR;
  84. } catch (IOException e) {
  85. logger.error("IOException", e);
  86. throw JudgementException.IO_ERROR;
  87. }
  88. }
  89. }