data types are better

This commit is contained in:
2023-07-16 01:08:30 -06:00
parent ed1963c67b
commit d691f817b7
13 changed files with 196 additions and 99 deletions

View File

@@ -1,21 +1,19 @@
using CanvasModel.EnrollmentTerms;
using CanvasModel.Courses;
using CanvasModel;
public class CoursePlanner
{
public void SetConfiguration(
EnrollmentTermModel canvasTerm,
DayOfWeek[] daysOfWeek
)
public void SetConfiguration(EnrollmentTermModel canvasTerm, DayOfWeek[] daysOfWeek)
{
var start = canvasTerm.StartAt ?? throw new Exception($"Canvas Term must have a start date. Term: {canvasTerm.Name}");
var end = canvasTerm.EndAt ?? throw new Exception($"Canvas Term must have a end date. Term: {canvasTerm.Name}");
var start =
canvasTerm.StartAt
?? throw new Exception($"Canvas Term must have a start date. Term: {canvasTerm.Name}");
var end =
canvasTerm.EndAt
?? throw new Exception($"Canvas Term must have a end date. Term: {canvasTerm.Name}");
SemesterCalendar = new SemesterCalendarConfig(
StartDate: start,
EndDate: end,
Days: daysOfWeek
);
SemesterCalendar = new SemesterCalendarConfig(StartDate: start, EndDate: end, Days: daysOfWeek);
}
public SemesterCalendarConfig? SemesterCalendar { get; set; } = null;
@@ -23,4 +21,4 @@ public class CoursePlanner
public IEnumerable<CourseModule> Modules { get; set; } = new CourseModule[] { };
public IEnumerable<LocalAssignment> Assignments { get; set; } = new LocalAssignment[] { };
public CourseModel? Course { get; set; } = null;
}
}

View File

@@ -1,8 +1,8 @@
using CanvasModel.Enrollments;
namespace CanvasModel.Courses;
public record CourseModel
(
public record CourseModel(
[property: JsonPropertyName("id")] ulong Id,
[property: JsonPropertyName("sis_course_id")] string SisCourseId,
[property: JsonPropertyName("uuid")] string Uuid,
@@ -33,21 +33,27 @@ public record CourseModel
[property: JsonPropertyName("needs_grading_count")] uint? NeedsGradingCount = null,
[property: JsonPropertyName("term")] TermModel? Term = null,
[property: JsonPropertyName("course_progress")] CourseProgressModel? CourseProgress = null,
[property: JsonPropertyName("apply_assignment_group_weights")] bool? ApplyAssignmentGroupWeights = null,
[property: JsonPropertyName("is_public")] bool? Is= null,
[property: JsonPropertyName("apply_assignment_group_weights")]
bool? ApplyAssignmentGroupWeights = null,
[property: JsonPropertyName("is_public")] bool? Is = null,
[property: JsonPropertyName("is_public_to_auth_users")] bool? IsPublicToAuthUsers = null,
[property: JsonPropertyName("public_syllabus")] bool? PublicSyllabus = null,
[property: JsonPropertyName("public_syllabus_to_auth")] bool? PublicSyllabusToAuth = null,
[property: JsonPropertyName("public_description")] string? PublicDescription = null,
[property: JsonPropertyName("hide_final_grades")] bool? HideFinalGrades = null,
[property: JsonPropertyName("allow_student_assignment_edits")] bool? AllowStudentAssignmentEdits = null,
[property: JsonPropertyName("allow_student_assignment_edits")]
bool? AllowStudentAssignmentEdits = null,
[property: JsonPropertyName("allow_wiki_comments")] bool? AllowWikiComments = null,
[property: JsonPropertyName("allow_student_forum_attachments")] bool? AllowStudentForumAttachments = null,
[property: JsonPropertyName("allow_student_forum_attachments")]
bool? AllowStudentForumAttachments = null,
[property: JsonPropertyName("open_enrollment")] bool? OpenEnrollment = null,
[property: JsonPropertyName("self_enrollment")] bool? SelfEnrollment = null,
[property: JsonPropertyName("restrict_enrollments_to_courses")] bool? RestrictEnrollmentsToCourseDates = null,
[property: JsonPropertyName("restrict_enrollments_to_courses")]
bool? RestrictEnrollmentsToCourseDates = null,
[property: JsonPropertyName("access_restricted_by_date")] bool? AccessRestrictedByDate = null,
[property: JsonPropertyName("blueprint")] bool? Blueprint = null,
[property: JsonPropertyName("blueprint_restrictions")] Dictionary<string, bool>? BlueprintRestrictions = null,
[property: JsonPropertyName("blueprint_restrictions_by_object_type")] Dictionary<string, Dictionary<string, bool>>? BlueprintRestrictionsByObjectType = null
);
[property: JsonPropertyName("blueprint_restrictions")]
Dictionary<string, bool>? BlueprintRestrictions = null,
[property: JsonPropertyName("blueprint_restrictions_by_object_type")]
Dictionary<string, Dictionary<string, bool>>? BlueprintRestrictionsByObjectType = null
);

View File

@@ -1,12 +1,11 @@
using System.ComponentModel.DataAnnotations;
namespace CanvasModel;
public record CourseModule(
[property: Required]
[property: StringLength(50, ErrorMessage = "Name too long (50 character limit).")]
string Name,
IEnumerable<LocalAssignment>? Assignments = null
)
{
[JsonInclude]
public IEnumerable<LocalAssignment> Assignments = Assignments ?? new LocalAssignment[] { };
}
[property: JsonPropertyName("id")] ulong Id,
[property: JsonPropertyName("name")] string Name
// [property: JsonPropertyName("start_at")] DateTime StartAt,
// [property: JsonPropertyName("end_at")] DateTime EndAt,
// [property: JsonPropertyName("description")] string Description
);

View File

@@ -1,3 +1,4 @@
using CanvasModel;
using CanvasModel.Courses;
using CanvasModel.EnrollmentTerms;
using RestSharp;
@@ -40,35 +41,59 @@ public class CanvasService : ICanvasService
public async Task<CourseModel> GetCourse(ulong courseId)
{
var url = $"course/${courseId}";
var url = $"course/{courseId}";
var request = new RestRequest(url);
var response = await webRequestor.GetAsync<CourseModel>(request);
var (data, response) = await webRequestor.GetAsync<CourseModel>(request);
if (response.Data == null)
if (data == null)
{
System.Console.WriteLine(response.Content);
System.Console.WriteLine(response.ResponseUri);
throw new Exception("error getting course from canvas");
}
return response.Data;
return data;
}
public async Task<IEnumerable<CourseModule>> GetModules(ulong courseId)
{
var url = $"courses/{courseId}/modules";
var request = new RestRequest(url);
var modules = await PaginatedRequest<IEnumerable<CourseModule>>(request);
return modules.SelectMany(c => c).ToArray();
}
public async Task CreateModule(ulong courseId, string name)
{
var url = $"courses/{courseId}/modules";
var request = new RestRequest(url);
request.AddParameter("module[name]", name);
await webRequestor.PostAsync(request);
}
private async Task<IEnumerable<T>> PaginatedRequest<T>(RestRequest request)
{
var requestCount = 1;
request.AddQueryParameter("per_page", "100");
RestResponse<T> response = await webRequestor.GetAsync<T>(request);
var (data, response) = await webRequestor.GetAsync<T>(request);
var returnData = response.Data != null ? new T[] { response.Data } : new T[] { };
if (response.ErrorMessage?.Length > 0)
{
System.Console.WriteLine("error in response");
System.Console.WriteLine(response.ErrorMessage);
throw new Exception("error in response");
}
var returnData = data != null ? new T[] { data } : new T[] { };
var nextUrl = getNextUrl(response.Headers);
while (nextUrl is not null)
{
requestCount += 1;
RestRequest nextRequest = new RestRequest(nextUrl);
var nextResponse = await webRequestor.GetAsync<T>(nextRequest);
if (nextResponse.Data is not null)
returnData = returnData.Append(nextResponse.Data).ToArray();
var (nextData, nextResponse) = await webRequestor.GetAsync<T>(nextRequest);
if (nextData is not null)
returnData = returnData.Append(nextData).ToArray();
nextUrl = getNextUrl(nextResponse.Headers);
}

View File

@@ -2,6 +2,8 @@ using RestSharp;
public interface IWebRequestor
{
Task<RestResponse<T[]>> GetManyAsync<T>(RestRequest request);
Task<RestResponse<T>> GetAsync<T>(RestRequest request);
Task<(T[]?, RestResponse)> GetManyAsync<T>(RestRequest request);
Task<(T?, RestResponse)> GetAsync<T>(RestRequest request);
Task<RestResponse> PostAsync(RestRequest request);
Task<(T?, RestResponse)> PostAsync<T>(RestRequest request);
}

View File

@@ -13,16 +13,67 @@ public class WebRequestor : IWebRequestor
?? throw new Exception("CANVAS_TOKEN not in environment");
client = new RestClient(BaseUrl);
client.AddDefaultHeader("Authorization", $"Bearer {token}");
}
public async Task<RestResponse<T[]>> GetManyAsync<T>(RestRequest request)
public async Task<(T[]?, RestResponse)> GetManyAsync<T>(RestRequest request)
{
return await client.ExecuteGetAsync<T[]>(request);
var response = await client.ExecuteGetAsync(request);
return (Deserialize<T[]>(response), response);
}
public async Task<RestResponse<T>> GetAsync<T>(RestRequest request)
public async Task<(T?, RestResponse)> GetAsync<T>(RestRequest request)
{
return await client.ExecuteGetAsync<T>(request);
var response = await client.ExecuteGetAsync(request);
return (Deserialize<T>(response), response);
}
public async Task<RestResponse> PostAsync(RestRequest request)
{
var response = await client.ExecutePostAsync(request);
if (!response.IsSuccessful)
{
System.Console.WriteLine(response.Content);
System.Console.WriteLine(response.ResponseUri);
System.Console.WriteLine("error with response");
throw new Exception("error with response");
}
return response;
}
public async Task<(T?, RestResponse)> PostAsync<T>(RestRequest request)
{
var response = await client.ExecutePostAsync(request);
return (Deserialize<T>(response), response);
}
public T? Deserialize<T>(RestResponse response)
{
if (!response.IsSuccessful)
{
System.Console.WriteLine(response.Content);
System.Console.WriteLine(response.ResponseUri);
System.Console.WriteLine(response.ErrorMessage);
System.Console.WriteLine("error with response");
throw new Exception("error with response");
}
try
{
var data = JsonSerializer.Deserialize<T>(response.Content);
if (data == null)
{
System.Console.WriteLine(response.Content);
System.Console.WriteLine(response.ResponseUri);
System.Console.WriteLine("could not parse response, got empty object");
}
return data;
}
catch (JsonException ex)
{
System.Console.WriteLine(response.ResponseUri);
System.Console.WriteLine(response.Content);
Console.WriteLine($"An error occurred during deserialization: {ex.Message}");
throw ex;
}
}
}