| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- package cn.seecoder.judgement;
- import cn.seecoder.judgement.dto.StudentAnswerDTO;
- import cn.seecoder.judgement.vo.JudgedAnswersVO;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.JsonNode;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import okhttp3.*;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.TimeUnit;
- import static cn.seecoder.judgement.JudgementException.BIZ_ERROR_CODE;
- public class JudgementClient implements JudgementApi {
- private final Logger logger = LoggerFactory.getLogger(getClass());
- private OkHttpClient client;
- private String host;
- private String token;
- public JudgementClient(String host,String token){
- this.token=token;
- this.host=host;
- this.client=new OkHttpClient.Builder()
- .connectTimeout(60, TimeUnit.SECONDS)
- .writeTimeout(60,TimeUnit.SECONDS)
- .retryOnConnectionFailure(true)
- .build();
- }
- private <T> T processResponse(ObjectMapper objectMapper, Response response, Class<T> clazz) throws JudgementException{
- try {
- validateResponseStatus(response);
- String body = response.body().string();
- JsonNode node = objectMapper.readTree(body);
- Integer code = Integer.parseInt(node.get("code").asText());
- String msg = node.get("msg").asText();
- if (code != 0) {
- throw new JudgementException(BIZ_ERROR_CODE, msg);
- }
- if (clazz == null) {
- return null;
- }
- return objectMapper.treeToValue(node.get("result"), clazz);
- } catch (IOException e) {
- logger.error("IOException", e);
- throw JudgementException.IO_ERROR;
- }
- }
- private void validateResponseStatus(Response response) throws JudgementException{
- if (response.code() == 403) {
- throw JudgementException.UNAHTORIZATION_ERROR;
- } else if (response.code() == 500) {
- throw JudgementException.INTERNAL_SERVER_ERROR;
- } else if (response.code() != 200) {
- throw JudgementException.UNKNOWN_ERROR;
- }
- }
- @Override
- public JudgedAnswersVO submitQuestionStudentAnswer(List<String> questionsIds, List<Object> answers) throws JudgementException{
- try {
- ObjectMapper objectMapper = new ObjectMapper();
- Request.Builder builder = new Request.Builder()
- .addHeader("Authorization", token);
- Map<String, StudentAnswerDTO.AnswerRecordDTO> records = new HashMap<>();
- for (int i = 0; i < questionsIds.size(); i++){
- StudentAnswerDTO.AnswerRecordDTO answerRecordDTO = new StudentAnswerDTO.AnswerRecordDTO();
- answerRecordDTO.setAnswer(answers.get(i));
- records.put(questionsIds.get(i), answerRecordDTO);
- }
- StudentAnswerDTO studentAnswerDTO=StudentAnswerDTO.builder()
- .records(records)
- .build();
- builder = builder
- .url(host + "/api/judgement/submit")
- .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(studentAnswerDTO)));
- Request request = builder.build();
- final Call call = client.newCall(request);
- Response response = call.execute();
- JudgedAnswersVO judgedAnswersVO = processResponse(objectMapper, response, JudgedAnswersVO.class);
- return judgedAnswersVO;
- }catch (JsonProcessingException e) {
- logger.error("JSON writeValueAsString error", e);
- throw JudgementException.JSON_ERROR;
- } catch (IOException e) {
- logger.error("IOException", e);
- throw JudgementException.IO_ERROR;
- }
- }
- }
|