1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
+ using System . Net ;
4
5
using System . Threading . Tasks ;
5
6
using RestSharp ;
6
7
using RestSharp . Authenticators ;
@@ -12,7 +13,7 @@ namespace Git.hub
12
13
/// </summary>
13
14
public class Client
14
15
{
15
- private RestClient client ;
16
+ private readonly RestClient _client ;
16
17
17
18
/// <summary>
18
19
/// Creates a new client instance for github.com
@@ -25,8 +26,7 @@ public Client() : this("https://api.github.com") { }
25
26
/// <param name="apiEndpoint">the host to connect to, e.g. 'https://api.github.com'</param>
26
27
public Client ( string apiEndpoint )
27
28
{
28
- client = new RestClient ( apiEndpoint ) ;
29
- client . UserAgent = "mabako/Git.hub" ;
29
+ _client = new RestClient ( apiEndpoint ) { UserAgent = "mabako/Git.hub" } ;
30
30
}
31
31
32
32
/// <summary>
@@ -36,10 +36,8 @@ public Client(string apiEndpoint)
36
36
/// <param name="password">password</param>
37
37
public void setCredentials ( string user , string password )
38
38
{
39
- if ( user != null && password != null )
40
- client . Authenticator = new HttpBasicAuthenticator ( user , password ) ;
41
- else
42
- client . Authenticator = null ;
39
+ _client . Authenticator =
40
+ user != null && password != null ? new HttpBasicAuthenticator ( user , password ) : null ;
43
41
}
44
42
45
43
/// <summary>
@@ -48,10 +46,7 @@ public void setCredentials(string user, string password)
48
46
/// <param name="token">oauth2-token</param>
49
47
public void setOAuth2Token ( string token )
50
48
{
51
- if ( token != null )
52
- client . Authenticator = new OAuth2AuthHelper ( token ) ;
53
- else
54
- client . Authenticator = null ;
49
+ _client . Authenticator = token != null ? new OAuth2AuthHelper ( token ) : null ;
55
50
}
56
51
57
52
/// <summary>
@@ -60,16 +55,16 @@ public void setOAuth2Token(string token)
60
55
/// <returns>list of repositories</returns>
61
56
public IList < Repository > getRepositories ( )
62
57
{
63
- if ( client . Authenticator == null )
58
+ if ( _client . Authenticator == null )
64
59
throw new ArgumentException ( "no authentication details" ) ;
65
60
66
61
var request = new RestRequest ( "/user/repos?type=all" ) ;
67
62
68
- var repos = client . GetList < Repository > ( request ) ;
63
+ var repos = _client . GetList < Repository > ( request ) ;
69
64
if ( repos == null )
70
65
throw new Exception ( "Bad Credentials" ) ;
71
66
72
- repos . ForEach ( r => r . _client = client ) ;
67
+ repos . ForEach ( r => r . _client = _client ) ;
73
68
return repos ;
74
69
}
75
70
@@ -80,14 +75,14 @@ public IList<Repository> getRepositories()
80
75
/// <returns>list of repositories</returns>
81
76
public IList < Repository > getRepositories ( string username )
82
77
{
83
- var request = new RestRequest ( "/users/{name}/repos" ) ;
84
- request . AddUrlSegment ( "name" , username ) ;
78
+ var request = new RestRequest ( "/users/{name}/repos" )
79
+ . AddUrlSegment ( "name" , username ) ;
85
80
86
- var list = client . GetList < Repository > ( request ) ;
81
+ var list = _client . GetList < Repository > ( request ) ;
87
82
if ( list == null )
88
83
throw new InvalidOperationException ( "User does not exist." ) ;
89
84
90
- list . ForEach ( r => r . _client = client ) ;
85
+ list . ForEach ( r => r . _client = _client ) ;
91
86
return list ;
92
87
}
93
88
@@ -99,15 +94,15 @@ public IList<Repository> getRepositories(string username)
99
94
/// <returns>fetched repository</returns>
100
95
public Repository getRepository ( string username , string repositoryName )
101
96
{
102
- var request = new RestRequest ( "/repos/{name}/{repo}" ) ;
103
- request . AddUrlSegment ( "name" , username ) ;
104
- request . AddUrlSegment ( "repo" , repositoryName ) ;
97
+ var request = new RestRequest ( "/repos/{name}/{repo}" )
98
+ . AddUrlSegment ( "name" , username )
99
+ . AddUrlSegment ( "repo" , repositoryName ) ;
105
100
106
- var repo = client . Get < Repository > ( request ) . Data ;
101
+ var repo = DoRequest < Repository > ( request ) ;
107
102
if ( repo == null )
108
103
return null ;
109
104
110
- repo . _client = client ;
105
+ repo . _client = _client ;
111
106
repo . Detailed = true ;
112
107
return repo ;
113
108
}
@@ -119,13 +114,13 @@ public Repository getRepository(string username, string repositoryName)
119
114
/// <returns></returns>
120
115
public IList < Repository > getOrganizationRepositories ( string organization )
121
116
{
122
- var request = new RestRequest ( "/orgs/{org}/repos" ) ;
123
- request . AddUrlSegment ( "org" , organization ) ;
117
+ var request = new RestRequest ( "/orgs/{org}/repos" )
118
+ . AddUrlSegment ( "org" , organization ) ;
124
119
125
- var list = client . GetList < Repository > ( request ) ;
120
+ var list = _client . GetList < Repository > ( request ) ;
126
121
127
- Organization org = new Organization { Login = organization } ;
128
- list . ForEach ( r => { r . _client = client ; r . Organization = org ; } ) ;
122
+ var org = new Organization { Login = organization } ;
123
+ list . ForEach ( r => { r . _client = _client ; r . Organization = org ; } ) ;
129
124
return list ;
130
125
}
131
126
@@ -137,16 +132,14 @@ public IList<Repository> getOrganizationRepositories(string organization)
137
132
/// <returns>current user</returns>
138
133
public User getCurrentUser ( )
139
134
{
140
- if ( client . Authenticator == null )
135
+ if ( _client . Authenticator == null )
141
136
throw new ArgumentException ( "no authentication details" ) ;
142
137
143
138
var request = new RestRequest ( "/user" ) ;
144
139
145
- var user = client . Get < User > ( request ) ;
146
- if ( user . Data == null )
147
- throw new Exception ( "Bad Credentials" ) ;
140
+ var user = DoRequest < User > ( request , false ) ;
148
141
149
- return user . Data ;
142
+ return user ;
150
143
}
151
144
152
145
public async Task < User > GetUserAsync ( string userName )
@@ -158,7 +151,7 @@ public async Task<User> GetUserAsync(string userName)
158
151
159
152
var request = new RestRequest ( $ "/users/{ userName } ") ;
160
153
161
- var user = await client . ExecuteGetTaskAsync < User > ( request ) ;
154
+ var user = await _client . ExecuteGetTaskAsync < User > ( request ) ;
162
155
return user . Data ;
163
156
}
164
157
@@ -172,11 +165,39 @@ public List<Repository> searchRepositories(string query)
172
165
var request = new RestRequest ( "/legacy/repos/search/{query}" ) ;
173
166
request . AddUrlSegment ( "query" , query ) ;
174
167
175
- var repos = client . Get < APIv2 . RepositoryListV2 > ( request ) . Data ;
176
- if ( repos == null || repos . Repositories == null )
177
- throw new Exception ( string . Format ( "Could not search for {0}" , query ) ) ;
168
+ var repos = DoRequest < APIv2 . RepositoryListV2 > ( request ) ;
169
+ if ( repos ? . Repositories == null )
170
+ {
171
+ throw new Exception ( $ "Could not search for { query } ") ;
172
+ }
173
+
174
+ return repos . Repositories . Select ( r => r . ToV3 ( _client ) ) . ToList ( ) ;
175
+ }
176
+
177
+ private T DoRequest < T > ( IRestRequest request , bool throwOnError = true ) where T : new ( )
178
+ {
179
+ var response = _client . Get < T > ( request ) ;
180
+ if ( response . IsSuccessful )
181
+ {
182
+ return response . Data ;
183
+ }
184
+
185
+ if ( ! throwOnError )
186
+ {
187
+ return default ;
188
+ }
189
+
190
+ if ( response . StatusCode == HttpStatusCode . Unauthorized )
191
+ {
192
+ if ( _client . Authenticator == null )
193
+ {
194
+ throw new UnauthorizedAccessException ( "Please configure a GitHub authentication token." ) ;
195
+ }
196
+
197
+ throw new UnauthorizedAccessException ( "The GitHub authentication token provided is not valid." ) ;
198
+ }
178
199
179
- return repos . Repositories . Select ( r => r . ToV3 ( client ) ) . ToList ( ) ;
200
+ throw new Exception ( response . StatusDescription ) ;
180
201
}
181
202
}
182
203
}
0 commit comments