|
| 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 | +} |
0 commit comments