using System.Collections.Generic; using System.Net; using System.Threading.Tasks; namespace Octokit { /// /// A client for GitHub's User Followers API /// /// /// See the Followers API documentation for more information. /// public class FollowersClient : ApiClient, IFollowersClient { /// /// Initializes a new GitHub User Followers API client. /// /// An API connection public FollowersClient(IApiConnection apiConnection) : base(apiConnection) { } /// /// List the authenticated user’s followers /// /// /// See the API documentation for more information. /// /// A of s that follow the authenticated user. public Task> GetAllForCurrent() { return GetAllForCurrent(ApiOptions.None); } /// /// List the authenticated user’s followers /// /// Options for changing the API response /// /// See the API documentation for more information. /// /// A of s that follow the authenticated user. public Task> GetAllForCurrent(ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Followers(),options); } /// /// List a user’s followers /// /// The login name for the user /// /// See the API documentation for more information. /// /// A of s that follow the passed user. public Task> GetAll(string login) { Ensure.ArgumentNotNullOrEmptyString(login, "login"); return GetAll(login, ApiOptions.None); } /// /// List a user’s followers /// /// The login name for the user /// Options for changing the API response /// /// See the API documentation for more information. /// /// A of s that follow the passed user. public Task> GetAll(string login, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(login, "login"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Followers(login), options); } /// /// List who the authenticated user is following /// /// /// See the API documentation for more information. /// /// A of s that the authenticated user follows. public Task> GetAllFollowingForCurrent() { return GetAllFollowingForCurrent(ApiOptions.None); } /// /// List who the authenticated user is following /// /// Options for changing the API response /// /// See the API documentation for more information. /// /// A of s that the authenticated user follows. public Task> GetAllFollowingForCurrent(ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Following(), options); } /// /// List who a user is following /// /// The login name of the user /// /// See the API documentation for more information. /// /// A of s that the passed user follows. public Task> GetAllFollowing(string login) { Ensure.ArgumentNotNullOrEmptyString(login, "login"); return GetAllFollowing(login, ApiOptions.None); } /// /// List who a user is following /// /// The login name of the user /// Options for changing the API response /// /// See the API documentation for more information. /// /// A of s that the passed user follows. public Task> GetAllFollowing(string login, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(login, "login"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Following(login), options); } /// /// Check if the authenticated user follows another user /// /// The login name of the other user /// /// See the API documentation for more information. /// /// A bool representing the success of the operation. public async Task IsFollowingForCurrent(string following) { Ensure.ArgumentNotNullOrEmptyString(following, "following"); try { var response = await Connection.Get(ApiUrls.IsFollowing(following), null, null).ConfigureAwait(false); return response.HttpResponse.IsTrue(); } catch (NotFoundException) { return false; } } /// /// Check if one user follows another user /// /// The login name of the user /// The login name of the other user /// /// See the API documentation for more information. /// /// A bool representing the success of the operation. public async Task IsFollowing(string login, string following) { Ensure.ArgumentNotNullOrEmptyString(login, "login"); Ensure.ArgumentNotNullOrEmptyString(following, "following"); try { var response = await Connection.Get(ApiUrls.IsFollowing(login, following), null, null).ConfigureAwait(false); return response.HttpResponse.IsTrue(); } catch (NotFoundException) { return false; } } /// /// Follow a user /// /// The login name of the user to follow /// /// See the API documentation for more information. /// /// A bool representing the success of the operation. public async Task Follow(string login) { Ensure.ArgumentNotNullOrEmptyString(login, "login"); try { var requestData = new { }; var response = await Connection.Put(ApiUrls.IsFollowing(login), requestData).ConfigureAwait(false); if (response.HttpResponse.StatusCode != HttpStatusCode.NoContent) { throw new ApiException("Invalid Status Code returned. Expected a 204", response.HttpResponse.StatusCode); } return response.HttpResponse.StatusCode == HttpStatusCode.NoContent; } catch (NotFoundException) { return false; } } /// /// Unfollow a user /// /// The login name of the user to unfollow /// /// See the API documentation for more information. /// /// public Task Unfollow(string login) { Ensure.ArgumentNotNullOrEmptyString(login, "login"); return ApiConnection.Delete(ApiUrls.IsFollowing(login)); } } }