Created
May 23, 2014 13:50
-
-
Save yemrekeskin/0a4f78d3e78df8e55df9 to your computer and use it in GitHub Desktop.
GithubGateway
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
using System; | |
using System.Collections.Generic; | |
using System.Data; | |
using System.Linq; | |
using ServiceStack; | |
using ServiceStack.Configuration; | |
using ServiceStack.Redis; | |
using ServiceStack.Text; | |
namespace GithubApi | |
{ | |
public class Config | |
{ | |
public string GitHubName { get; set; } | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var appSettings = new AppSettings(); | |
var config = appSettings.Get("my.config", new Config { GitHubName = "mythz" }); | |
var github = new GithubGateway(); | |
var repos = github.GetAllUserAndOrgsReposFor(config.GitHubName); | |
foreach (var item in repos) | |
{ | |
Console.WriteLine(item.Name); | |
} | |
Console.ReadLine(); | |
} | |
} | |
public class GithubGateway | |
{ | |
public const string GithubApiBaseUrl = "https://api.github.com/"; | |
public T GetJson<T>(string route, params object[] routeArgs) | |
{ | |
return GithubApiBaseUrl.AppendPath(route.Fmt(routeArgs)) | |
.GetJsonFromUrl(req => req.UserAgent = "ServiceStack Poco/Power") | |
.FromJson<T>(); | |
} | |
public List<GithubOrg> GetUserOrgs(string githubUsername) | |
{ | |
return GetJson<List<GithubOrg>>("users/{0}/orgs", githubUsername); | |
} | |
public List<GithubRepo> GetUserRepos(string githubUsername) | |
{ | |
return GetJson<List<GithubRepo>>("users/{0}/repos", githubUsername); | |
} | |
public List<GithubRepo> GetOrgRepos(string githubOrgName) | |
{ | |
return GetJson<List<GithubRepo>>("orgs/{0}/repos", githubOrgName); | |
} | |
public List<GithubRepo> GetAllUserAndOrgsReposFor(string githubUsername) | |
{ | |
var map = new Dictionary<int, GithubRepo>(); | |
GetUserRepos(githubUsername).ForEach(x => map[x.Id] = x); | |
GetUserOrgs(githubUsername).ForEach(org => | |
GetOrgRepos(org.Login) | |
.ForEach(repo => map[repo.Id] = repo)); | |
return map.Values.ToList(); | |
} | |
} | |
public class GithubRepo | |
{ | |
public int Id { get; set; } | |
public string Open_Issues { get; set; } | |
public int Watchers { get; set; } | |
public DateTime? Pushed_At { get; set; } | |
public string Homepage { get; set; } | |
public string Svn_Url { get; set; } | |
public DateTime? Updated_At { get; set; } | |
public string Mirror_Url { get; set; } | |
public bool Has_Downloads { get; set; } | |
public string Url { get; set; } | |
public bool Has_issues { get; set; } | |
public string Language { get; set; } | |
public bool Fork { get; set; } | |
public string Ssh_Url { get; set; } | |
public string Html_Url { get; set; } | |
public int Forks { get; set; } | |
public string Clone_Url { get; set; } | |
public int Size { get; set; } | |
public string Git_Url { get; set; } | |
public bool Private { get; set; } | |
public DateTime Created_at { get; set; } | |
public bool Has_Wiki { get; set; } | |
public GithubUser Owner { get; set; } | |
public string Name { get; set; } | |
public string Description { get; set; } | |
} | |
public class GithubUser | |
{ | |
public int Id { get; set; } | |
public string Login { get; set; } | |
public string Avatar_Url { get; set; } | |
public string Url { get; set; } | |
public int? Followers { get; set; } | |
public int? Following { get; set; } | |
public string Type { get; set; } | |
public int? Public_Gists { get; set; } | |
public string Location { get; set; } | |
public string Company { get; set; } | |
public string Html_Url { get; set; } | |
public int? Public_Repos { get; set; } | |
public DateTime? Created_At { get; set; } | |
public string Blog { get; set; } | |
public string Email { get; set; } | |
public string Name { get; set; } | |
public bool? Hireable { get; set; } | |
public string Gravatar_Id { get; set; } | |
public string Bio { get; set; } | |
} | |
public class GithubOrg | |
{ | |
public int Id { get; set; } | |
public string Avatar_Url { get; set; } | |
public string Url { get; set; } | |
public string Login { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment