Skip to content

Commit 7a99961

Browse files
committed
Client: Cleaning/refactoring
1 parent 7973935 commit 7a99961

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

Git.hub/Client.cs

+30-34
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Git.hub
1313
/// </summary>
1414
public class Client
1515
{
16-
private RestClient client;
16+
private readonly RestClient _client;
1717

1818
/// <summary>
1919
/// Creates a new client instance for github.com
@@ -26,8 +26,7 @@ public Client() : this("https://api.github.com") { }
2626
/// <param name="apiEndpoint">the host to connect to, e.g. 'https://api.github.com'</param>
2727
public Client(string apiEndpoint)
2828
{
29-
client = new RestClient(apiEndpoint);
30-
client.UserAgent = "mabako/Git.hub";
29+
_client = new RestClient(apiEndpoint) { UserAgent = "mabako/Git.hub" };
3130
}
3231

3332
/// <summary>
@@ -37,10 +36,8 @@ public Client(string apiEndpoint)
3736
/// <param name="password">password</param>
3837
public void setCredentials(string user, string password)
3938
{
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;
4441
}
4542

4643
/// <summary>
@@ -49,10 +46,7 @@ public void setCredentials(string user, string password)
4946
/// <param name="token">oauth2-token</param>
5047
public void setOAuth2Token(string token)
5148
{
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;
5650
}
5751

5852
/// <summary>
@@ -61,16 +55,16 @@ public void setOAuth2Token(string token)
6155
/// <returns>list of repositories</returns>
6256
public IList<Repository> getRepositories()
6357
{
64-
if (client.Authenticator == null)
58+
if (_client.Authenticator == null)
6559
throw new ArgumentException("no authentication details");
6660

6761
var request = new RestRequest("/user/repos?type=all");
6862

69-
var repos = client.GetList<Repository>(request);
63+
var repos = _client.GetList<Repository>(request);
7064
if (repos == null)
7165
throw new Exception("Bad Credentials");
7266

73-
repos.ForEach(r => r._client = client);
67+
repos.ForEach(r => r._client = _client);
7468
return repos;
7569
}
7670

@@ -81,14 +75,14 @@ public IList<Repository> getRepositories()
8175
/// <returns>list of repositories</returns>
8276
public IList<Repository> getRepositories(string username)
8377
{
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);
8680

87-
var list = client.GetList<Repository>(request);
81+
var list = _client.GetList<Repository>(request);
8882
if (list == null)
8983
throw new InvalidOperationException("User does not exist.");
9084

91-
list.ForEach(r => r._client = client);
85+
list.ForEach(r => r._client = _client);
9286
return list;
9387
}
9488

@@ -100,15 +94,15 @@ public IList<Repository> getRepositories(string username)
10094
/// <returns>fetched repository</returns>
10195
public Repository getRepository(string username, string repositoryName)
10296
{
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);
106100

107101
var repo = DoRequest<Repository>(request);
108102
if (repo == null)
109103
return null;
110104

111-
repo._client = client;
105+
repo._client = _client;
112106
repo.Detailed = true;
113107
return repo;
114108
}
@@ -120,13 +114,13 @@ public Repository getRepository(string username, string repositoryName)
120114
/// <returns></returns>
121115
public IList<Repository> getOrganizationRepositories(string organization)
122116
{
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);
125119

126-
var list = client.GetList<Repository>(request);
120+
var list = _client.GetList<Repository>(request);
127121

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; });
130124
return list;
131125
}
132126

@@ -138,7 +132,7 @@ public IList<Repository> getOrganizationRepositories(string organization)
138132
/// <returns>current user</returns>
139133
public User getCurrentUser()
140134
{
141-
if (client.Authenticator == null)
135+
if (_client.Authenticator == null)
142136
throw new ArgumentException("no authentication details");
143137

144138
var request = new RestRequest("/user");
@@ -157,7 +151,7 @@ public async Task<User> GetUserAsync(string userName)
157151

158152
var request = new RestRequest($"/users/{userName}");
159153

160-
var user = await client.ExecuteGetTaskAsync<User>(request);
154+
var user = await _client.ExecuteGetTaskAsync<User>(request);
161155
return user.Data;
162156
}
163157

@@ -172,15 +166,17 @@ public List<Repository> searchRepositories(string query)
172166
request.AddUrlSegment("query", query);
173167

174168
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+
}
177173

178-
return repos.Repositories.Select(r => r.ToV3(client)).ToList();
174+
return repos.Repositories.Select(r => r.ToV3(_client)).ToList();
179175
}
180176

181177
private T DoRequest<T>(IRestRequest request, bool throwOnError = true) where T : new()
182178
{
183-
var response = client.Get<T>(request);
179+
var response = _client.Get<T>(request);
184180
if (response.IsSuccessful)
185181
{
186182
return response.Data;
@@ -193,7 +189,7 @@ public List<Repository> searchRepositories(string query)
193189

194190
if (response.StatusCode == HttpStatusCode.Unauthorized)
195191
{
196-
if (client.Authenticator == null)
192+
if (_client.Authenticator == null)
197193
{
198194
throw new UnauthorizedAccessException("Please configure a GitHub authentication token.");
199195
}

0 commit comments

Comments
 (0)