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