mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-25 23:28:33 -06:00
hooks
This commit is contained in:
@@ -5,11 +5,14 @@ namespace LocalModels;
|
||||
|
||||
public record LocalQuiz: IModuleItem
|
||||
{
|
||||
|
||||
public required string Name { get; init; }
|
||||
public required string Description { get; init; }
|
||||
public string? Password { get; init; } = null;
|
||||
public DateTime? LockAt { get; init; }
|
||||
public DateTime DueAt { get; init; }
|
||||
public bool ShuffleAnswers { get; init; } = true;
|
||||
public bool showCorrectAnswers { get; init; } = true;
|
||||
public bool OneQuestionAtATime { get; init; } = false;
|
||||
public string? LocalAssignmentGroupName { get; init; }
|
||||
public int AllowedAttempts { get; init; } = -1; // -1 is infinite
|
||||
@@ -21,6 +24,7 @@ public record LocalQuiz: IModuleItem
|
||||
// If “until_after_last_attempt”, students can only see results after their last attempt. (Only valid if allowed_attempts > 1). Defaults to null.
|
||||
public IEnumerable<LocalQuizQuestion> Questions { get; init; } =
|
||||
Enumerable.Empty<LocalQuizQuestion>();
|
||||
|
||||
public ulong? GetCanvasAssignmentGroupId(IEnumerable<LocalAssignmentGroup> assignmentGroups) =>
|
||||
assignmentGroups
|
||||
.FirstOrDefault(g => g.Name == LocalAssignmentGroupName)?
|
||||
@@ -44,7 +48,9 @@ public record LocalQuiz: IModuleItem
|
||||
return $@"Name: {Name}
|
||||
LockAt: {LockAt}
|
||||
DueAt: {DueAt}
|
||||
Password: {Password}
|
||||
ShuffleAnswers: {ShuffleAnswers.ToString().ToLower()}
|
||||
ShowCorrectAnswers: {showCorrectAnswers.ToString().ToLower()}
|
||||
OneQuestionAtATime: {OneQuestionAtATime.ToString().ToLower()}
|
||||
AssignmentGroup: {LocalAssignmentGroupName}
|
||||
AllowedAttempts: {AllowedAttempts}
|
||||
@@ -82,6 +88,18 @@ Description: {Description}
|
||||
: throw new QuizMarkdownParseException($"Error with ShuffleAnswers: {rawShuffleAnswers}");
|
||||
|
||||
|
||||
var rawPassword = extractLabelValue(settings, "Password");
|
||||
var password = rawPassword == null || rawPassword.Trim() == string.Empty
|
||||
? null
|
||||
: rawPassword;
|
||||
|
||||
|
||||
var rawShowCorrectAnswers = extractLabelValue(settings, "ShowCorrectAnswers");
|
||||
var showCorrectAnswers = bool.TryParse(rawShowCorrectAnswers, out bool parsedShowCorrectAnswers)
|
||||
? parsedShowCorrectAnswers
|
||||
: true; //default to true
|
||||
|
||||
|
||||
var rawOneQuestionAtATime = extractLabelValue(settings, "OneQuestionAtATime");
|
||||
var oneQuestionAtATime = bool.TryParse(rawOneQuestionAtATime, out bool parsedOneQuestion)
|
||||
? parsedOneQuestion
|
||||
@@ -112,9 +130,11 @@ Description: {Description}
|
||||
{
|
||||
Name = name,
|
||||
Description = description,
|
||||
Password = password,
|
||||
LockAt = lockAt,
|
||||
DueAt = dueAt,
|
||||
ShuffleAnswers = shuffleAnswers,
|
||||
showCorrectAnswers = showCorrectAnswers,
|
||||
OneQuestionAtATime = oneQuestionAtATime,
|
||||
LocalAssignmentGroupName = assignmentGroup,
|
||||
AllowedAttempts = allowedAttempts,
|
||||
|
||||
@@ -11,7 +11,7 @@ public class CanvasAssignmentGroupService
|
||||
private readonly MyLogger<CanvasAssignmentGroupService> logger;
|
||||
|
||||
public CanvasAssignmentGroupService(
|
||||
IWebRequestor webRequestor,
|
||||
IWebRequestor webRequestor,
|
||||
CanvasServiceUtils utils,
|
||||
MyLogger<CanvasAssignmentGroupService> logger
|
||||
)
|
||||
@@ -73,4 +73,4 @@ public class CanvasAssignmentGroupService
|
||||
|
||||
await webRequestor.PutAsync<CanvasAssignmentGroup>(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,6 +50,8 @@ public class CanvasQuizService(
|
||||
// assignment_group_id = "quiz", // TODO: support specific assignment groups
|
||||
// time_limit = localQuiz.TimeLimit,
|
||||
shuffle_answers = localQuiz.ShuffleAnswers,
|
||||
access_code = localQuiz.Password,
|
||||
show_correct_answers = localQuiz.showCorrectAnswers,
|
||||
// hide_results = localQuiz.HideResults,
|
||||
allowed_attempts = localQuiz.AllowedAttempts,
|
||||
one_question_at_a_time = false,
|
||||
@@ -91,11 +93,9 @@ public class CanvasQuizService(
|
||||
|
||||
private async Task hackFixRedundantAssignments(ulong canvasCourseId)
|
||||
{
|
||||
|
||||
using var activity = DiagnosticsConfig.Source.StartActivity("hack fixing redundant quiz assignments that are auto-created");
|
||||
activity?.SetTag("canvas syncronization", true);
|
||||
|
||||
|
||||
var canvasAssignments = await assignments.GetAll(canvasCourseId);
|
||||
var assignmentsToDelete = canvasAssignments
|
||||
.Where(
|
||||
@@ -113,7 +113,7 @@ public class CanvasQuizService(
|
||||
a.Name
|
||||
);
|
||||
}
|
||||
);
|
||||
).ToArray();
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@ public class WebRequestor : IWebRequestor
|
||||
private RestClient client;
|
||||
private readonly IConfiguration _config;
|
||||
private readonly ILogger<WebRequestor> logger;
|
||||
private static int rateLimitRetryCount = 6;
|
||||
private static int rateLimitSleepInterval = 1000;
|
||||
|
||||
public WebRequestor(IConfiguration config, ILogger<WebRequestor> logger)
|
||||
{
|
||||
@@ -34,39 +36,38 @@ public class WebRequestor : IWebRequestor
|
||||
return (deserialize<T>(response), response);
|
||||
}
|
||||
|
||||
public async Task<RestResponse> PostAsync(RestRequest request)
|
||||
{
|
||||
using var activity = DiagnosticsConfig.Source.StartActivity("sending post");
|
||||
activity?.AddTag("success", false);
|
||||
public async Task<RestResponse> PostAsync(RestRequest request) => await rateLimitAwarePostAsync(request, 0);
|
||||
|
||||
|
||||
private async Task<RestResponse> rateLimitAwarePostAsync(RestRequest request, int retryCount = 0)
|
||||
{
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
try
|
||||
{
|
||||
var response = await client.ExecutePostAsync(request);
|
||||
activity?.AddTag("url", response.ResponseUri);
|
||||
var response = await client.ExecutePostAsync(request);
|
||||
|
||||
if (isRateLimited(response))
|
||||
logger.LogInformation("hit rate limit");
|
||||
|
||||
if (!response.IsSuccessful)
|
||||
{
|
||||
logger.LogError($"Error with response, response content: {response.Content}", response);
|
||||
throw new Exception("error with response");
|
||||
}
|
||||
activity?.AddTag("success", true);
|
||||
return response;
|
||||
if (isRateLimited(response)) {
|
||||
if(retryCount < rateLimitRetryCount){
|
||||
logger.LogInformation($"hit rate limit on post, retry count is {retryCount} / {rateLimitRetryCount}, retrying");
|
||||
Console.WriteLine($"hit rate limit on post, retry count is {retryCount} / {rateLimitRetryCount}, retrying");
|
||||
Thread.Sleep(rateLimitSleepInterval);
|
||||
return await rateLimitAwarePostAsync(request, retryCount + 1);}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("inside post catch block");
|
||||
throw e;
|
||||
|
||||
if (!response.IsSuccessful)
|
||||
{
|
||||
logger.LogError($"Error with response, response content: {response.Content}");
|
||||
throw new Exception($"error post response, retrycount: {retryCount}, ratelimited: {isRateLimited(response)}, code: {response.StatusCode}, response content: {response.Content}");
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
private static bool isRateLimited(RestResponse response) =>
|
||||
response.StatusCode == HttpStatusCode.Forbidden && response.Content?.Contains("403 Forbidden (Rate Limit Exceeded)") != null;
|
||||
private static bool isRateLimited(RestResponse response)
|
||||
{
|
||||
if(response.Content == null)
|
||||
return false;
|
||||
return response.StatusCode == HttpStatusCode.Forbidden
|
||||
&& response.Content.Contains("403 Forbidden (Rate Limit Exceeded)");
|
||||
}
|
||||
|
||||
public async Task<(T?, RestResponse)> PostAsync<T>(RestRequest request)
|
||||
{
|
||||
@@ -95,32 +96,33 @@ public class WebRequestor : IWebRequestor
|
||||
return (deserialize<T>(response), response);
|
||||
}
|
||||
|
||||
public async Task<RestResponse> DeleteAsync(RestRequest request)
|
||||
public Task<RestResponse> DeleteAsync(RestRequest request) => recursiveDeleteAsync(request, 0);
|
||||
private async Task<RestResponse> recursiveDeleteAsync(RestRequest request, int retryCount)
|
||||
{
|
||||
// using var activity = DiagnosticsConfig.Source.StartActivity($"sending delete web request");
|
||||
// activity?.AddTag("success", false);
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
var response = await client.DeleteAsync(request);
|
||||
if (isRateLimited(response))
|
||||
Console.WriteLine("after delete response in rate limited");
|
||||
// Console.WriteLine(response.Content);
|
||||
// activity?.AddTag("url", response.ResponseUri);
|
||||
// activity?.AddTag("success", true);
|
||||
return response;
|
||||
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
if (e.StatusCode == HttpStatusCode.Forbidden) // && response.Content == "403 Forbidden (Rate Limit Exceeded)"
|
||||
logger.LogInformation("hit rate limit in delete");
|
||||
|
||||
Console.WriteLine(e.StatusCode);
|
||||
// Console.WriteLine();
|
||||
throw e;
|
||||
|
||||
{
|
||||
if(retryCount < rateLimitRetryCount)
|
||||
{
|
||||
logger.LogInformation($"hit rate limit in delete, retry count is {retryCount} / {rateLimitRetryCount}, retrying");
|
||||
Console.WriteLine($"hit rate limit in delete, retry count is {retryCount} / {rateLimitRetryCount}, retrying");
|
||||
Thread.Sleep(rateLimitSleepInterval);
|
||||
return await recursiveDeleteAsync(request, retryCount + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogInformation($"hit rate limit in delete, {rateLimitRetryCount} retries did not fix it");
|
||||
}
|
||||
}
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,4 +166,5 @@ public class WebRequestor : IWebRequestor
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user