Reputation: 156
How can I get a list of file from GitHub link?
For example, from this GitHub repository link: https://github.com/crs2007/ActiveReport/tree/master/ActiveReport/SQLFiles
We can see that there are SQL text files:
I would like to get a list of these files:
How can I do that?
Upvotes: 4
Views: 4461
Reputation: 933
You should be able to use GitHub Contents API
By making a request like:
curl https://api.github.com/repos/crs2007/ActiveReport/contents/ActiveReport
Github will return JSON containing directory contents.
You can do this in C# in multiple ways, using something like Octokit is probably recommended as they ironed out most issues you're likely to encounter. In case you can't use an external library, the example below shows how to use plain HttpClient to achieve the same, albeit with a lot more plumbing involved:
class Program
{
static void Main()
{
Task.Factory.StartNew(async () =>
{
var repoOwner = "crs2007";
var repoName = "ActiveReport";
var path = "ActiveReport";
var httpClientResults = await ListContents(repoOwner, repoName, path);
PrintResults("From HttpClient", httpClientResults);
var octokitResults = await ListContentsOctokit(repoOwner, repoName, path);
PrintResults("From Octokit", octokitResults);
}).Wait();
Console.ReadKey();
}
static async Task<IEnumerable<string>> ListContents(string repoOwner, string repoName, string path)
{
using (var client = GetGithubHttpClient())
{
var resp = await client.GetAsync($"repos/{repoOwner}/{repoName}/contents/{path}");
var bodyString = await resp.Content.ReadAsStringAsync();
var bodyJson = JToken.Parse(bodyString);
return bodyJson.SelectTokens("$.[*].name").Select(token => token.Value<string>());
}
}
static async Task<IEnumerable<string>> ListContentsOctokit(string repoOwner, string repoName, string path)
{
var client = new GitHubClient(new ProductHeaderValue("Github-API-Test"));
// client.Credentials = ... // Set credentials here, otherwise harsh rate limits apply.
var contents = await client.Repository.Content.GetAllContents(repoOwner, repoName, path);
return contents.Select(content => content.Name);
}
private static HttpClient GetGithubHttpClient()
{
return new HttpClient
{
BaseAddress = new Uri("https://api.github.com"),
DefaultRequestHeaders =
{
// NOTE: You'll have to set up Authentication tokens in real use scenario
// NOTE: as without it you're subject to harsh rate limits.
{"User-Agent", "Github-API-Test"}
}
};
}
static void PrintResults(string source, IEnumerable<string> files)
{
Console.WriteLine(source);
foreach (var file in files)
{
Console.WriteLine($" -{file}");
}
}
}
Upvotes: 5