Skip to content

Commit 8ea7566

Browse files
committed
Pulled some members up into the base class. Added a copyright header.
1 parent 5219c94 commit 8ea7566

2 files changed

Lines changed: 66 additions & 32 deletions

File tree

csharp-github-api/Core/Api.cs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,57 @@
1-
using System.Net;
2-
using RestSharp;
1+
//-----------------------------------------------------------------------
2+
// <copyright file="cs" company="TemporalCohesion.co.uk">
3+
// Copyright [2010] [Stuart Grassie]
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
// </copyright>
17+
//----------------------------------------------------------------------
318

419
namespace csharp_github_api.Core
520
{
21+
using System.Net;
22+
using RestSharp;
23+
624
/// <summary>
725
/// Base class for specific API classes.
826
/// </summary>
927
public abstract class Api
1028
{
29+
public string BaseUrl;
30+
protected RestClient Client;
31+
protected IAuthenticator Authenticator;
32+
33+
/// <summary>
34+
/// Instantiattes a new instance of the <see cref="Api"/> class.
35+
/// </summary>
36+
/// <param name="baseUrl">The base url for GitHub's API.</param>
37+
protected Api(string baseUrl)
38+
{
39+
BaseUrl = baseUrl;
40+
Client = new RestClient(BaseUrl);
41+
}
42+
43+
/// <summary>
44+
/// Instantiattes a new instance of the <see cref="Api"/> class.
45+
/// </summary>
46+
/// <param name="baseUrl">The base url for GitHub's API.</param>
47+
/// <param name="authenticator">The <see cref="IAuthenticator"/> class to use to authenticate requests to the user api.</param>
48+
protected Api(string baseUrl, IAuthenticator authenticator)
49+
{
50+
BaseUrl = baseUrl;
51+
Authenticator = authenticator;
52+
Client = new RestClient(BaseUrl);
53+
}
54+
1155
public virtual void ThrowExceptionForBadResponseIfNeccessary(RestResponseBase response)
1256
{
1357
if (response.StatusCode == HttpStatusCode.Unauthorized)
@@ -22,5 +66,10 @@ public virtual void ThrowExceptionForBadResponseIfNeccessary(RestResponseBase re
2266
throw exception;
2367
}
2468
}
69+
70+
protected virtual RestClient GetRestClient()
71+
{
72+
return new RestClient(BaseUrl);
73+
}
2574
}
2675
}

csharp-github-api/Core/UserApi.cs

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,21 @@ namespace csharp_github_api.Core
3131
/// </remarks>
3232
public class UserApi : Api
3333
{
34-
public readonly string BaseUrl;
35-
36-
private RestClient _client;
37-
private readonly IAuthenticator _authenticator;
38-
3934
/// <summary>
4035
/// Instantiattes a new instance of the <see cref="UserApi"/> class.
4136
/// </summary>
4237
/// <param name="baseUrl">The base url for GitHub's API.</param>
43-
public UserApi(string baseUrl)
38+
public UserApi(string baseUrl) : base(baseUrl)
4439
{
45-
BaseUrl = baseUrl;
46-
_client = new RestClient(BaseUrl);
4740
}
4841

4942
/// <summary>
5043
/// Instantiattes a new instance of the <see cref="UserApi"/> class.
5144
/// </summary>
5245
/// <param name="baseUrl">The base url for GitHub's API.</param>
5346
/// <param name="authenticator">The <see cref="IAuthenticator"/> class to use to authenticate requests to the user api.</param>
54-
public UserApi(string baseUrl, IAuthenticator authenticator)
47+
public UserApi(string baseUrl, IAuthenticator authenticator) : base(baseUrl, authenticator)
5548
{
56-
BaseUrl = baseUrl;
57-
_authenticator = authenticator;
58-
_client = new RestClient(BaseUrl);
5949
}
6050

6151
/// <summary>
@@ -65,10 +55,10 @@ public UserApi(string baseUrl, IAuthenticator authenticator)
6555
/// <returns>An authenticated instance of the <see cref="UserApi"/> class.</returns>
6656
public UserApi Authenticated()
6757
{
68-
if (_authenticator != null)
58+
if (Authenticator != null)
6959
{
70-
_client = GetRestClient();
71-
_client.Authenticator = _authenticator;
60+
Client = GetRestClient();
61+
Client.Authenticator = Authenticator;
7262
}
7363

7464
return this;
@@ -81,15 +71,15 @@ public UserApi Authenticated()
8171
/// <returns>A <see cref="User"/> instance which encapsulates the response from GitHub for the requested user.</returns>
8272
public User GetUser(string username)
8373
{
84-
if (_client == null) _client = GetRestClient();
74+
if (Client == null) Client = GetRestClient();
8575

8676
var request = new RestRequest
8777
{
8878
Resource = string.Format("/user/show/{0}", username),
8979
RootElement = "user"
9080
};
9181

92-
var response = _client.Execute<User>(request);
82+
var response = Client.Execute<User>(request);
9383

9484
var user = response.Data;
9585

@@ -103,15 +93,15 @@ public User GetUser(string username)
10393
/// <returns>Returns a lise of <see cref="User"/> instances of GitHub users who may match the search.</returns>
10494
public IList<User> SearchUser(string username)
10595
{
106-
if (_client == null) _client = GetRestClient();
96+
if (Client == null) Client = GetRestClient();
10797

10898
var request = new RestRequest
10999
{
110100
Resource = string.Format("/user/search/{0}", username),
111101
RootElement = "users"
112102
};
113103

114-
var response = _client.Execute<List<User>>(request);
104+
var response = Client.Execute<List<User>>(request);
115105

116106
return response.Data;
117107
}
@@ -124,15 +114,15 @@ public IList<User> SearchUser(string username)
124114
/// <returns>A <see cref="User"/> instance which encapsulates the response from GitHub for the requested user.</returns>
125115
public User FindUserByEmail(string email)
126116
{
127-
if (_client == null) _client = GetRestClient();
117+
if (Client == null) Client = GetRestClient();
128118

129119
var request = new RestRequest
130120
{
131121
Resource = string.Format("/user/email/{0}", email),
132122
RootElement = "users"
133123
};
134124

135-
var response = _client.Execute<User>(request);
125+
var response = Client.Execute<User>(request);
136126

137127
return response.Data;
138128
}
@@ -154,15 +144,15 @@ public IList<string> GetFollowing(User user)
154144
/// <returns>A list of the users (username only) that the specified user is following.</returns>
155145
public IList<string> GetFollowing(string username)
156146
{
157-
if (_client == null) _client = GetRestClient();
147+
if (Client == null) Client = GetRestClient();
158148

159149
var request = new RestRequest
160150
{
161151
Resource = string.Format("/user/show/{0}/following", username),
162152
RootElement = "users"
163153
};
164154

165-
var response = _client.Execute<List<string>>(request);
155+
var response = Client.Execute<List<string>>(request);
166156

167157
return response.Data;
168158
}
@@ -184,23 +174,18 @@ public IList<string> GetFollowers(User user)
184174
/// <returns>A string list containing the (username only) list of users who are followers of the specified user.</returns>
185175
public IList<string> GetFollowers(string username)
186176
{
187-
if (_client == null) _client = GetRestClient();
177+
if (Client == null) Client = GetRestClient();
188178

189179
var request = new RestRequest
190180
{
191181
Resource = string.Format("/user/show/{0}/followers", username),
192182
RootElement = "users"
193183
};
194184

195-
var response = _client.Execute<List<string>>(request);
185+
var response = Client.Execute<List<string>>(request);
196186
ThrowExceptionForBadResponseIfNeccessary(response);
197187

198188
return response.Data;
199189
}
200-
201-
private RestClient GetRestClient()
202-
{
203-
return new RestClient(BaseUrl);
204-
}
205190
}
206191
}

0 commit comments

Comments
 (0)