diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/CourseTableApi.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/CourseTableApi.java index 05bc712..bccd401 100644 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/CourseTableApi.java +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/CourseTableApi.java @@ -1,8 +1,7 @@ package cn.wustlinghang.wusthelper.internal.graduate.api.http; import cn.wustlinghang.mywust.data.global.Course; -import cn.wustlinghang.wusthelper.internal.graduate.services.ParseService; -import cn.wustlinghang.wusthelper.internal.graduate.services.RequestAgentService; +import cn.wustlinghang.wusthelper.internal.graduate.services.CourseTableService; import cn.wustlinghang.wusthelper.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; import jakarta.validation.constraints.NotNull; @@ -16,13 +15,10 @@ import java.util.List; @Path("/course_table") @ApplicationScoped public class CourseTableApi { - private final RequestAgentService agentService; - private final ParseService parseService; + private final CourseTableService service; - public CourseTableApi(RequestAgentService agentService, - ParseService parseService) { - this.agentService = agentService; - this.parseService = parseService; + public CourseTableApi(CourseTableService service) { + this.service = service; } @GET @@ -36,12 +32,12 @@ public class CourseTableApi { @Path("/agent") public String agent(@QueryParam("cookie") @NotNull String cookie) throws RpcException { - return agentService.getCourseTable(cookie); + return service.getCourseTable(cookie); } @POST @Path("/parse") public List parse(String html) throws RpcException { - return parseService.parseCourseTable(html); + return service.parseCourseTable(html); } } diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/ScoreApi.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/ScoreApi.java index 8d7431e..15a9b74 100644 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/ScoreApi.java +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/ScoreApi.java @@ -1,8 +1,7 @@ package cn.wustlinghang.wusthelper.internal.graduate.api.http; import cn.wustlinghang.mywust.data.global.Score; -import cn.wustlinghang.wusthelper.internal.graduate.services.ParseService; -import cn.wustlinghang.wusthelper.internal.graduate.services.RequestAgentService; +import cn.wustlinghang.wusthelper.internal.graduate.services.ScoreService; import cn.wustlinghang.wusthelper.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; import jakarta.validation.constraints.NotNull; @@ -16,13 +15,10 @@ import java.util.List; @Path("/score") @ApplicationScoped public class ScoreApi { - private final RequestAgentService agentService; - private final ParseService parseService; + private final ScoreService service; - public ScoreApi(RequestAgentService agentService, - ParseService parseService) { - this.agentService = agentService; - this.parseService = parseService; + public ScoreApi(ScoreService service) { + this.service = service; } @GET @@ -36,12 +32,12 @@ public class ScoreApi { @Path("/agent") public String agent(@QueryParam("cookie") @NotNull String cookie) throws RpcException { - return agentService.getScore(cookie); + return service.getScore(cookie); } @POST @Path("/parse") public List parse(String html) throws RpcException { - return parseService.parseScore(html); + return service.parseScore(html); } } diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/StudentInfoApi.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/StudentInfoApi.java index a8acf10..0d037c8 100644 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/StudentInfoApi.java +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/StudentInfoApi.java @@ -1,8 +1,7 @@ package cn.wustlinghang.wusthelper.internal.graduate.api.http; import cn.wustlinghang.mywust.data.global.StudentInfo; -import cn.wustlinghang.wusthelper.internal.graduate.services.ParseService; -import cn.wustlinghang.wusthelper.internal.graduate.services.RequestAgentService; +import cn.wustlinghang.wusthelper.internal.graduate.services.StudentInfoService; import cn.wustlinghang.wusthelper.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; import jakarta.validation.constraints.NotNull; @@ -14,13 +13,10 @@ import jakarta.ws.rs.QueryParam; @Path("/student_info") @ApplicationScoped public class StudentInfoApi { - private final RequestAgentService agentService; - private final ParseService parseService; + private final StudentInfoService service; - public StudentInfoApi(RequestAgentService agentService, - ParseService parseService) { - this.agentService = agentService; - this.parseService = parseService; + public StudentInfoApi(StudentInfoService service) { + this.service = service; } @GET @@ -34,12 +30,12 @@ public class StudentInfoApi { @Path("/agent") public String agent(@QueryParam("cookie") @NotNull String cookie) throws RpcException { - return agentService.getStudentInfoPage(cookie); + return service.getStudentInfoPage(cookie); } @POST @Path("/parse") public StudentInfo parse(String html) throws RpcException { - return parseService.parseStudentInfo(html); + return service.parseStudentInfo(html); } } diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/TrainingPlanApi.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/TrainingPlanApi.java index a1fb13a..ec10ece 100644 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/TrainingPlanApi.java +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/TrainingPlanApi.java @@ -1,7 +1,6 @@ package cn.wustlinghang.wusthelper.internal.graduate.api.http; -import cn.wustlinghang.wusthelper.internal.graduate.services.ParseService; -import cn.wustlinghang.wusthelper.internal.graduate.services.RequestAgentService; +import cn.wustlinghang.wusthelper.internal.graduate.services.TrainingPlanService; import cn.wustlinghang.wusthelper.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; import jakarta.validation.constraints.NotNull; @@ -13,13 +12,10 @@ import jakarta.ws.rs.QueryParam; @Path("/training_plan") @ApplicationScoped public class TrainingPlanApi { - private final RequestAgentService agentService; - private final ParseService parseService; + private final TrainingPlanService service; - public TrainingPlanApi(RequestAgentService agentService, - ParseService parseService) { - this.agentService = agentService; - this.parseService = parseService; + public TrainingPlanApi(TrainingPlanService service) { + this.service = service; } @GET @@ -33,12 +29,12 @@ public class TrainingPlanApi { @Path("/agent") public String agent(@QueryParam("cookie") @NotNull String cookie) throws RpcException { - return agentService.getTrainingPlan(cookie); + return service.getTrainingPlan(cookie); } @POST @Path("/parse") public String parse(String html) throws RpcException { - return parseService.parseTrainingPlan(html); + return service.parseTrainingPlan(html); } } diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/BaseService.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/BaseService.java new file mode 100644 index 0000000..b429d4f --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/BaseService.java @@ -0,0 +1,32 @@ +package cn.wustlinghang.wusthelper.internal.graduate.services; + +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.wusthelper.internal.graduate.exception.GraduateRpcException; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public abstract class BaseService { + protected GraduateRpcException wrapApiException(ApiException e, GraduateRpcException.SubModuleCode subModuleCode) { + return switch (e.getCode()) { + case NETWORK_EXCEPTION -> new GraduateRpcException( + GraduateRpcException.TypeCode.NETWORK_EXCEPTION, + subModuleCode, + GraduateRpcException.ErrorCode.NETWORK_ERROR + ); + case COOKIE_INVALID -> new GraduateRpcException( + GraduateRpcException.TypeCode.AUTH_EXCEPTION, + subModuleCode, + GraduateRpcException.ErrorCode.COOKIE_INVALID + ); + default -> { + log.error("研究生:{}代理请求异常,异常未处理", subModuleCode.name()); + log.error("异常:", e); + yield new GraduateRpcException( + GraduateRpcException.TypeCode.AUTH_EXCEPTION, + subModuleCode, + GraduateRpcException.ErrorCode.COOKIE_INVALID + ); + } + }; + } +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/CourseTableService.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/CourseTableService.java new file mode 100644 index 0000000..c054238 --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/CourseTableService.java @@ -0,0 +1,57 @@ +package cn.wustlinghang.wusthelper.internal.graduate.services; + +import cn.wustlinghang.mywust.core.parser.graduate.GraduateCourseTableParser; +import cn.wustlinghang.mywust.core.request.service.graduate.GraduateCourseTableApiService; +import cn.wustlinghang.mywust.data.global.Course; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.network.RequestClientOption; +import cn.wustlinghang.wusthelper.internal.graduate.exception.GraduateRpcException; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; + +import java.io.IOException; +import java.util.List; + +@ApplicationScoped +public class CourseTableService extends BaseService { + + private final RequestClientOption requestClientOption; + + private final GraduateCourseTableApiService courseTableApiService; + private final GraduateCourseTableParser courseTableParser; + + public CourseTableService(RequestClientOption requestClientOption, + GraduateCourseTableApiService courseTableApiService, + GraduateCourseTableParser courseTableParser) { + this.requestClientOption = requestClientOption; + + this.courseTableApiService = courseTableApiService; + this.courseTableParser = courseTableParser; + } + + public String getCourseTable(String cookie) throws RpcException { + try { + return courseTableApiService.getPage(cookie, requestClientOption); + } catch (ApiException e) { + throw wrapApiException(e, GraduateRpcException.SubModuleCode.COURSE_TABLE); + } catch (IOException e) { + throw new GraduateRpcException( + GraduateRpcException.TypeCode.NETWORK_EXCEPTION, + GraduateRpcException.SubModuleCode.COURSE_TABLE, + GraduateRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public List parseCourseTable(String data) throws GraduateRpcException { + try { + return courseTableParser.parse(data); + } catch (Exception e) { + throw new GraduateRpcException( + GraduateRpcException.TypeCode.PARSE_EXCEPTION, + GraduateRpcException.SubModuleCode.COURSE_TABLE, + GraduateRpcException.ErrorCode.PARSE_ERROR + ); + } + } +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/ParseService.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/ParseService.java deleted file mode 100644 index 06f73d0..0000000 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/ParseService.java +++ /dev/null @@ -1,83 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.graduate.services; - -import cn.wustlinghang.mywust.core.parser.graduate.GraduateCourseTableParser; -import cn.wustlinghang.mywust.core.parser.graduate.GraduateScoreParser; -import cn.wustlinghang.mywust.core.parser.graduate.GraduateStudentInfoPageParser; -import cn.wustlinghang.mywust.core.parser.graduate.GraduateTrainingPlanPageParser; -import cn.wustlinghang.mywust.data.global.Course; -import cn.wustlinghang.mywust.data.global.Score; -import cn.wustlinghang.mywust.data.global.StudentInfo; -import cn.wustlinghang.mywust.data.undergrad.ExamDelayApplication; -import cn.wustlinghang.wusthelper.internal.graduate.exception.GraduateRpcException; -import cn.wustlinghang.wusthelper.rpc.exception.RpcException; -import jakarta.enterprise.context.ApplicationScoped; - -import java.util.List; - -@ApplicationScoped -public class ParseService { - - private final GraduateCourseTableParser courseTableParser; - private final GraduateScoreParser scoreParser; - private final GraduateStudentInfoPageParser studentInfoPageParser; - private final GraduateTrainingPlanPageParser trainingPlanPageParser; - - public ParseService(GraduateCourseTableParser courseTableParser, - GraduateScoreParser scoreParser, - GraduateStudentInfoPageParser studentInfoPageParser, - GraduateTrainingPlanPageParser trainingPlanPageParser) { - - this.courseTableParser = courseTableParser; - this.scoreParser = scoreParser; - this.studentInfoPageParser = studentInfoPageParser; - this.trainingPlanPageParser = trainingPlanPageParser; - } - - public List parseCourseTable(String data) throws GraduateRpcException { - try { - return courseTableParser.parse(data); - } catch (Exception e) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.PARSE_EXCEPTION, - GraduateRpcException.SubModuleCode.COURSE_TABLE, - GraduateRpcException.ErrorCode.PARSE_ERROR - ); - } - } - - public List parseScore(String data) throws RpcException { - try { - return scoreParser.parse(data); - } catch (Exception e) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.PARSE_EXCEPTION, - GraduateRpcException.SubModuleCode.SCORE, - GraduateRpcException.ErrorCode.PARSE_ERROR - ); - } - } - - public StudentInfo parseStudentInfo(String data) throws RpcException { - try { - return studentInfoPageParser.parse(data); - } catch (Exception e) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.PARSE_EXCEPTION, - GraduateRpcException.SubModuleCode.STUDENT_INFO, - GraduateRpcException.ErrorCode.PARSE_ERROR - ); - } - } - - public String parseTrainingPlan(String data) throws RpcException { - try { - return trainingPlanPageParser.parse(data); - } catch (Exception e) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.PARSE_EXCEPTION, - GraduateRpcException.SubModuleCode.TRAINING_PLAN, - GraduateRpcException.ErrorCode.PARSE_ERROR - ); - } - } -} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/RequestAgentService.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/RequestAgentService.java deleted file mode 100644 index 961a631..0000000 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/RequestAgentService.java +++ /dev/null @@ -1,122 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.graduate.services; - -import cn.wustlinghang.mywust.core.request.service.auth.GraduateLogin; -import cn.wustlinghang.mywust.core.request.service.graduate.GraduateCourseTableApiService; -import cn.wustlinghang.mywust.core.request.service.graduate.GraduateScoreApiService; -import cn.wustlinghang.mywust.core.request.service.graduate.GraduateStudentInfoApiService; -import cn.wustlinghang.mywust.core.request.service.graduate.GraduateTrainingPlanApiService; -import cn.wustlinghang.mywust.core.request.service.undergraduate.*; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.network.RequestClientOption; -import cn.wustlinghang.wusthelper.internal.graduate.exception.GraduateRpcException; -import cn.wustlinghang.wusthelper.rpc.exception.RpcException; -import jakarta.enterprise.context.ApplicationScoped; -import lombok.extern.slf4j.Slf4j; - -import java.io.IOException; - -@Slf4j -@ApplicationScoped -public class RequestAgentService { - - private final RequestClientOption requestClientOption; - - private final GraduateCourseTableApiService courseTableApiService; - private final GraduateScoreApiService scoreApiService; - private final GraduateStudentInfoApiService studentInfoApiService; - private final GraduateTrainingPlanApiService trainingPlanApiService; - - public RequestAgentService(RequestClientOption requestClientOption, - GraduateCourseTableApiService courseTableApiService, - GraduateScoreApiService scoreApiService, - GraduateStudentInfoApiService studentInfoApiService, - GraduateTrainingPlanApiService trainingPlanApiService) { - - this.requestClientOption = requestClientOption; - - this.courseTableApiService = courseTableApiService; - this.scoreApiService = scoreApiService; - this.studentInfoApiService = studentInfoApiService; - this.trainingPlanApiService = trainingPlanApiService; - } - - public String getStudentInfoPage(String cookie) throws RpcException { - try { - return studentInfoApiService.getPage(cookie, requestClientOption); - } catch (ApiException e) { - throw wrapApiException(e, GraduateRpcException.SubModuleCode.STUDENT_INFO); - } catch (IOException e) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.NETWORK_EXCEPTION, - GraduateRpcException.SubModuleCode.STUDENT_INFO, - GraduateRpcException.ErrorCode.NETWORK_ERROR - ); - } - } - - public String getCourseTable(String cookie) throws RpcException { - try { - return courseTableApiService.getPage(cookie, requestClientOption); - } catch (ApiException e) { - throw wrapApiException(e, GraduateRpcException.SubModuleCode.COURSE_TABLE); - } catch (IOException e) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.NETWORK_EXCEPTION, - GraduateRpcException.SubModuleCode.COURSE_TABLE, - GraduateRpcException.ErrorCode.NETWORK_ERROR - ); - } - } - - public String getScore(String cookie) throws RpcException { - try { - return scoreApiService.getPage(cookie, requestClientOption); - } catch (ApiException e) { - throw wrapApiException(e, GraduateRpcException.SubModuleCode.SCORE); - } catch (IOException e) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.NETWORK_EXCEPTION, - GraduateRpcException.SubModuleCode.SCORE, - GraduateRpcException.ErrorCode.NETWORK_ERROR - ); - } - } - - public String getTrainingPlan(String cookie) throws RpcException { - try { - return trainingPlanApiService.getPage(cookie, requestClientOption); - } catch (ApiException e) { - throw wrapApiException(e, GraduateRpcException.SubModuleCode.TRAINING_PLAN); - } catch (IOException e) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.NETWORK_EXCEPTION, - GraduateRpcException.SubModuleCode.TRAINING_PLAN, - GraduateRpcException.ErrorCode.NETWORK_ERROR - ); - } - } - - private GraduateRpcException wrapApiException(ApiException e, GraduateRpcException.SubModuleCode subModuleCode) { - return switch (e.getCode()) { - case NETWORK_EXCEPTION -> new GraduateRpcException( - GraduateRpcException.TypeCode.NETWORK_EXCEPTION, - subModuleCode, - GraduateRpcException.ErrorCode.NETWORK_ERROR - ); - case COOKIE_INVALID -> new GraduateRpcException( - GraduateRpcException.TypeCode.AUTH_EXCEPTION, - subModuleCode, - GraduateRpcException.ErrorCode.COOKIE_INVALID - ); - default -> { - log.error("研究生:{}代理请求异常,异常未处理", subModuleCode.name()); - log.error("异常:", e); - yield new GraduateRpcException( - GraduateRpcException.TypeCode.AUTH_EXCEPTION, - subModuleCode, - GraduateRpcException.ErrorCode.COOKIE_INVALID - ); - } - }; - } -} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/ScoreService.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/ScoreService.java new file mode 100644 index 0000000..2fcf478 --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/ScoreService.java @@ -0,0 +1,57 @@ +package cn.wustlinghang.wusthelper.internal.graduate.services; + +import cn.wustlinghang.mywust.core.parser.graduate.GraduateScoreParser; +import cn.wustlinghang.mywust.core.request.service.graduate.GraduateScoreApiService; +import cn.wustlinghang.mywust.data.global.Score; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.network.RequestClientOption; +import cn.wustlinghang.wusthelper.internal.graduate.exception.GraduateRpcException; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; + +import java.io.IOException; +import java.util.List; + +@ApplicationScoped +public class ScoreService extends BaseService { + + private final RequestClientOption requestClientOption; + + private final GraduateScoreApiService scoreApiService; + private final GraduateScoreParser scoreParser; + + public ScoreService(RequestClientOption requestClientOption, + GraduateScoreApiService scoreApiService, + GraduateScoreParser scoreParser) { + this.requestClientOption = requestClientOption; + + this.scoreApiService = scoreApiService; + this.scoreParser = scoreParser; + } + + public String getScore(String cookie) throws RpcException { + try { + return scoreApiService.getPage(cookie, requestClientOption); + } catch (ApiException e) { + throw wrapApiException(e, GraduateRpcException.SubModuleCode.SCORE); + } catch (IOException e) { + throw new GraduateRpcException( + GraduateRpcException.TypeCode.NETWORK_EXCEPTION, + GraduateRpcException.SubModuleCode.SCORE, + GraduateRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public List parseScore(String data) throws RpcException { + try { + return scoreParser.parse(data); + } catch (Exception e) { + throw new GraduateRpcException( + GraduateRpcException.TypeCode.PARSE_EXCEPTION, + GraduateRpcException.SubModuleCode.SCORE, + GraduateRpcException.ErrorCode.PARSE_ERROR + ); + } + } +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/StudentInfoService.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/StudentInfoService.java new file mode 100644 index 0000000..429cedd --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/StudentInfoService.java @@ -0,0 +1,56 @@ +package cn.wustlinghang.wusthelper.internal.graduate.services; + +import cn.wustlinghang.mywust.core.parser.graduate.GraduateStudentInfoPageParser; +import cn.wustlinghang.mywust.core.request.service.graduate.GraduateStudentInfoApiService; +import cn.wustlinghang.mywust.data.global.StudentInfo; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.network.RequestClientOption; +import cn.wustlinghang.wusthelper.internal.graduate.exception.GraduateRpcException; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; + +import java.io.IOException; + +@ApplicationScoped +public class StudentInfoService extends BaseService { + + private final RequestClientOption requestClientOption; + + private final GraduateStudentInfoApiService studentInfoApiService; + private final GraduateStudentInfoPageParser studentInfoPageParser; + + public StudentInfoService(RequestClientOption requestClientOption, + GraduateStudentInfoApiService studentInfoApiService, + GraduateStudentInfoPageParser studentInfoPageParser) { + this.requestClientOption = requestClientOption; + + this.studentInfoApiService = studentInfoApiService; + this.studentInfoPageParser = studentInfoPageParser; + } + + public String getStudentInfoPage(String cookie) throws RpcException { + try { + return studentInfoApiService.getPage(cookie, requestClientOption); + } catch (ApiException e) { + throw wrapApiException(e, GraduateRpcException.SubModuleCode.STUDENT_INFO); + } catch (IOException e) { + throw new GraduateRpcException( + GraduateRpcException.TypeCode.NETWORK_EXCEPTION, + GraduateRpcException.SubModuleCode.STUDENT_INFO, + GraduateRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public StudentInfo parseStudentInfo(String data) throws RpcException { + try { + return studentInfoPageParser.parse(data); + } catch (Exception e) { + throw new GraduateRpcException( + GraduateRpcException.TypeCode.PARSE_EXCEPTION, + GraduateRpcException.SubModuleCode.STUDENT_INFO, + GraduateRpcException.ErrorCode.PARSE_ERROR + ); + } + } +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/TrainingPlanService.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/TrainingPlanService.java new file mode 100644 index 0000000..908141a --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/TrainingPlanService.java @@ -0,0 +1,55 @@ +package cn.wustlinghang.wusthelper.internal.graduate.services; + +import cn.wustlinghang.mywust.core.parser.graduate.GraduateTrainingPlanPageParser; +import cn.wustlinghang.mywust.core.request.service.graduate.GraduateTrainingPlanApiService; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.network.RequestClientOption; +import cn.wustlinghang.wusthelper.internal.graduate.exception.GraduateRpcException; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; + +import java.io.IOException; + +@ApplicationScoped +public class TrainingPlanService extends BaseService { + + private final RequestClientOption requestClientOption; + + private final GraduateTrainingPlanApiService trainingPlanApiService; + private final GraduateTrainingPlanPageParser trainingPlanPageParser; + + public TrainingPlanService(RequestClientOption requestClientOption, + GraduateTrainingPlanApiService trainingPlanApiService, + GraduateTrainingPlanPageParser trainingPlanPageParser) { + this.requestClientOption = requestClientOption; + + this.trainingPlanApiService = trainingPlanApiService; + this.trainingPlanPageParser = trainingPlanPageParser; + } + + public String getTrainingPlan(String cookie) throws RpcException { + try { + return trainingPlanApiService.getPage(cookie, requestClientOption); + } catch (ApiException e) { + throw wrapApiException(e, GraduateRpcException.SubModuleCode.TRAINING_PLAN); + } catch (IOException e) { + throw new GraduateRpcException( + GraduateRpcException.TypeCode.NETWORK_EXCEPTION, + GraduateRpcException.SubModuleCode.TRAINING_PLAN, + GraduateRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public String parseTrainingPlan(String data) throws RpcException { + try { + return trainingPlanPageParser.parse(data); + } catch (Exception e) { + throw new GraduateRpcException( + GraduateRpcException.TypeCode.PARSE_EXCEPTION, + GraduateRpcException.SubModuleCode.TRAINING_PLAN, + GraduateRpcException.ErrorCode.PARSE_ERROR + ); + } + } +}