mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-26 07:38:33 -06:00
commit pre-purge
This commit is contained in:
@@ -6,4 +6,8 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="RestSharp" Version="108.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
|
||||
|
||||
namespace CanvasModel.Courses;
|
||||
public struct CalendarLinkModel
|
||||
{
|
||||
[JsonPropertyName("ics")]
|
||||
public string Ics { get; set; }
|
||||
}
|
||||
public record CalendarLinkModel
|
||||
(
|
||||
[property: JsonPropertyName("ics")] string Ics
|
||||
);
|
||||
@@ -1,17 +1,8 @@
|
||||
|
||||
namespace CanvasModel.Courses;
|
||||
public class TermModel
|
||||
{
|
||||
|
||||
[JsonPropertyName("id")]
|
||||
public ulong Id { get; set; }
|
||||
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("start_at")]
|
||||
public DateTime? StartAt { get; set; }
|
||||
|
||||
[JsonPropertyName("end_at")]
|
||||
public DateTime? EndAt { get; set; }
|
||||
}
|
||||
public record TermModel
|
||||
(
|
||||
[property: JsonPropertyName("id")] ulong Id,
|
||||
[property: JsonPropertyName("name")] string Name,
|
||||
[property: JsonPropertyName("start_at")] DateTime? StartAt,
|
||||
[property: JsonPropertyName("end_at")] DateTime? EndAt
|
||||
);
|
||||
|
||||
@@ -1,43 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
namespace CanvasModel.EnrollmentTerms;
|
||||
public class EnrollmentTermModel
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public ulong Id { get; set; }
|
||||
|
||||
[JsonPropertyName("sis_term_id")]
|
||||
public string SisTermId { get; set; }
|
||||
public record EnrollmentTermModel
|
||||
(
|
||||
[property: JsonPropertyName("id")] ulong Id,
|
||||
[property: JsonPropertyName("name")] string Name,
|
||||
[property: JsonPropertyName("sis_term_id")] string? SisTermId = null,
|
||||
[property: JsonPropertyName("sis_import_id")] ulong? SisImportId = null,
|
||||
[property: JsonPropertyName("start_at")] DateTime? StartAt = null,
|
||||
[property: JsonPropertyName("end_at")] DateTime? EndAt = null,
|
||||
[property: JsonPropertyName("grading_period_group_id")] ulong? GradingPeriodGroupId = null,
|
||||
[property: JsonPropertyName("workflow_state")] string? WorkflowState = null,
|
||||
[property: JsonPropertyName("overrides")]
|
||||
Dictionary<string, EnrollmentTermDateOverrideModel>? Overrides = null
|
||||
);
|
||||
|
||||
[JsonPropertyName("sis_import_id")]
|
||||
public ulong? SisImportId { get; set; }
|
||||
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("start_at")]
|
||||
public DateTime? StartAt { get; set; }
|
||||
|
||||
[JsonPropertyName("end_at")]
|
||||
public DateTime? EndAt { get; set; }
|
||||
|
||||
[JsonPropertyName("grading_period_group_id")]
|
||||
public ulong? GradingPeriodGroupId { get; set; }
|
||||
|
||||
[JsonPropertyName("workflow_state")]
|
||||
public string WorkflowState { get; set; }
|
||||
|
||||
[JsonPropertyName("overrides")]
|
||||
public Dictionary<string, EnrollmentTermDateOverrideModel> Overrides { get; set; }
|
||||
}
|
||||
|
||||
public struct EnrollmentTermDateOverrideModel
|
||||
{
|
||||
[JsonPropertyName("start_at")]
|
||||
public DateTime? StartAt { get; set; }
|
||||
|
||||
[JsonPropertyName("end_at")]
|
||||
public DateTime? EndAt { get; set; }
|
||||
}
|
||||
public record EnrollmentTermDateOverrideModel
|
||||
(
|
||||
[property: JsonPropertyName("start_at")] DateTime? StartAt = null,
|
||||
[property: JsonPropertyName("end_at")] DateTime? EndAt = null
|
||||
);
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
namespace CanvasModel.EnrollmentTerms;
|
||||
public struct RedundantEnrollmentTermsResponse
|
||||
{
|
||||
[JsonPropertyName("enrollment_terms")]
|
||||
public IEnumerable<EnrollmentTermModel> EnrollmentTerms { get; set; }
|
||||
}
|
||||
public record RedundantEnrollmentTermsResponse
|
||||
(
|
||||
[property: JsonPropertyName("enrollment_terms")]
|
||||
IEnumerable<EnrollmentTermModel> EnrollmentTerms
|
||||
);
|
||||
|
||||
@@ -1,4 +1,68 @@
|
||||
using CanvasModel.Courses;
|
||||
using CanvasModel.EnrollmentTerms;
|
||||
using RestSharp;
|
||||
public class CanvasService
|
||||
{
|
||||
|
||||
private const string BaseUrl = "https://snow.instructure.com/api/v1/";
|
||||
private readonly IWebRequestor webRequestor;
|
||||
private RestClient client;
|
||||
private string courseid { get; }
|
||||
public CanvasService(IWebRequestor webRequestor)
|
||||
{
|
||||
courseid = "774898";
|
||||
this.webRequestor = webRequestor;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<EnrollmentTermModel>> GetTerms()
|
||||
{
|
||||
var url = $"accounts/10/terms";
|
||||
|
||||
var request = new RestRequest(url);
|
||||
var terms = await PaginatedRequest<EnrollmentTermModel>(request);
|
||||
return terms;
|
||||
}
|
||||
|
||||
private async Task<IEnumerable<T>> PaginatedRequest<T>(RestRequest request)
|
||||
{
|
||||
var requestCount = 1;
|
||||
request.AddQueryParameter("per_page", "100");
|
||||
IEnumerable<T> returnData = new T[] { };
|
||||
RestResponse<T[]> response = await webRequestor.GetAsync<T>(request);
|
||||
returnData = returnData.Concat(response.Data);
|
||||
|
||||
var nextUrl = getNextUrl(response);
|
||||
|
||||
while (nextUrl is not null)
|
||||
{
|
||||
requestCount += 1;
|
||||
var nextRequest = new RestRequest(nextUrl);
|
||||
var nextResponse = await webRequestor.GetAsync<T>(nextRequest);
|
||||
if (nextResponse.Data is not null)
|
||||
returnData = returnData.Concat(nextResponse.Data);
|
||||
nextUrl = getNextUrl(nextResponse);
|
||||
}
|
||||
|
||||
System.Console.WriteLine($"Requesting {typeof(T)} took {requestCount} requests");
|
||||
|
||||
return returnData;
|
||||
}
|
||||
|
||||
|
||||
private static string? getNextUrl<T>(RestResponse<T[]> response) => response.Headers?
|
||||
.ToList()
|
||||
.Find(h => h.Name == "Link")?
|
||||
.Value?
|
||||
.ToString()?
|
||||
.Split(",")
|
||||
.Where(url => url.Contains("rel=\"next\""))
|
||||
.FirstOrDefault()?
|
||||
.Split(";")
|
||||
.FirstOrDefault()?
|
||||
.TrimEnd('>')
|
||||
.TrimStart('<')
|
||||
.Replace(" ", "")
|
||||
.Replace(BaseUrl, "");
|
||||
|
||||
|
||||
|
||||
}
|
||||
7
Management/Services/IWebRequestor.cs
Normal file
7
Management/Services/IWebRequestor.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
using RestSharp;
|
||||
|
||||
public interface IWebRequestor
|
||||
{
|
||||
Task<RestResponse<T[]>> GetManyAsync<T>(RestRequest request);
|
||||
Task<RestResponse<T>> GetAsync<T>(RestRequest request);
|
||||
}
|
||||
26
Management/Services/WebRequestor.cs
Normal file
26
Management/Services/WebRequestor.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using RestSharp;
|
||||
|
||||
public class WebRequestor : IWebRequestor
|
||||
{
|
||||
private const string BaseUrl = "https://snow.instructure.com/api/v1/";
|
||||
private string token;
|
||||
private RestClient client;
|
||||
private string courseid { get; }
|
||||
public WebRequestor(RestClient client)
|
||||
{
|
||||
// token = Environment.GetEnvironmentVariable("CANVAS_TOKEN");
|
||||
// client = new RestClient(BaseUrl);
|
||||
// client.AddDefaultHeader("Authorization", $"Bearer {token}");
|
||||
|
||||
this.client = client;
|
||||
courseid = "774898";
|
||||
}
|
||||
public async Task<RestResponse<T[]>> GetManyAsync<T>(RestRequest request)
|
||||
{
|
||||
return await client.ExecuteGetAsync<T[]>(request);
|
||||
}
|
||||
public async Task<RestResponse<T>> GetAsync<T>(RestRequest request)
|
||||
{
|
||||
return await client.ExecuteGetAsync<T>(request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user