From 83b6331c9f3d9c44fd9366c6068d91332daf4278 Mon Sep 17 00:00:00 2001 From: Henrik Andersson Date: Mon, 27 Jan 2014 12:11:34 +1000 Subject: [PATCH] Refactor User Followers Client Create ApiUrls methods for base Uri's --- Octokit/Clients/IUserFollowersClient.cs | 6 ++-- Octokit/Clients/UserFollowersClient.cs | 38 ++++++++++++++++++------- Octokit/Helpers/ApiUrls.cs | 30 +++++++++++++++++++ 3 files changed, 60 insertions(+), 14 deletions(-) diff --git a/Octokit/Clients/IUserFollowersClient.cs b/Octokit/Clients/IUserFollowersClient.cs index f10361a7..b5b7487a 100644 --- a/Octokit/Clients/IUserFollowersClient.cs +++ b/Octokit/Clients/IUserFollowersClient.cs @@ -60,7 +60,7 @@ namespace Octokit /// See the API documentation for more information. /// /// - Task CheckFollowingForCurrent(string following); + Task IsFollowingForCurrent(string following); /// /// Check if one user follows another user @@ -71,7 +71,7 @@ namespace Octokit /// See the API documentation for more information. /// /// - Task CheckFollowing(string login, string following); + Task IsFollowing(string login, string following); /// /// Follow a user @@ -92,6 +92,6 @@ namespace Octokit /// /// [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Unfollow")] - Task Unfollow(string login); + Task Unfollow(string login); } } diff --git a/Octokit/Clients/UserFollowersClient.cs b/Octokit/Clients/UserFollowersClient.cs index d0f6279b..ebd3c9a3 100644 --- a/Octokit/Clients/UserFollowersClient.cs +++ b/Octokit/Clients/UserFollowersClient.cs @@ -32,7 +32,7 @@ namespace Octokit /// public Task> GetAllForCurrent() { - return ApiConnection.GetAll(new Uri("/user/followers")); + return ApiConnection.GetAll(ApiUrls.Followers()); } /// @@ -43,11 +43,12 @@ namespace Octokit /// See the API documentation for more information. /// /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object[])"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object)")] public Task> GetAll(string login) { Ensure.ArgumentNotNullOrEmptyString(login, "login"); - return ApiConnection.GetAll(new Uri(String.Format("/users/{0}/followers", login))); + return ApiConnection.GetAll(ApiUrls.Followers(login)); } /// @@ -59,7 +60,7 @@ namespace Octokit /// public Task> GetFollowingForCurrent() { - return ApiConnection.GetAll(new Uri("/user/following")); + return ApiConnection.GetAll(ApiUrls.Following()); } /// @@ -70,11 +71,12 @@ namespace Octokit /// See the API documentation for more information. /// /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object[])"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object)")] public Task> GetFollowing(string login) { Ensure.ArgumentNotNullOrEmptyString(login, "login"); - return ApiConnection.GetAll(new Uri(String.Format("/users/{0}/following", login))); + return ApiConnection.GetAll(ApiUrls.Following(login)); } /// @@ -85,13 +87,13 @@ namespace Octokit /// See the API documentation for more information. /// /// - public async Task CheckFollowingForCurrent(string following) + public async Task IsFollowingForCurrent(string following) { Ensure.ArgumentNotNullOrEmptyString(following, "following"); try { - var response = await Connection.GetAsync(new Uri(String.Format("/user/following/{0}", following)), null, null) + var response = await Connection.GetAsync(ApiUrls.IsFollowing(following), null, null) .ConfigureAwait(false); if(response.StatusCode != HttpStatusCode.NotFound && response.StatusCode != HttpStatusCode.NoContent) { @@ -114,14 +116,14 @@ namespace Octokit /// See the API documentation for more information. /// /// - public async Task CheckFollowing(string login, string following) + public async Task IsFollowing(string login, string following) { Ensure.ArgumentNotNullOrEmptyString(login, "login"); Ensure.ArgumentNotNullOrEmptyString(following, "following"); try { - var response = await Connection.GetAsync(new Uri(String.Format("/users/{0}/following/{1}", login, following)), null, null) + var response = await Connection.GetAsync(ApiUrls.IsFollowing(login, following), null, null) .ConfigureAwait(false); if (response.StatusCode != HttpStatusCode.NotFound && response.StatusCode != HttpStatusCode.NoContent) { @@ -143,11 +145,25 @@ namespace Octokit /// See the API documentation for more information. /// /// - public Task Follow(string login) + public async Task Follow(string login) { Ensure.ArgumentNotNullOrEmptyString(login, "login"); - return ApiConnection.Put(new Uri(String.Format("/user/following/{0}", login))); + try + { + var requestData = new { }; + var response = await Connection.PutAsync(ApiUrls.IsFollowing(login), requestData) + .ConfigureAwait(false); + if (response.StatusCode != HttpStatusCode.NoContent) + { + throw new ApiException("Invalid Status Code returned. Expected a 204", response.StatusCode); + } + return response.StatusCode == HttpStatusCode.NoContent; + } + catch (NotFoundException) + { + return false; + } } /// @@ -162,7 +178,7 @@ namespace Octokit { Ensure.ArgumentNotNullOrEmptyString(login, "login"); - return ApiConnection.Delete(new Uri(String.Format("/user/following/{0}", login))); + return ApiConnection.Delete(ApiUrls.IsFollowing(login)); } } } diff --git a/Octokit/Helpers/ApiUrls.cs b/Octokit/Helpers/ApiUrls.cs index acede773..de29e397 100644 --- a/Octokit/Helpers/ApiUrls.cs +++ b/Octokit/Helpers/ApiUrls.cs @@ -833,5 +833,35 @@ namespace Octokit { return "search/code".FormatUri(); } + + public static Uri Followers() + { + return "user/followers".FormatUri(); + } + + public static Uri Followers(string login) + { + return "users/{0}/followers".FormatUri(login); + } + + public static Uri Following() + { + return "user/following".FormatUri(); + } + + public static Uri Following(string login) + { + return "users/{0}/following".FormatUri(login); + } + + public static Uri IsFollowing(string following) + { + return "user/following/{0}".FormatUri(following); + } + + public static Uri IsFollowing(string login, string following) + { + return "users/{0}/following/{1}".FormatUri(login, following); + } } }