Skip to content

Commit

Permalink
Switch to more detailed exam endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
DL444 committed Jun 17, 2024
1 parent 1f2f166 commit d86a3c4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 49 deletions.
10 changes: 1 addition & 9 deletions DL444.CquSchedule.Backend/Models/UpstreamExamResponseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@ internal struct UpstreamExamResponseModel
[JsonPropertyName("msg")]
public string Message { get; set; }
[JsonPropertyName("data")]
public ExamDataModel Data { get; set; }
}

internal struct ExamDataModel
{
[JsonPropertyName("content")]
public ExamContentEntry[] Content { get; set; }
[JsonPropertyName("totalPages")]
public int TotalPages { get; set; }
public ExamContentEntry[] Data { get; set; }
}

internal struct ExamContentEntry
Expand Down
24 changes: 0 additions & 24 deletions DL444.CquSchedule.Backend/Services/ExamStudentIdService.cs

This file was deleted.

26 changes: 11 additions & 15 deletions DL444.CquSchedule.Backend/Services/ScheduleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ internal interface IScheduleService

internal sealed class UndergraduateScheduleService : IScheduleService
{
public UndergraduateScheduleService(HttpClient httpClient, IUpstreamCredentialEncryptionService encryptionService, IExamStudentIdService examStudentIdService, IConfiguration config)
public UndergraduateScheduleService(HttpClient httpClient, IUpstreamCredentialEncryptionService encryptionService, IConfiguration config)
{
this.httpClient = httpClient;
this.encryptionService = encryptionService;
this.examStudentIdService = examStudentIdService;
vacationServeDays = config.GetValue("Calendar:VacationServeDays", 3);
}

Expand Down Expand Up @@ -169,13 +168,15 @@ public async Task<Schedule> GetScheduleAsync(string username, string termId, ISi
throw new ArgumentException("Sign in context is not for undergraduate.");
}

string examStudentId = examStudentIdService.GetExamStudentId(username);
Task<HttpResponseMessage> examTask = httpClient.GetAsync($"https://my.cqu.edu.cn/api/exam/examTask/get-student-exam-list-outside?studentId={examStudentId}");
var authenticationHeader = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
var examRequest = new HttpRequestMessage(HttpMethod.Get, $"https://my.cqu.edu.cn/api/exam/examTask/get-student-exam-tab-list");
examRequest.Headers.Authorization = authenticationHeader;
Task<HttpResponseMessage> examTask = httpClient.SendAsync(examRequest);

var request = new HttpRequestMessage(HttpMethod.Post, $"https://my.cqu.edu.cn/api/timetable/class/timetable/student/my-table-detail?sessionId={termId}");
request.Content = JsonContent.Create<string[]>([username]);
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = await httpClient.SendAsync(request);
var courseRequest = new HttpRequestMessage(HttpMethod.Post, $"https://my.cqu.edu.cn/api/timetable/class/timetable/student/my-table-detail?sessionId={termId}");
courseRequest.Content = JsonContent.Create<string[]>([username]);
courseRequest.Headers.Authorization = authenticationHeader;
HttpResponseMessage response = await httpClient.SendAsync(courseRequest);
var responseModel = await UpstreamScheduleResponseModelSerializerContext.Default.DeserializeFromStringAsync(await response.Content.ReadAsStreamAsync());
Schedule schedule = new Schedule(username);
if (responseModel.Data == null)
Expand Down Expand Up @@ -229,18 +230,14 @@ public async Task<Schedule> GetScheduleAsync(string username, string termId, ISi

response = await examTask;
var examResponseModel = await UpstreamExamResponseModelSerializerContext.Default.DeserializeFromStringAsync(await response.Content.ReadAsStreamAsync());
if (!examResponseModel.Status.Equals("success", StringComparison.Ordinal) || examResponseModel.Data.Content == null)
if (!examResponseModel.Status.Equals("success", StringComparison.Ordinal) || examResponseModel.Data == null)
{
throw new UpstreamRequestException("Upstream server did not return success status for exam request.")
{
ErrorDescription = examResponseModel.Message
};
}
if (examResponseModel.Data.TotalPages > 1)
{
throw new UpstreamRequestException("Upstream server returned multiple exam pages.");
}
foreach (var responseEntry in examResponseModel.Data.Content)
foreach (var responseEntry in examResponseModel.Data)
{
bool dateParseSuccess = DateTime.TryParse(responseEntry.Date, out DateTime localDate);
bool startTimeParseSuccess = DateTime.TryParse(responseEntry.StartTime, out DateTime localStartTimeInDay);
Expand Down Expand Up @@ -407,7 +404,6 @@ private struct SigninInfo

private readonly HttpClient httpClient;
private readonly IUpstreamCredentialEncryptionService encryptionService;
private readonly IExamStudentIdService examStudentIdService;
private readonly int vacationServeDays;
private static readonly Regex lecturerExtractionRegex = new Regex("(.*?)-\\d");
private static readonly Regex roomSimplifyRegex = new Regex("(室|机房|中心|分析系统|创新设计|展示与分析).*?-(.*?)$");
Expand Down
1 change: 0 additions & 1 deletion DL444.CquSchedule.Backend/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ static void ConfigureServices(HostBuilderContext context, IServiceCollection ser

services.AddTransient<IUpstreamCredentialEncryptionService, UpstreamCredentialEncryptionService>();
services.AddTransient<IStoredCredentialEncryptionService, KeyVaultCredentialEncryptionService>();
services.AddTransient<IExamStudentIdService, ExamStudentIdService>();
services.AddTransient<IDataService, DataService>();
services.AddTransient<ITermService, TermService>();
services.AddTransient<ICalendarService, CalendarService>();
Expand Down

0 comments on commit d86a3c4

Please sign in to comment.