patterncsharpMinor
Multiple calls to the GitHub API
Viewed 0 times
callsthegithubmultipleapi
Problem
I have the following class:
where
I populate my objects by calling the following code:
This works, but just takes 7.640 seconds on average according to
Is there a better way to either run the loop or should I go with
public class IssueWithComments : Issue
{
public IReadOnlyList FullComments { get; set; }
public IssueWithComments()
: this(new List())
{
}
public IssueWithComments(IEnumerable comments)
{
FullComments = comments.ToList();
}
public static IssueWithComments FromIssue(Issue issue, IEnumerable comments)
{
return new IssueWithComments(comments)
{
Assignee = issue.Assignee,
Body = issue.Body,
ClosedAt = issue.ClosedAt,
Comments = issue.Comments,
CreatedAt = issue.CreatedAt,
HtmlUrl = issue.HtmlUrl,
Labels = issue.Labels,
Milestone = issue.Milestone,
Number = issue.Number,
PullRequest = issue.PullRequest,
State = issue.State,
Title = issue.Title,
UpdatedAt = issue.UpdatedAt,
Url = issue.Url,
User = issue.User
};
}
}where
Issue is a type defined in the Octokit.Net namespace.I populate my objects by calling the following code:
public static async Task> GetAllForRepositoryWithComments(this IIssuesClient client, string owner, string name)
{
var issues = await client.GetAllForRepository(owner, name);
List all = new List();
foreach (var i in issues)
{
all.Add(IssueWithComments.FromIssue(i, await client.Comment.GetAllForIssue(owner, name, i.Number)));
}
return all;
}This works, but just takes 7.640 seconds on average according to
System.Diagnostics.Stopwatch (plus that I can see it taking a long time to load).Task.WhenAll could work, but the way to find the Issue it is referring to is as follows:x.FullComments.First().HtmlUrl.Segments[4]Is there a better way to either run the loop or should I go with
Task.WhenAll and drill into the URL?Solution
Since you're loading all comments for all issues, I think
is what you're looking for. Otherwise, if you do need to use
await client.Comment.GetAllForRepository(owner, name)is what you're looking for. Otherwise, if you do need to use
GetAllForIssue, try using Task.WhenAll to run comments retrieval in parallel:public static async Task> GetAllForRepositoryWithComments(IIssuesClient client, string owner, string name)
{
var issues = await client.GetAllForRepository(owner, name);
return await Task.WhenAll(issues.Select(async i => IssueWithComments.FromIssue(i, await client.Comment.GetAllForIssue(owner, name, i.Number)));
}Code Snippets
await client.Comment.GetAllForRepository(owner, name)public static async Task<IReadOnlyList<IssueWithComments>> GetAllForRepositoryWithComments(IIssuesClient client, string owner, string name)
{
var issues = await client.GetAllForRepository(owner, name);
return await Task.WhenAll(issues.Select(async i => IssueWithComments.FromIssue(i, await client.Comment.GetAllForIssue(owner, name, i.Number)));
}Context
StackExchange Code Review Q#92402, answer score: 2
Revisions (0)
No revisions yet.