This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 630
/
GithubV3ApiTests.cs
161 lines (143 loc) · 6.39 KB
/
GithubV3ApiTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
namespace ServiceStack.Text.Tests.UseCases
{
/// <summary>
/// Stand-alone C# client for the Github v3 API
/// Uses only ServiceStack.Text (+NUnit for tests)
/// </summary>
[TestFixture, Ignore("Requires 3rd Party API")]
public class GithubV3ApiGatewayTests
{
[Test]
public void DO_ALL_THE_THINGS()
{
var client = new GithubV3ApiGateway();
Console.WriteLine("\n-- GetUserRepos(mythz): \n" + client.GetUserRepos("mythz").Dump());
Console.WriteLine("\n-- GetOrgRepos(ServiceStack): \n" + client.GetOrgRepos("ServiceStack").Dump());
Console.WriteLine("\n-- GetUserRepo(ServiceStack,ServiceStack.Text): \n" + client.GetUserRepo("mythz", "jquip").Dump());
Console.WriteLine("\n-- GetUserRepoContributors(ServiceStack,ServiceStack.Text): \n" + client.GetUserRepoContributors("ServiceStack", "ServiceStack.Text").Dump());
Console.WriteLine("\n-- GetUserRepoWatchers(ServiceStack,ServiceStack.Text): \n" + client.GetUserRepoWatchers("ServiceStack", "ServiceStack.Text").Dump());
Console.WriteLine("\n-- GetReposUserIsWatching(mythz): \n" + client.GetReposUserIsWatching("mythz").Dump());
Console.WriteLine("\n-- GetUserOrgs(mythz): \n" + client.GetUserOrgs("mythz").Dump());
Console.WriteLine("\n-- GetUserFollowers(mythz): \n" + client.GetUserFollowers("mythz").Dump());
Console.WriteLine("\n-- GetOrgMembers(ServiceStack): \n" + client.GetOrgMembers("ServiceStack").Dump());
Console.WriteLine("\n-- GetAllUserAndOrgsReposFor(mythz): \n" + client.GetAllUserAndOrgsReposFor("mythz").Dump());
}
}
public class GithubV3ApiGateway
{
public const string GithubApiBaseUrl = "https://api.github.com/";
public T GetJson<T>(string route, params object[] routeArgs)
{
var url = GithubApiBaseUrl.AppendUrlPathsRaw(route.Fmt(routeArgs));
return url
.GetJsonFromUrl(requestFilter:x => x.With(c => c.UserAgent = $"ServiceStack/{Env.VersionString}"))
.FromJson<T>();
}
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 GithubRepo GetUserRepo(string githubUsername, string projectName)
{
return GetJson<GithubRepo>("repos/{0}/{1}", githubUsername, projectName);
}
public List<GithubUser> GetUserRepoContributors(string githubUsername, string projectName)
{
return GetJson<List<GithubUser>>("repos/{0}/{1}/contributors", githubUsername, projectName);
}
public List<GithubUser> GetUserRepoWatchers(string githubUsername, string projectName)
{
return GetJson<List<GithubUser>>("repos/{0}/{1}/watchers", githubUsername, projectName);
}
public List<GithubRepo> GetReposUserIsWatching(string githubUsername)
{
return GetJson<List<GithubRepo>>("users/{0}/watched", githubUsername);
}
public List<GithubOrg> GetUserOrgs(string githubUsername)
{
return GetJson<List<GithubOrg>>("users/{0}/orgs", githubUsername);
}
public List<GithubUser> GetUserFollowers(string githubUsername)
{
return GetJson<List<GithubUser>>("users/{0}/followers", githubUsername);
}
public List<GithubUser> GetOrgMembers(string githubOrgName)
{
return GetJson<List<GithubUser>>("orgs/{0}/members", 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; }
}
}