11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4+ using System . Net ;
45using System . Threading . Tasks ;
56using RestSharp ;
67using RestSharp . Authenticators ;
@@ -12,7 +13,7 @@ namespace Git.hub
1213 /// </summary>
1314 public class Client
1415 {
15- private RestClient client ;
16+ private readonly RestClient _client ;
1617
1718 /// <summary>
1819 /// Creates a new client instance for github.com
@@ -25,8 +26,7 @@ public Client() : this("https://api.github.com") { }
2526 /// <param name="apiEndpoint">the host to connect to, e.g. 'https://api.github.com'</param>
2627 public Client ( string apiEndpoint )
2728 {
28- client = new RestClient ( apiEndpoint ) ;
29- client . UserAgent = "mabako/Git.hub" ;
29+ _client = new RestClient ( apiEndpoint ) { UserAgent = "mabako/Git.hub" } ;
3030 }
3131
3232 /// <summary>
@@ -36,10 +36,8 @@ public Client(string apiEndpoint)
3636 /// <param name="password">password</param>
3737 public void setCredentials ( string user , string password )
3838 {
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 ;
4341 }
4442
4543 /// <summary>
@@ -48,10 +46,7 @@ public void setCredentials(string user, string password)
4846 /// <param name="token">oauth2-token</param>
4947 public void setOAuth2Token ( string token )
5048 {
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 ;
5550 }
5651
5752 /// <summary>
@@ -60,16 +55,16 @@ public void setOAuth2Token(string token)
6055 /// <returns>list of repositories</returns>
6156 public IList < Repository > getRepositories ( )
6257 {
63- if ( client . Authenticator == null )
58+ if ( _client . Authenticator == null )
6459 throw new ArgumentException ( "no authentication details" ) ;
6560
6661 var request = new RestRequest ( "/user/repos?type=all" ) ;
6762
68- var repos = client . GetList < Repository > ( request ) ;
63+ var repos = _client . GetList < Repository > ( request ) ;
6964 if ( repos == null )
7065 throw new Exception ( "Bad Credentials" ) ;
7166
72- repos . ForEach ( r => r . _client = client ) ;
67+ repos . ForEach ( r => r . _client = _client ) ;
7368 return repos ;
7469 }
7570
@@ -80,14 +75,14 @@ public IList<Repository> getRepositories()
8075 /// <returns>list of repositories</returns>
8176 public IList < Repository > getRepositories ( string username )
8277 {
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 ) ;
8580
86- var list = client . GetList < Repository > ( request ) ;
81+ var list = _client . GetList < Repository > ( request ) ;
8782 if ( list == null )
8883 throw new InvalidOperationException ( "User does not exist." ) ;
8984
90- list . ForEach ( r => r . _client = client ) ;
85+ list . ForEach ( r => r . _client = _client ) ;
9186 return list ;
9287 }
9388
@@ -99,15 +94,15 @@ public IList<Repository> getRepositories(string username)
9994 /// <returns>fetched repository</returns>
10095 public Repository getRepository ( string username , string repositoryName )
10196 {
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 ) ;
105100
106- var repo = client . Get < Repository > ( request ) . Data ;
101+ var repo = DoRequest < Repository > ( request ) ;
107102 if ( repo == null )
108103 return null ;
109104
110- repo . _client = client ;
105+ repo . _client = _client ;
111106 repo . Detailed = true ;
112107 return repo ;
113108 }
@@ -119,13 +114,13 @@ public Repository getRepository(string username, string repositoryName)
119114 /// <returns></returns>
120115 public IList < Repository > getOrganizationRepositories ( string organization )
121116 {
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 ) ;
124119
125- var list = client . GetList < Repository > ( request ) ;
120+ var list = _client . GetList < Repository > ( request ) ;
126121
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 ; } ) ;
129124 return list ;
130125 }
131126
@@ -137,16 +132,14 @@ public IList<Repository> getOrganizationRepositories(string organization)
137132 /// <returns>current user</returns>
138133 public User getCurrentUser ( )
139134 {
140- if ( client . Authenticator == null )
135+ if ( _client . Authenticator == null )
141136 throw new ArgumentException ( "no authentication details" ) ;
142137
143138 var request = new RestRequest ( "/user" ) ;
144139
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 ) ;
148141
149- return user . Data ;
142+ return user ;
150143 }
151144
152145 public async Task < User > GetUserAsync ( string userName )
@@ -158,7 +151,7 @@ public async Task<User> GetUserAsync(string userName)
158151
159152 var request = new RestRequest ( $ "/users/{ userName } ") ;
160153
161- var user = await client . ExecuteGetTaskAsync < User > ( request ) ;
154+ var user = await _client . ExecuteGetTaskAsync < User > ( request ) ;
162155 return user . Data ;
163156 }
164157
@@ -172,11 +165,39 @@ public List<Repository> searchRepositories(string query)
172165 var request = new RestRequest ( "/legacy/repos/search/{query}" ) ;
173166 request . AddUrlSegment ( "query" , query ) ;
174167
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+ }
178199
179- return repos . Repositories . Select ( r => r . ToV3 ( client ) ) . ToList ( ) ;
200+ throw new Exception ( response . StatusDescription ) ;
180201 }
181202 }
182203}
0 commit comments