Skip to content
This repository has been archived by the owner on Sep 4, 2023. It is now read-only.

Fix outcomes being capped at 100 #21

Merged
merged 3 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions Epsilon.Canvas.Abstractions/Data/LinkHeader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Text.RegularExpressions;

public class LinkHeader
{
public string FirstLink { get; set; }
public string PrevLink { get; set; }
public string NextLink { get; set; }
public string LastLink { get; set; }

public static LinkHeader LinksFromHeader(HttpResponseMessage httpResponseMessage)
{
if (httpResponseMessage.Headers.Contains("Link"))
{
return LinkHeader.LinksFromHeader(httpResponseMessage.Headers.GetValues("Link").FirstOrDefault());
}

return null;
}

public static LinkHeader LinksFromHeader(string linkHeaderStr)
{
LinkHeader linkHeader = null;

if (!string.IsNullOrWhiteSpace(linkHeaderStr))
{
string[] linkStrings = linkHeaderStr.Split(',');

if (linkStrings != null && linkStrings.Any())
{
linkHeader = new LinkHeader();

foreach (string linkString in linkStrings)
{
var relMatch = Regex.Match(linkString, "(?<=rel=\").+?(?=\")", RegexOptions.IgnoreCase);
var linkMatch = Regex.Match(linkString, "(?<=<).+?(?=>)", RegexOptions.IgnoreCase);

if (relMatch.Success && linkMatch.Success)
{
string rel = relMatch.Value.ToUpper();
string link = linkMatch.Value;

switch (rel)
{
case "FIRST":
linkHeader.FirstLink = link;
break;
case "PREV":
linkHeader.PrevLink = link;
break;
case "NEXT":
linkHeader.NextLink = link;
break;
case "LAST":
linkHeader.LastLink = link;
break;
}
}
}
}
}

return linkHeader;
}
}
2 changes: 1 addition & 1 deletion Epsilon.Canvas/Response/OutcomeResultResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ namespace Epsilon.Canvas.Response;

public record OutcomeResultResponse(
[property: JsonPropertyName("outcome_results")] IEnumerable<OutcomeResult> OutcomeResults
);
);
25 changes: 20 additions & 5 deletions Epsilon.Canvas/Service/OutcomeService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Epsilon.Canvas.Abstractions.Data;
using Epsilon.Canvas.Abstractions.Data;
using Epsilon.Canvas.Abstractions.Services;
using Epsilon.Canvas.Response;
using Epsilon.Http.Abstractions;
using Epsilon.Http.Abstractions.Json;
using Microsoft.Extensions.Logging;
using System.Text.Json;

namespace Epsilon.Canvas.Service;

Expand All @@ -22,9 +24,22 @@ public OutcomeService(HttpClient client) : base(client)

public async Task<IEnumerable<OutcomeResult>?> AllResults(int courseId, int count = 1000)
{
var request = new HttpRequestMessage(HttpMethod.Get, $"v1/courses/{courseId}/outcome_results?per_page={count}");
var (_, value) = await Client.SendAsync<OutcomeResultResponse>(request);
IEnumerable<OutcomeResult>? res = null;
var page = 1;
do {
var request = new HttpRequestMessage(HttpMethod.Get, $"v1/courses/{courseId}/outcome_results?per_page={count}&offset={res?.Count() ?? 0}&page={page}");
var (response, value) = await Client.SendAsync<OutcomeResultResponse>(request);
var links = LinkHeader.LinksFromHeader(response);

return value?.OutcomeResults;
res = res == null ? value?.OutcomeResults : res.Concat(value.OutcomeResults);

if (links.NextLink == null) {
break;
}

page += 1;
} while (res.Count() % 100 == 0);

return res;
}
}
}