Skip to content

Commit 034bee2

Browse files
committed
Refactored namespace slightly.
1 parent 9ac2409 commit 034bee2

7 files changed

Lines changed: 281 additions & 6 deletions

File tree

csharp-github-api.IntegrationTests/ApiTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using csharp_github_api.Core;
56
using NUnit.Framework;
6-
using csharp_github_api.API;
77

88
namespace csharp_github_api.IntegrationTests
99
{

csharp-github-api/Core/Api.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Net;
2+
using RestSharp;
3+
4+
namespace csharp_github_api.Core
5+
{
6+
/// <summary>
7+
/// Base class for specific API classes.
8+
/// </summary>
9+
public abstract class Api
10+
{
11+
public virtual void ThrowExceptionForBadResponseIfNeccessary(RestResponseBase response)
12+
{
13+
if (response.StatusCode == HttpStatusCode.Unauthorized)
14+
{
15+
var message = response.Content;
16+
17+
var exception = new GitHubResponseException(message)
18+
{
19+
Response = response
20+
};
21+
22+
throw exception;
23+
}
24+
}
25+
}
26+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright file="GitHubResponseException.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+
//----------------------------------------------------------------------
18+
19+
using System;
20+
using RestSharp;
21+
22+
namespace csharp_github_api.Core
23+
{
24+
[Serializable]
25+
public class GitHubResponseException : Exception
26+
{
27+
public GitHubResponseException() { }
28+
public GitHubResponseException(string message) : base(message) { }
29+
public GitHubResponseException(string message, Exception inner) : base(message, inner) { }
30+
protected GitHubResponseException(
31+
System.Runtime.Serialization.SerializationInfo info,
32+
System.Runtime.Serialization.StreamingContext context)
33+
: base(info, context) { }
34+
35+
/// <summary>
36+
/// Gets or sets the raw error response from GitHub.
37+
/// </summary>
38+
public RestResponseBase Response
39+
{
40+
get; set;
41+
}
42+
}
43+
}

csharp-github-api/Core/UserApi.cs

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright file="UserApi.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+
//----------------------------------------------------------------------
18+
19+
20+
using RestSharp;
21+
using csharp_github_api.Models;
22+
using System.Collections.Generic;
23+
24+
namespace csharp_github_api.Core
25+
{
26+
/// <summary>
27+
/// Encapsulates access to the Github.com User API.
28+
/// </summary>
29+
/// <remarks>
30+
/// See http://develop.github.com/p/users.html for more details.
31+
/// </remarks>
32+
public class UserApi : Api
33+
{
34+
public readonly string BaseUrl;
35+
36+
private RestClient _client;
37+
private readonly IAuthenticator _authenticator;
38+
39+
/// <summary>
40+
/// Instantiattes a new instance of the <see cref="UserApi"/> class.
41+
/// </summary>
42+
/// <param name="baseUrl">The base url for GitHub's API.</param>
43+
public UserApi(string baseUrl)
44+
{
45+
BaseUrl = baseUrl;
46+
_client = new RestClient(BaseUrl);
47+
}
48+
49+
/// <summary>
50+
/// Instantiattes a new instance of the <see cref="UserApi"/> class.
51+
/// </summary>
52+
/// <param name="baseUrl">The base url for GitHub's API.</param>
53+
/// <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)
55+
{
56+
BaseUrl = baseUrl;
57+
_authenticator = authenticator;
58+
_client = new RestClient(BaseUrl);
59+
}
60+
61+
/// <summary>
62+
/// If the user is authenticated, authenticated instance of the <see cref="UserApi"/> class.
63+
/// If the user is not authenticated then an unauthenticated instance is returned.
64+
/// </summary>
65+
/// <returns>An authenticated instance of the <see cref="UserApi"/> class.</returns>
66+
public UserApi Authenticated()
67+
{
68+
if (_authenticator != null)
69+
{
70+
_client = GetRestClient();
71+
_client.Authenticator = _authenticator;
72+
}
73+
74+
return this;
75+
}
76+
77+
/// <summary>
78+
/// Gets the specified user from GitHub.
79+
/// </summary>
80+
/// <param name="username">The user to get from GitHub.</param>
81+
/// <returns>A <see cref="User"/> instance which encapsulates the response from GitHub for the requested user.</returns>
82+
public User GetUser(string username)
83+
{
84+
if (_client == null) _client = GetRestClient();
85+
86+
var request = new RestRequest
87+
{
88+
Resource = string.Format("/user/show/{0}", username),
89+
RootElement = "user"
90+
};
91+
92+
var response = _client.Execute<User>(request);
93+
94+
var user = response.Data;
95+
96+
return user;
97+
}
98+
99+
/// <summary>
100+
/// Searches for the user on GitHub.
101+
/// </summary>
102+
/// <param name="username">The user to search for.</param>
103+
/// <returns>Returns a lise of <see cref="User"/> instances of GitHub users who may match the search.</returns>
104+
public IList<User> SearchUser(string username)
105+
{
106+
if (_client == null) _client = GetRestClient();
107+
108+
var request = new RestRequest
109+
{
110+
Resource = string.Format("/user/search/{0}", username),
111+
RootElement = "users"
112+
};
113+
114+
var response = _client.Execute<List<User>>(request);
115+
116+
return response.Data;
117+
}
118+
119+
/// <summary>
120+
/// Finds a user specified by their email address.
121+
/// This will only match the email address listed in a users public profile, and is opt-in for everyone.
122+
/// </summary>
123+
/// <param name="email">The email address of the user to search for.</param>
124+
/// <returns>A <see cref="User"/> instance which encapsulates the response from GitHub for the requested user.</returns>
125+
public User FindUserByEmail(string email)
126+
{
127+
if (_client == null) _client = GetRestClient();
128+
129+
var request = new RestRequest
130+
{
131+
Resource = string.Format("/user/email/{0}", email),
132+
RootElement = "users"
133+
};
134+
135+
var response = _client.Execute<User>(request);
136+
137+
return response.Data;
138+
}
139+
140+
/// <summary>
141+
/// Gets a list of the users that the specified user is following.
142+
/// </summary>
143+
/// <param name="user">The <see cref="User"/> to get the following list for.</param>
144+
/// <returns>A list of the users (username only) that the specified user is following.</returns>
145+
public IList<string> GetFollowing(User user)
146+
{
147+
return GetFollowing(user.Login);
148+
}
149+
150+
/// <summary>
151+
/// Gets a list of the users that the specified user is following.
152+
/// </summary>
153+
/// <param name="username">The username to get the following list for.</param>
154+
/// <returns>A list of the users (username only) that the specified user is following.</returns>
155+
public IList<string> GetFollowing(string username)
156+
{
157+
if (_client == null) _client = GetRestClient();
158+
159+
var request = new RestRequest
160+
{
161+
Resource = string.Format("/user/show/{0}/following", username),
162+
RootElement = "users"
163+
};
164+
165+
var response = _client.Execute<List<string>>(request);
166+
167+
return response.Data;
168+
}
169+
170+
/// <summary>
171+
/// Gets a list of the users that the specified user is following.
172+
/// </summary>
173+
/// <param name="user">The <see cref="User"/> to get the list of followers for.</param>
174+
/// <returns>A string list containing the (username only) list of users who are followers of the specified user.</returns>
175+
public IList<string> GetFollowers(User user)
176+
{
177+
return GetFollowers(user.Login);
178+
}
179+
180+
/// <summary>
181+
/// Gets a list of the users that the specified user is following.
182+
/// </summary>
183+
/// <param name="username">The user to get the list of followers for.</param>
184+
/// <returns>A string list containing the (username only) list of users who are followers of the specified user.</returns>
185+
public IList<string> GetFollowers(string username)
186+
{
187+
if (_client == null) _client = GetRestClient();
188+
189+
var request = new RestRequest
190+
{
191+
Resource = string.Format("/user/show/{0}/followers", username),
192+
RootElement = "users"
193+
};
194+
195+
var response = _client.Execute<List<string>>(request);
196+
ThrowExceptionForBadResponseIfNeccessary(response);
197+
198+
return response.Data;
199+
}
200+
201+
private RestClient GetRestClient()
202+
{
203+
return new RestClient(BaseUrl);
204+
}
205+
}
206+
}

csharp-github-api/Github.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
// </copyright>
1717
//----------------------------------------------------------------------
1818

19+
using csharp_github_api.Core;
20+
1921
namespace csharp_github_api
2022
{
2123
using RestSharp;
2224
using Models;
23-
using API;
2425

2526
/// <summary>
2627
/// Access the Github.com API.

csharp-github-api/Models/User.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
// </copyright>
1717
//----------------------------------------------------------------------
1818

19-
using csharp_github_api.API;
2019
namespace csharp_github_api.Models
2120
{
2221
/// <summary>

csharp-github-api/csharp-github-api.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@
5454
<Compile Include="..\SolutionVersion.cs">
5555
<Link>Properties\SolutionVersion.cs</Link>
5656
</Compile>
57-
<Compile Include="API\Api.cs" />
58-
<Compile Include="API\GitHubResponseException.cs" />
59-
<Compile Include="API\UserApi.cs" />
57+
<Compile Include="Core\Api.cs" />
58+
<Compile Include="Core\GitHubResponseException.cs" />
59+
<Compile Include="Core\UserApi.cs" />
6060
<Compile Include="Github.cs" />
6161
<Compile Include="GitHubAuthenticator.cs" />
6262
<Compile Include="Models\Plan.cs" />

0 commit comments

Comments
 (0)