I've created this dotnetcore library as an example to show how to get github information. It's kinda one of my pet projects so please, be nice 😇
.
├── README.md
├── docs
│ └── pull_request_template.md
└── src
├── GitHubClient
├── GitHubClientTest
├── GitHubConsoleDemo
└── github-client-sample.sln
I've created a connector to the GitHub API, I'm using the RestSharp library for get the information I need to consume and the Json.NET library to serialize it.
The GitHubConnector
class contains all you need to get the required GitHub information.
public class GitHubConnector
{
private IRestClient _restClient { get; set; }
public GitHubConnector(): this(new RestClient()) {}
public GitHubConnector(IRestClient client)
{
this._restClient = client;
this._restClient.BaseUrl = new Uri("https://api.github.com/");
}
private IRestResponse getGitHubResponse(string username)
{
var request = new RestRequest("users/{username}", Method.GET);
request.AddUrlSegment("username", username);
return _restClient.Execute(request);
}
public UserInformation GetUserInformation(string username)
{
var gitHubResponse = getGitHubResponse(username);
return JsonConvert.DeserializeObject<UserInformation>(gitHubResponse.Content);
}
}
You'll need to go into the ./src
folder and run this command:
$ dotnet build
dotnet 8.0 would be required 🤓
You'll see this result
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:01.31
This is a class library so you can run a console client I built, it's just an example and you can use the dotnet run --project GitHubConsoleDemo
command in the root folder or the dotnet run
command in the ./src/GitHubConsoleDemo
folder:
Put your GitHub username: jersson
Hello jersson, this is your GitHub information:
* Login: jersson
* FullName: Jersson Dongo
* Company: @mckinsey
* Blog: http://jersson.net
* GitHubPage: https://github.com/jersson
* PublicRepos: 26
* CreationDate: 02/20/2012 01:07:28
* LastUpdate: 10/22/2021 14:29:46
* Twitter: @jersson
You can run the dotnet test
command in the ./src
folder or the test folder:
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Passed! - Failed: 0, Passed: 3, Skipped: 0, Total: 3, Duration: 445 ms
I'm using xUnit for the test cases I've written and moq to mock the use of the RestClient
object, this is a really interesting post about moq. If you want to read about dependency injection, you can go to this post.
The last thing I've added to this project is the feature to send the information to a Sonar server. This is kinda open source project so I'm using the free access to SonarSource but you can make the commercial way or install it locally (I mean, in your cloud)
You can check the cd workflow file I've made using GitHub Actions, I thing it's easy to understand and of course, run it, but if you want to use it, don't forget to define at least these GitHub Secrets:
- SONAR_HOST_URL
- SONAR_ORGANIZATION_KEY
- SONAR_PROJECT_KEY
- SONAR_TOKEN