Created
May 4, 2021 21:14
-
-
Save drawcode/deade8600a623c8031946847d123cfeb to your computer and use it in GitHub Desktop.
dotnet-csharp-github-list-get.cs - GitHub directory and file gets.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var httpClient = new HttpClient(); | |
httpClient.DefaultRequestHeaders.UserAgent.Add( | |
new ProductInfoHeaderValue("AppSpecificHeader", "1")); | |
var repo = "{org}/{repo}"; | |
var contentsUrl = $"https://api.github.com/repos/{repo}/contents"; | |
var contentsJson = await httpClient.GetStringAsync(contentsUrl); | |
var contents = (JArray)JsonConvert.DeserializeObject(contentsJson); | |
foreach(var file in contents) | |
{ | |
var fileType = (string)file["type"]; | |
if (fileType == "dir") | |
{ | |
var directoryContentsUrl = (string)file["url"]; | |
// use this URL to list the contents of the folder | |
Console.WriteLine($"DIR: {directoryContentsUrl}"); | |
} | |
else if (fileType == "file") | |
{ | |
var downloadUrl = (string)file["download_url"]; | |
// use this URL to download the contents of the file | |
Console.WriteLine($"DOWNLOAD: {downloadUrl}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment