Skip to content

Commit 41bf71c

Browse files
committed
Starting to modify the way that the internal and external api is designed to something a little more natural and fluent.
1 parent 8ea7566 commit 41bf71c

5 files changed

Lines changed: 67 additions & 23 deletions

File tree

csharp-github-api.IntegrationTests/ApiTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ApiTests
1616
public void ExceptionThrownForBadRequest()
1717
{
1818
var github = new Github("http://github.com/api/v2/json");
19-
github.User.GetFollowers("sgrassie");
19+
//github.User.GetFollowers("sgrassie");
2020
}
2121
}
2222
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<ItemGroup>
6060
<Compile Include="ApiTests.cs" />
6161
<Compile Include="GitHubAuthenticatorTests.cs" />
62+
<Compile Include="Models\UserTests.cs" />
6263
<Compile Include="Properties\AssemblyInfo.cs" />
6364
<Compile Include="UserApiTests.cs" />
6465
</ItemGroup>

csharp-github-api/Core/UserApi.cs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
//----------------------------------------------------------------------
1818

1919

20+
using System;
2021
using RestSharp;
2122
using csharp_github_api.Models;
2223
using System.Collections.Generic;
@@ -48,22 +49,6 @@ public UserApi(string baseUrl, IAuthenticator authenticator) : base(baseUrl, aut
4849
{
4950
}
5051

51-
/// <summary>
52-
/// If the user is authenticated, authenticated instance of the <see cref="UserApi"/> class.
53-
/// If the user is not authenticated then an unauthenticated instance is returned.
54-
/// </summary>
55-
/// <returns>An authenticated instance of the <see cref="UserApi"/> class.</returns>
56-
public UserApi Authenticated()
57-
{
58-
if (Authenticator != null)
59-
{
60-
Client = GetRestClient();
61-
Client.Authenticator = Authenticator;
62-
}
63-
64-
return this;
65-
}
66-
6752
/// <summary>
6853
/// Gets the specified user from GitHub.
6954
/// </summary>
@@ -82,6 +67,7 @@ public User GetUser(string username)
8267
var response = Client.Execute<User>(request);
8368

8469
var user = response.Data;
70+
user.UserApi = this;
8571

8672
return user;
8773
}
@@ -127,12 +113,23 @@ public User FindUserByEmail(string email)
127113
return response.Data;
128114
}
129115

116+
internal UserApi Authenticated()
117+
{
118+
if (Authenticator != null)
119+
{
120+
Client = GetRestClient();
121+
Client.Authenticator = Authenticator;
122+
}
123+
124+
return this;
125+
}
126+
130127
/// <summary>
131128
/// Gets a list of the users that the specified user is following.
132129
/// </summary>
133130
/// <param name="user">The <see cref="User"/> to get the following list for.</param>
134131
/// <returns>A list of the users (username only) that the specified user is following.</returns>
135-
public IList<string> GetFollowing(User user)
132+
internal IList<string> GetFollowing(User user)
136133
{
137134
return GetFollowing(user.Login);
138135
}
@@ -142,7 +139,7 @@ public IList<string> GetFollowing(User user)
142139
/// </summary>
143140
/// <param name="username">The username to get the following list for.</param>
144141
/// <returns>A list of the users (username only) that the specified user is following.</returns>
145-
public IList<string> GetFollowing(string username)
142+
internal IList<string> GetFollowing(string username)
146143
{
147144
if (Client == null) Client = GetRestClient();
148145

@@ -162,7 +159,7 @@ public IList<string> GetFollowing(string username)
162159
/// </summary>
163160
/// <param name="user">The <see cref="User"/> to get the list of followers for.</param>
164161
/// <returns>A string list containing the (username only) list of users who are followers of the specified user.</returns>
165-
public IList<string> GetFollowers(User user)
162+
internal IList<string> GetFollowers(User user)
166163
{
167164
return GetFollowers(user.Login);
168165
}
@@ -172,7 +169,7 @@ public IList<string> GetFollowers(User user)
172169
/// </summary>
173170
/// <param name="username">The user to get the list of followers for.</param>
174171
/// <returns>A string list containing the (username only) list of users who are followers of the specified user.</returns>
175-
public IList<string> GetFollowers(string username)
172+
internal IList<string> GetFollowers(string username)
176173
{
177174
if (Client == null) Client = GetRestClient();
178175

@@ -187,5 +184,10 @@ public IList<string> GetFollowers(string username)
187184

188185
return response.Data;
189186
}
187+
188+
internal bool Follow(string username)
189+
{
190+
throw new NotImplementedException();
191+
}
190192
}
191193
}

csharp-github-api/Github.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public UserApi User
102102
{
103103
get
104104
{
105-
return _userApi.Authenticated();
105+
return _userApi;
106106
}
107107
}
108108
}

csharp-github-api/Models/User.cs

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

19+
using csharp_github_api.Core;
20+
using System.Collections.Generic;
1921
namespace csharp_github_api.Models
2022
{
2123
/// <summary>
2224
/// Represents a GitHub.com user account.
2325
/// </summary>
2426
public class User
2527
{
28+
internal UserApi UserApi;
29+
2630
/* public, authentication not required */
2731
public virtual int Id { get; set; }
2832
public virtual string Login { get; set; }
@@ -44,13 +48,50 @@ public class User
4448
public virtual int PrivateGistCount { get; set;}
4549
public virtual Plan Plan { get; set;}
4650

51+
public User Authenticated
52+
{
53+
get
54+
{
55+
return this;
56+
}
57+
}
58+
59+
public IEnumerable<string> Following
60+
{
61+
get
62+
{
63+
return UserApi.GetFollowing(this);
64+
}
65+
}
66+
67+
/// <summary>
68+
/// Gets an <see cref="IEnumerable{T}"/> of the usernames of the followers of this user.
69+
/// </summary>
70+
public IEnumerable<string> Followers
71+
{
72+
get
73+
{
74+
return UserApi.GetFollowers(this);
75+
}
76+
}
77+
78+
/// <summary>
79+
/// Follow the specified user. Must be authenticated.
80+
/// </summary>
81+
/// <param name="username">The user name of the user on github to follow.</param>
82+
/// <returns></returns>
83+
public bool Follow(string username)
84+
{
85+
return UserApi.Authenticated().Follow(username);
86+
}
87+
4788
public override bool Equals(object obj)
4889
{
4990
if(obj is User)
5091
{
5192
var compareTo = (User) obj;
5293

53-
return compareTo.Id.Equals(Id);
94+
return compareTo.Id.Equals(Id) && compareTo.Name.Equals(Name) && compareTo.Email.Equals(Email);
5495
}
5596

5697
return base.Equals(obj);

0 commit comments

Comments
 (0)