working on canvas sync

This commit is contained in:
2023-07-31 12:18:10 -06:00
parent 2cca727b4d
commit 1511763f14
6 changed files with 92 additions and 24 deletions

View File

@@ -123,18 +123,39 @@ public class CoursePlanner
});
var modules = await Task.WhenAll(moduleTasks);
LocalCourse = LocalCourse with
{
Modules = modules
};
LocalCourse = LocalCourse with { Modules = modules };
}
private async Task<LocalAssignment> ensureAssignmentInCanvas_returnUpdated(
LocalAssignment localAssignment
)
{
// var canvasAssignment = await canvas.
return localAssignment;
if (
LocalCourse == null
|| LocalCourse.CanvasId == null
|| CanvasAssignments == null
|| CanvasModules == null
)
throw new Exception(
"cannot create canvas assignment if local course is null or other values not set"
);
ulong canvasId = LocalCourse.CanvasId ?? throw new Exception("no canvas id to create course");
var canvasAssignment = await canvas.CreateAssignment(
courseId: canvasId,
name: localAssignment.name,
submissionTypes: localAssignment.submission_types,
description: localAssignment.description,
dueAt: localAssignment.due_at,
lockAt: localAssignment.lock_at,
pointsPossible: localAssignment.points_possible
);
Console.WriteLine(JsonSerializer.Serialize(canvasAssignment));
return localAssignment with
{
canvasId = canvasAssignment.Id
};
}
public void Clear()

View File

@@ -5,7 +5,6 @@ namespace CanvasModel.Assignments;
public record CanvasAssignment
(
[property: JsonPropertyName("id")]
ulong Id,

View File

@@ -0,0 +1,9 @@
public record CanvasAssignmentCreationRequest
{
public string? name { get; set; }
public IEnumerable<string> submission_types { get; set; } = Enumerable.Empty<string>();
public string? description { get; set; }
public DateTime? lock_at { get; set; }
public DateTime? due_at { get; set; }
public int? points_possible { get; set; }
}

View File

@@ -62,12 +62,36 @@ public class CanvasService : ICanvasService
return assignmentResponse.SelectMany(c => c);
}
// public async Task<CanvasAssignment> CreateAssignment(
// )
// {
// }
public async Task<CanvasAssignment> CreateAssignment(
ulong courseId,
string name,
IEnumerable<SubmissionType> submissionTypes,
string? description,
DateTime? dueAt,
DateTime? lockAt,
int? pointsPossible
)
{
System.Console.WriteLine($"creating assignment: {name}");
var url = $"courses/{courseId}/assignments";
var request = new RestRequest(url);
var body = new CanvasAssignmentCreationRequest()
{
name = name,
submission_types = submissionTypes.Select(t => t.ToString()),
description = description,
due_at = dueAt,
lock_at = lockAt,
points_possible = pointsPossible
};
request.AddHeader("Content-Type", "application/json");
var bodyObj = new { assignment = body };
request.AddBody(bodyObj);
var (canvasAssignment, response) = await webRequestor.PostAsync<CanvasAssignment>(request);
if (canvasAssignment == null)
throw new Exception("created canvas assignment was null");
return canvasAssignment;
}
public async Task<IEnumerable<CanvasModule>> GetModules(ulong courseId)
{

View File

@@ -15,7 +15,6 @@ public class WebRequestor : IWebRequestor
client.AddDefaultHeader("Authorization", $"Bearer {token}");
}
public async Task<(T[]?, RestResponse)> GetManyAsync<T>(RestRequest request)
{
var response = await client.ExecuteGetAsync(request);
@@ -55,8 +54,12 @@ public class WebRequestor : IWebRequestor
System.Console.WriteLine(response.ResponseUri);
System.Console.WriteLine(response.ErrorMessage);
System.Console.WriteLine("error with response");
throw new Exception("error with response");
// Console.WriteLine(JsonSerializer.Serialize(response));
Console.WriteLine(JsonSerializer.Serialize(response.Request?.Parameters));
throw new Exception($"error with response to {response.ResponseUri} {response.StatusCode}");
}
try
{
try
{
var data = JsonSerializer.Deserialize<T>(response.Content!);
@@ -69,6 +72,12 @@ public class WebRequestor : IWebRequestor
}
return data;
}
catch (System.NotSupportedException exception)
{
Console.WriteLine(response.Content);
throw exception;
}
}
catch (JsonException ex)
{
System.Console.WriteLine(response.ResponseUri);

View File

@@ -11,9 +11,15 @@ Authorization: Bearer {{$dotenv CANVAS_TOKEN}}
###
POST https://snow.instructure.com/api/v1/courses/872095/assignments
Content-Type: application/json
Authorization: Bearer {{$dotenv CANVAS_TOKEN}}
{"assignment":{"name:":"New Assignment","description:":"This is a new assignment"}}
{
"assignment": {
"name:": "New Assignment",
"description:": "This is a new assignment"
}
}
###
POST https://snow.instructure.com/api/v1/courses/705168/assignments